using System;
using System.IO;
using System.Management;
using System.Text;
using System.Threading;
using System.Windows;

using XHWK.WKTool.DAL;
using XHWK.WKTool.Skin;

namespace XHWK.WKTool
{
    /// <summary>
    /// KeyVerification.xaml 的交互逻辑
    /// </summary>
    /*
     输入code 调认证服务，成功跳登录 并且调用添加历史接口，失败继续输code
         */

    public partial class KeyVerification : Window
    {
        private readonly XHApi registerController = new XHApi();
        private int serverReturnCode = 0;

        public KeyVerification()
        {
            InitializeComponent();
            Topmost = true;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;

            Console.WriteLine("初始化KeyVerification");
        }

        private void btnEnd_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txbKey.Text))
            {
                MessageWindow.Show("密匙未输入");
                return;
            }
            projectcode = txbKey.Text.Trim();
            if (projectcode.Length > 8)
            {
                MessageWindow.Show("密匙输入过长");
                txbKey.Text = projectcode.Substring(0, 8);
                return;
            }
            APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeActivationAddServering, InvokeActivationAddServerCompate);
        }

        private string projectcode = string.Empty;

        /// <summary>
        /// 认证服务-调用
        /// </summary>
        /// <returns>
        /// </returns>
        private object InvokeActivationAddServering()
        {
            string device = "pcwk";
            string mac = GetMacAddress();
            serverReturnCode = registerController.ActivationAdd(mac, device, projectcode);
            return APP.ServerMsg;
        }

        /// <summary>
        /// 认证服务-返回结果
        /// </summary>
        /// <returns>
        /// </returns>
        public void InvokeActivationAddServerCompate(object obj)
        {
            if (serverReturnCode == 0)
            {
                MessageWindow.Show("产品激活成功");
                APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeActivationAddHistoryServering, InvokeActivationAddHistoryServerCompate);
                Dispatcher.Invoke(new Action(() =>
                {
                    Hide();
                    CreateAMicroLessonWindow win = new CreateAMicroLessonWindow();
                    //win.Topmost = true;
                    win.Show();

                    Thread.Sleep(200);
                    Close();
                }));
            }
            else
            {
                MessageWindow.Show(APP.ServerMsg);
            }
        }

        /// <summary>
        /// 激活信息--添加激活历史-调用
        /// </summary>
        /// <returns>
        /// </returns>
        private object InvokeActivationAddHistoryServering()
        {
            serverReturnCode = registerController.ActivationAddHistory();
            return APP.ServerMsg;
        }

        /// <summary>
        /// 激活信息--添加激活历史-返回结果
        /// </summary>
        /// <returns>
        /// </returns>
        public void InvokeActivationAddHistoryServerCompate(object obj)
        {
            if (!Directory.Exists(APP.DataPath))
            {
                Directory.CreateDirectory(APP.DataPath);
            }
            string ApplicationData = APP.DataPath + "signatureTime.txt";
            string currentTime = DateTime.Now.ToLongDateString().ToString();//当前时间
            System.IO.File.WriteAllText(ApplicationData, currentTime, Encoding.Default);//存放签名验证日期
        }

        /// <summary>
        /// 获取本机MAC地址
        /// </summary>
        /// <returns>
        /// 本机MAC地址
        /// </returns>
        public static string GetMacAddress()
        {
            try
            {
                string strMac = string.Empty;
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        strMac = mo["MacAddress"].ToString();
                    }
                }
                moc = null;
                mc = null;
                return strMac;
            }
            catch
            {
                return "unknown";
            }
        }

        private void close_click(object sender, RoutedEventArgs e)
        {
            closeAction();
        }

        private void closeAction()
        {
            APP.myloading.Show();
            new Thread(o =>
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    System.Environment.Exit(0);
                }));
            }).Start();
        }
    }
}