using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using static Shafer;

namespace ShaferExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Page, IBaseActivationPage
    {
        public MainWindow()
        {
            InitializeComponent();
            config = new NalpLibConfiguration();
            UpdateLicenseStatus(false);
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Tick;
        }

        private void StartTrial(object sender, RoutedEventArgs e)
        {
            App.Licensing.NewLicense();
            App.Licensing.GetLicenseFromServer(string.Empty);
            UpdateLicenseStatus();
            LicenseStatus licenseStatus = (LicenseStatus)App.Licensing.RefreshLicenseStatus();
            if (licenseStatus == LicenseStatus.PROD_INTRIAL)
            {
                CurrentWindow.ContinueApp();
            } else if (licenseStatus == LicenseStatus.PROD_COMPUTERIDALREADYACTIVE)
            {
                MessageBox.Show("Computer already active");
            }
        }

        private void UpdateLicenseStatus(bool refresh = true)
        {
            timer.Stop();
            if (refresh) { App.Licensing.RefreshLicenseStatus(); };
            btnActivateAppLtco.Visibility = App.Licensing.LanDeamonMode ? Visibility.Visible : Visibility.Collapsed;
            btnActivateApp.IsEnabled = !(App.Licensing.LicenseAcquired);
            btnStartTrial.IsEnabled = !(
                App.Licensing.PassiveMode ||
                App.Licensing.LanDeamonMode ||
                App.Licensing.LicenseStatus == LicenseStatus.PROD_INTRIAL ||
                App.Licensing.LicenseAcquired ||
                App.Licensing.LicenseStatus == LicenseStatus.PROD_COMPUTERIDALREADYACTIVE
                );

            if (App.Licensing.LicenseStatus == LicenseStatus.PROD_INTRIAL)
            {
                btnContinueTrial.Visibility = Visibility.Visible;
                btnStartTrial.Visibility = Visibility.Hidden;
                ExpiresIn = App.Licensing.GetTrialExpirySecs();
                timer.Start();
            }
            else
            {
                btnContinueTrial.Visibility = Visibility.Hidden;
                btnStartTrial.Visibility = Visibility.Visible;
                lblTrialExpiresIn.Content = string.Empty;
            }
        }

        private void ActivatePassive()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "Text files (*.txt)|*.txt"
            };
            if (openFileDialog.ShowDialog() == true)
            {
                CurrentWindow.RunJob(() =>
                {
                    App.Licensing.RunActivationCert(App.Licensing.Config.ClientID, File.ReadAllText(openFileDialog.FileName));
                    if (!CurrentWindow.CheckLicense())
                    {
                        App.Licensing.ActivationFailedMessage();
                    }
                });
            }
        }

        private void GetLicense(object sender, RoutedEventArgs e)
        {
            if (App.Licensing.LanDeamonMode)
            {
                CurrentWindow.GetLicenseOnline("netseat");
            }
            else if(App.Licensing.PassiveMode)
            {
                ActivatePassive();
            }
            else
            {
                CurrentWindow.SetOnlineActivation();
            }
        }

        private void ContinueTrial(object sender, RoutedEventArgs e)
        {
            CurrentWindow.ContinueApp();
        }

        private void Tick(object sender, EventArgs e)
        {
            lblTrialExpiresIn.Content = "Trial expires in " + AppUtils.ExpiresIn(--ExpiresIn);
        }

        private void GetLtcoLicense(object sender, RoutedEventArgs e)
        {
            CurrentWindow.OpenLtcoCodePopup();
        }

        private NalpLibConfiguration config;
        private long ExpiresIn;
        private DispatcherTimer timer = new DispatcherTimer();
        public RegistrationWindow CurrentWindow { get; set; }
    }
}
