using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using static Shafer;

namespace ShaferExample
{
    /// <summary>
    /// Interaction logic for RegistrationWindow.xaml
    /// </summary>
    public partial class RegistrationWindow : Window
    {
        private readonly NalpLibConfiguration config;

        public RegistrationWindow()
        {
            InitializeComponent();
            SetMainWindow();
            config = new NalpLibConfiguration();
            AppName.Text = App.AppName;
            AppDesc.Text = App.AppDesc;
        }

        private void Window_Close(object sender, MouseButtonEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

        public void Cancel()
        {
            SetMainWindow();
        }

        public void SetMainWindow()
        {
            MainFrame.Source = new Uri("/MainWindow.xaml", UriKind.Relative);
        }

        public void SetOnlineActivation()
        {
            MainFrame.Source = new Uri("/OnlineActivation.xaml", UriKind.Relative);
        }

        public void SetOfflineActivation()
        {
            MainFrame.Source = new Uri("/OfflineActivation.xaml", UriKind.Relative);
        }

        public void SetAblActivation()
        {
            MainFrame.Source = new Uri("/AblActivation.xaml", UriKind.Relative);
        }

        public bool CheckLicense()
        {
            var result = true;
            LicenseStatus licenseStatus = (LicenseStatus)App.Licensing.RefreshLicenseStatus();
            if (AppUtils.LicenseAcquired(licenseStatus))
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    new AppWindow().Show();
                    this.Close();
                });
            }
            else
            {
                result = false;
            }
            return result;
        }

        public void RunJob(Action job)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (object workSender, DoWorkEventArgs workE) =>
            {
                job.Invoke();
            };
            worker.RunWorkerCompleted += (object completeSender, RunWorkerCompletedEventArgs completeE) =>
            {
                Loader.Visibility = Visibility.Hidden;
            };
            Loader.Visibility = Visibility.Visible;
            worker.RunWorkerAsync();
        }

        public void ContinueApp()
        {
            new AppWindow().Show();
            this.Close();
        }

        public void GetLicenseOnline(string licenseCode)
        {
            RunJob(() =>
            {
                App.Licensing.NewLicense();
                App.Licensing.GetLicenseFromServer(licenseCode);
                if (!CheckLicense())
                {
                    App.Licensing.ActivationFailedMessage();
                }
            });
        }

        private void MainFrameLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            (MainFrame.Content as IBaseActivationPage).CurrentWindow = this;
        }

        private void LtcoCheckout(object sender, RoutedEventArgs e)
        {
            popLtcoCheckout.IsOpen = false;
            GetLicenseOnline(tbLtcoCode.Text);
        }

        private void CloseLtcoCheckoutPopup(object sender, RoutedEventArgs e)
        {
            popLtcoCheckout.IsOpen = false;
        }

        public void OpenLtcoCodePopup()
        {
            popLtcoCheckout.IsOpen = true;
        }
    }
}
