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

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

        private void GetLicense(object sender, RoutedEventArgs e)
        {
            var licenseCode = tbLicenseCode.Text;
            var activationCode = tbActivationCode.Text;

            CurrentWindow.RunJob(() =>
            {
                App.Licensing.NewLicense();
                App.Licensing.RunActivationCert(licenseCode, activationCode);
                if (!CurrentWindow.CheckLicense())
                {
                    App.Licensing.ActivationFailedMessage();
                }
            });
        }

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

        private void AblActivation(object sender, MouseButtonEventArgs e)
        {
            CurrentWindow.SetAblActivation();
        }

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

        private void Generate(object sender, RoutedEventArgs e)
        {
            App.Licensing.ReturnLicense();
            tbActivationCertificate.Text = App.Licensing.GetActivationCert(tbLicenseCode.Text);
        }

        private void GenerateTrial(object sender, RoutedEventArgs e)
        {
            App.Licensing.ReturnLicense();
            tbActivationCertificate.Text = App.Licensing.GetActivationCert(string.Empty);
        }

        private void LicenseCodeTextChanged(object sender, TextChangedEventArgs e)
        {
            btnGenerate.Visibility = tbLicenseCode.Text.Length > 0 ? Visibility.Visible : Visibility.Hidden;
            btnGenerateTrial.Visibility = tbLicenseCode.Text.Length > 0 ? Visibility.Hidden : Visibility.Visible;
        }

        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.Hidden;
            btnCopyFromClipboard.Visibility = tbActivationCertificate.Text.Length > 0 ? Visibility.Visible : Visibility.Hidden;
        }

        public RegistrationWindow CurrentWindow { get; set; }
    }
}
