using Common.system;

using System;
using System.IO;
using System.Text;
using System.Windows;

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

namespace XHWK.WKTool
{
    /// <summary>
    /// ProductVerification.xaml 的交互逻辑
    /// </summary>
    /*
     本地解密  解密成功 日期在有效期内 ，判断当天是否调用添加历史接口，没有则调用
     解密失败 或解密获取的时间 过期 ， 跳到输入code 激活界面
         */
    public partial class ProductVerification : Window
    {
        private readonly XHApi registerController = new XHApi();
        private int serverReturnCode = 0;

        public ProductVerification()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Topmost = true;
            App.BackgroundWorkerHelper.RunWorkerAsync(InvokeActivationDecryptServering, InvokeActivationDecryptServerCompate);
        }
        /// <summary>
        /// 激活信息--验证签名是否过期-调用
        /// </summary>
        /// <returns></returns>
        private object InvokeActivationDecryptServering()
        {
            return App.ServerMsg;
        }

        /// <summary>
        /// 激活信息--验证签名是否过期-返回结果
        /// </summary>
        /// <returns></returns>
        public void InvokeActivationDecryptServerCompate(object obj)
        {
            //解析
            try
            {
                App.Signature = System.IO.File.ReadAllText(App.DataPath + "signature.txt", Encoding.Default);
                string decrypt = DataProvider.TripleDESDecrypt(App.Signature, App.secretKey);
                decrypt = decrypt.Replace("\u0001", "").Replace("\u0007", "").Trim();
                Model_Signatures msgBean = JsonHelper.JsonToObj<Model_Signatures>(decrypt);
                long currentTimeTimestamp = DataProvider.TimestampTotalSeconds(); //当前时间 时间戳
                if (msgBean.endtime > currentTimeTimestamp)//条件成立  解析成功 code 在有效期内
                {
                    if (File.Exists(App.DataPath + "signatureTime.txt"))
                    {
                        string signatureTime = System.IO.File.ReadAllText(App.DataPath + "signatureTime.txt", Encoding.Default);
                        string currentTime = DateTime.Now.ToLongDateString().ToString(); //当前时间  
                        if (signatureTime != currentTime)
                        {
                            App.BackgroundWorkerHelper.RunWorkerAsync(InvokeActivationAddHistoryServering, InvokeActivationAddHistoryServerCompate);
                        }
                    }
                    Dispatcher.Invoke(new Action(() =>
                    {
                        Hide();
                        CreateAMicroLessonWindow win = new CreateAMicroLessonWindow();
                        win.Show();
                        Close();
                    }));
                }
                else//时间失效
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        Hide();
                        KeyVerification wins = new KeyVerification
                        {
                            Topmost = true
                        };
                        wins.ShowDialog();
                        Close();
                    }));
                }
            }
            catch (Exception ex) //解析失败 输code  
            {
                LogHelper.WriteErrLog("ProductVerification(解析失败)" + ex.Message, ex);
                Dispatcher.Invoke(new Action(() =>
                {
                    Hide();
                    KeyVerification wins = new KeyVerification
                    {
                        Topmost = true
                    };
                    wins.ShowDialog();
                    Close();
                }));
            }

        }
        /// <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>
        /// 关闭弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_click(object sender, RoutedEventArgs e)
        {
            closeAction();
        }
        /// <summary>
        /// 退出程序
        /// </summary>
        private void closeAction()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                System.Environment.Exit(0);
            }));
        }

        ~ProductVerification()
        {
            //Console.WriteLine("{0}被销毁", this);
            Dispose(false);
        }
        public void Dispose()
        {
            //Console.WriteLine("{0}被手动销毁", this);
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private bool disposed;
        protected void Dispose(bool disposing)
        {
            //Console.WriteLine("{0}被自动销毁", this);
            if (!disposed)
            {
                if (disposing)
                {
                    //托管资源释放

                }
                //非托管资源释放
                // ...
            }
            disposed = true;
        }
    }
}
