using MeterVision.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MeterVision.Dlg
{
    /// <summary>
    /// RegisterDlg.xaml 的交互逻辑
    /// </summary>
    public partial class RegisterDlg : Window
    {
        public RegisterDlg()
        {
            InitializeComponent();

            MachineCodeTextBox.Text = LicenseMana.GetMachineCode();
            this.Title = "未注册";
            if (LicenseMana.mLicenseModel != null)
            {
                LicenseTextBox.Text = LicenseMana.mLicenseModel.LicenseKey;
                this.Title = "已注册";
            }
        }

        private void BtnRegister_Click(object sender, RoutedEventArgs e)
        {
            string licenseKey = LicenseTextBox.Text.Trim();
            if(string.IsNullOrWhiteSpace(licenseKey))
            {
                return;
            }


            try
            {
                bool validated = LicenseMana.ValidateLicense(licenseKey);
                if (validated)
                {
                    //保存成 文件
                    LicenseMana.SaveLicenseKey(licenseKey);
                    MessageBox.Show("注册成功！");
                    DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("注册失败！");
            }

            //
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            //MessageBox.Show("RegisterDlg Close");
        }
        //-------------------------------------------------------
    }
}
