using KGIS.Framework.Core.Services;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.Core.Authorize;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
namespace JKRJ_Startup
{
    /// 
    /// App.xaml 的交互逻辑
    /// 
    public partial class App : Application
    {
        private bool v = false;
        private string exeV = "";
        private string licV = "";
        /// 
        /// 重写Startup函数
        /// 
        /// 
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            // 接收参数数组
            var args = e.Args;
            // 定义为字符数组也可以
            //string[] args = e.Args;
            // 判断参数中是否包含 TestWindows
            Validity(null);
            if (v)
            {
                new MainWindow().Show();
            }
            else
            {
                MessageHelper.ShowTips("软件授权失败!");
                Application.Current.Shutdown();
            }
        }
        private void Validity(object obj)
        {
            try
            {
                GetVersion();
                //判断授权方式
                var type = ConfigurationManager.AppSettings.Get("LicenseType");
                if (type == "F")//本地授权
                {
                    Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
                    string guid = ((GuidAttribute)guid_attr).Value;
                    try
                    {
                        string[] licpath = System.IO.Directory.GetFiles(System.Environment.CurrentDirectory, "*.lic", System.IO.SearchOption.TopDirectoryOnly);
                        if (licpath.Length == 0)
                        {
                            MessageHelper.ShowError("授权校验失败!");
                        }
                        if (licpath.Length == 0 || !RunIDService2.Instance.Verify(AuthorizeType.多合一授权, "KGIS_SJJK", exeV, licV, "", "10eaa61"))
                        {
                            KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", exeV, licV);
                            lic.Code = "10eaa61";
                            int num = -1;
                            //lic.DocValidity = Validity;
                            System.Windows.Forms.DialogResult dialog = lic.ShowDialog();
                            if (dialog != System.Windows.Forms.DialogResult.Yes && dialog != System.Windows.Forms.DialogResult.OK)
                            {
                                v = false;
                                return;
                            }
                        }
                        else
                        {
                            v = true;
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogAPI.Debug(ex);
                        KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", exeV, licV);
                        lic.Code = "10eaa61";
                        int num = -1;
                        //lic.DocValidity = Validity;
                        System.Windows.Forms.DialogResult dialog = lic.ShowDialog();
                        if (dialog != System.Windows.Forms.DialogResult.Yes && dialog != System.Windows.Forms.DialogResult.OK)
                        {
                            v = false;
                            return;
                        }
                    }
                }
                else if (type == "D")
                {
                    if (!RunIDService2.Instance.Verify("KGIS_SJJK", "400", "4x"))
                    {
                        KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", exeV, licV);
                        System.Windows.Forms.DialogResult dialog = lic.ShowDialog();
                        if (dialog != System.Windows.Forms.DialogResult.Yes && dialog != System.Windows.Forms.DialogResult.OK)
                        {
                            v = false;
                            return;
                        }
                    }
                }
                else
                {
                    KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", exeV, licV);
                    int num = -1;
                    lic.DocValidity = Validity;
                    System.Windows.Forms.DialogResult dialog = lic.ShowDialog();
                    if (dialog != System.Windows.Forms.DialogResult.Yes && dialog != System.Windows.Forms.DialogResult.OK)
                    {
                        v = false;
                        return;
                    }
                }
                v = true;
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
            }
        }
        private void GetVersion()
        {
            string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName);
            FileVersionInfo file1 = System.Diagnostics.FileVersionInfo.GetVersionInfo(fileName);
            exeV = String.Format("{0}{1}{2}", file1.ProductMajorPart, file1.ProductMinorPart, file1.ProductBuildPart);
            licV = string.Format("{0}x", file1.ProductMajorPart);
        }
    }
}