using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace ShaferExample
{
    /// <summary>
    /// Interaction logic for OnlineActivation.xaml
    /// </summary>
    public partial class AblActivation : Page, IBaseActivationPage
    {
        public AblActivation()
        {
            InitializeComponent();
            tbUsername.Focus();
        }

        private void GetLicense(object sender, RoutedEventArgs e)
        {
            var username = tbUsername.Text;
            var password = GetPassword();

            if (OnlineMode)
            {
                CurrentWindow.RunJob(() =>
                {
                    App.Licensing.NewLicense();
                    App.Licensing.SetCredentials(username, password);
                    if (!CurrentWindow.CheckLicense())
                    {
                        App.Licensing.ActivationFailedMessage();
                    }
                });
            } else
            {
                var activationCode = tbActivationCode.Text;
                CurrentWindow.RunJob(() =>
                {
                    App.Licensing.NewLicense();
                    App.Licensing.RunActivationCert(username, activationCode);
                    if (!CurrentWindow.CheckLicense())
                    {
                        App.Licensing.ActivationFailedMessage();
                    }
                });
            }
        }

        public void Generate(object sender, RoutedEventArgs e)
        {
            var username = tbUsername.Text;
            var password = GetPassword();
            App.Licensing.ReturnLicense();
            App.Licensing.SetCredentials(username, password, false);
            Generate();
        }

        private string GetPassword()
        {
            return imgEye.Visibility == Visibility.Collapsed ? pbPassword.Password : tbPassword.Text;
        }

        private void Generate()
        {
            tbActivationCertificate.Text = App.Licensing.GetActivationCert(tbUsername.Text);
        }

        private void EyeOffClick(object sender, RoutedEventArgs e)
        {
            imgEyeOff.Visibility = Visibility.Collapsed;
            imgEye.Visibility = Visibility.Visible;
            tbPassword.Text = pbPassword.Password;
            pbPassword.Visibility = Visibility.Collapsed;
            tbPassword.Visibility = Visibility.Visible;

            tbPassword.CaretIndex = tbPassword.Text.Length;
            tbPassword.ScrollToEnd();
            tbPassword.Focus();
        }

        private void EyeClick(object sender, RoutedEventArgs e)
        {
            imgEye.Visibility = Visibility.Collapsed;
            imgEyeOff.Visibility = Visibility.Visible;
            pbPassword.Password = tbPassword.Text;
            pbPassword.Visibility = Visibility.Visible;
            tbPassword.Visibility = Visibility.Collapsed;

            pbPassword.Focus();
        }

        private void OfflineActivation(object sender, MouseButtonEventArgs e)
        {
            CurrentWindow.SetOfflineActivation();
        }

        private void OnlineActivation(object sender, MouseButtonEventArgs e)
        {
            CurrentWindow.SetOnlineActivation();
        }

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

        private void CopyToClipboard(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(tbActivationCertificate.Text);
            MessageBox.Show("Certificate has been copied to clipboard");
        }

        private void CopyFromClipboard(object sender, RoutedEventArgs e)
        {
            tbActivationCode.Text = Clipboard.GetText();
            MessageBox.Show("Activation code has been pasted from clipboard");
        }

        private void ActivationCertificateTextChanged(object sender, TextChangedEventArgs e)
        {
            btnCopyToClipboard.Visibility = tbActivationCertificate.Text.Length > 0 ? Visibility.Visible : Visibility.Collapsed;
            btnCopyFromClipboard.Visibility = btnCopyToClipboard.Visibility;
        }

        private void ActivationCodeTextChanged(object sender, TextChangedEventArgs e)
        {
            SetActivateButtonVisibility();
        }

        private void OfflineChange(object sender, RoutedEventArgs e)
        {
            SetActivateButtonVisibility();
        }

        private void SetActivateButtonVisibility()
        {
            if (OnlineMode)
            {
                btnActivateApp.Visibility = Visibility.Visible;
            }
            else
            {
                btnActivateApp.Visibility = tbActivationCode.Text.Length > 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public bool OnlineMode
        {
            get
            {
                return !(cbOffline.IsChecked.HasValue && cbOffline.IsChecked.Value);
            }
        }

        public RegistrationWindow CurrentWindow { get; set; }
    }
}
