using KGIS.Framework.Core.Services;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.Core.Authorize;
using KUI;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Newtonsoft.Json;
using System.Linq;
namespace JKRJ_Startup
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        private string guid = string.Empty;
        private Dictionary keyValuePairs = new Dictionary();
        public MainWindow()
        {
            //ListenerHelper.Instance.License(null);
            //Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
            //guid = ((GuidAttribute)guid_attr).Value;
            //if (RunIDService.VerifyModel(guid, "4a924103-02dd-4d8d-b3ea-2b26a24d6b4b") || RunIDService.VerifyModel(guid, "6c09e2e7-1b2c-4a2a-b42c-11151ad40519") || RunIDService.VerifyModel(guid, "C6273157-3215-49C1-AFB8-59A3DF96A398") || RunIDService.VerifyModel(guid, "2ef73d4e-6bfd-4b48-b964-a76b75f3b630") || RunIDService.VerifyModel(guid, "7acaaadd-f2b6-4c60-a21e-cc31640e2bd9"))
            //{
            //}
            InitializeComponent();
            #region 陕西发布版本
            //keyValuePairs.Add("单图斑建库&e8581fc9-fa1e-4a3c-8eca-6ad6d8b0d2bd_DTBJK", "DataDTBJK_DTBJK");
            //keyValuePairs.Add("日常变更建库&08716cf2-16e4-445a-8fd6-cb50010eaa61_RCBG", "BuildDB_RCBG");
            //keyValuePairs.Add("年度变更建库&08716cf2-16e4-445a-8fd6-cb50010eaa61_NDBG", "BuildDB_NDBG");
            #endregion
            #region 山西,青海版本
            keyValuePairs.Add("图斑预处理&e8581fc9-fa1e-4a3c-8eca-6ad6d8b0d2bd_DTBJK", "DataDTBJK_YCLJK");
            keyValuePairs.Add("单图斑建库&e8581fc9-fa1e-4a3c-8eca-6ad6d8b0d2bd_DTBJK", "DataDTBJK_DTBJK");
            keyValuePairs.Add("单图斑质检&e8581fc9-fa1e-4a3c-8eca-6ad6d8b0d2bd_DTBJK", "DataDTBJK_WYZS");
            #endregion
           this.txtSubTitle.Content = KGIS.Framework.Utils.SysConfigsOprator.GetAppsetingValueByKey("SystemName");
            int i = 0;
            string ShowAllApp = ConfigurationManager.AppSettings.Get("ShowAllApp");
            List LicModels = JsonConvert.DeserializeObject>(RunIDService2.Instance.Models);
            foreach (var item in keyValuePairs.Keys)
            {
                string[] pra = item.Split('&');
                LicIDModel mod = LicModels.FirstOrDefault(f => f.ID == pra[1]);
                string tag = string.Empty;
                if (mod != null)
                {
                    if (mod.Models != null)
                    {
                        foreach (var subMod in mod.Models)
                        {
                            tag += subMod.ID + ";";
                        }
                    }
                    else
                    {
                        tag = mod.ID;
                    }
                }
                i++;
                KFontIconButton btn = new KFontIconButton();
                btn.Tag = keyValuePairs[item] + "_" + tag;
                btn.Content = pra[0];
                btn.IsEnabled = RunIDService2.Instance.Models.Contains(pra[1]);
                btn.BorderThickness = new Thickness(0);
                btn.Width = 300;
                btn.Height = 150;
                btn.Margin = new Thickness(40, 20, 40, 20);
                btn.FontSize = 38;
                btn.Foreground = System.Windows.Media.Brushes.White;
                btn.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(240, 240, 128, 128));
                btn.MouseOverBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(190, 240, 128, 128));
                btn.MouseOverBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(190, 240, 128, 128));
                if (i % 2 == 0)
                {
                    btn.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(240, 135, 206, 235));
                    btn.MouseOverBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(190, 135, 206, 235));
                    btn.PressedBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(190, 135, 206, 235));
                }
                btn.FIconSize = 0;
                btn.Click += Btn_Click;
                if (ShowAllApp.ToLower() == "true" || btn.IsEnabled)
                    Apps.Children.Add(btn);
            }
        }
        #region 标题栏事件
        /// 
        /// 窗口移动事件
        /// 
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
        int i = 0;
        /// 
        /// 标题栏双击事件
        ///         
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            i += 1;
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += (s, e1) => { timer.IsEnabled = false; i = 0; };
            timer.IsEnabled = true;
            if (i % 2 == 0)
            {
                timer.IsEnabled = false;
                i = 0;
                this.WindowState = this.WindowState == WindowState.Maximized ?
                              WindowState.Normal : WindowState.Maximized;
            }
        }
        /// 
        /// 窗口最小化
        /// 
        private void btn_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized; //设置窗口最小化
        }
        /// 
        /// 窗口最大化与还原
        /// 
        private void btn_max_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal; //设置窗口还原
            }
            else
            {
                this.WindowState = WindowState.Maximized; //设置窗口最大化
            }
        }
        /// 
        /// 窗口关闭
        ///         
        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion 标题栏事件
        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                KFontIconButton btn = (sender as KFontIconButton);
                if (btn.Tag == null) return;
                string[] str = btn.Tag.ToString().Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
                string args = string.Empty;
                for (int i = 1; i < str.Length; i++)
                {
                    args += $" {str[i]}";
                }
                var psi = new ProcessStartInfo("Kingo." + str[0] + ".exe", $"{args}");
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.RedirectStandardError = true;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                var pes = Process.Start(psi);
                this.WindowState = WindowState.Minimized;
                this.ShowInTaskbar = false;
                var sout = pes.StandardOutput;
                while (!sout.EndOfStream)
                {
                    var line = sout.ReadLine();
                    if (String.IsNullOrEmpty(line)) continue;
                }
                pes.WaitForExit();
                pes.Close();
                this.WindowState = WindowState.Normal;
                this.ShowInTaskbar = true;
            }
            catch (Exception ex)
            {
                MessageHelper.ShowTips(ex.Message);
            }
        }
    }
    public class LicIDModel
    {
        /// 
        /// 
        /// 
        public string ID { get; set; }
        /// 
        /// 
        /// 
        public List Models { get; set; }
    }
}