using FLY.OBJComponents.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
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 FLY.Winder.UI.Server
{
    /// <summary>
    /// WdSetup.xaml 的交互逻辑
    /// </summary>
    public partial class WdSetup : Window
    {


        public WdSetup()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            var items = new List<PlcGroupItem>();

            //查找 脚本/称重 下的全部 设备连接变量表_xxx 的文件夹
            DirectoryInfo directoryInfo = new DirectoryInfo(@"plcgroups");
            if (!directoryInfo.Exists)
            {
                //异常
                return;
            }

            var dirs = directoryInfo.GetDirectories();
            foreach (var dir in dirs)
            {
                if (dir.Name.StartsWith("设备连接变量表_"))
                {

                    PlcGroupItem item = new PlcGroupItem();
                    item.Name = dir.Name.Substring("设备连接变量表_".Length);
                    item.Path = System.IO.Path.Combine(dir.FullName, "Generated", "plcgroup.json");
                    items.Add(item);
                }
            }

            comboBox.ItemsSource = items;
            foreach (var item in items)
            {
                if (IsSameContent(item.Path, "plcgroup.json"))
                {
                    //找到了
                    comboBox.SelectedItem = item;
                    break;
                }
            }
            
        }

        public static bool IsSameContent(string filePath1, string filePath2)
        {
            //创建一个哈希算法对象
            using (HashAlgorithm hash = HashAlgorithm.Create())
            {
                using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
                {
                    byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组
                    byte[] hashByte2 = hash.ComputeHash(file2);
                    return Enumerable.SequenceEqual(hashByte1, hashByte2);

                    //string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
                    //string str2 = BitConverter.ToString(hashByte2);
                    //return (str1 == str2);//比较哈希码
                }
            }
        }
        private void btnOkClick(object sender, RoutedEventArgs e)
        {
            var item = comboBox.SelectedItem as PlcGroupItem;
            if (item == null) {
                MessageBox.Show("请选择型号!!!");
                return;
            }
            if (MessageBox.Show("需要重启,才能生效", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) {
                File.Copy(item.Path, "plcgroup.json", true);

                //切换为exe的目录,这样程序再次进入时,才能 进入Gage1 文件夹
                System.Environment.CurrentDirectory = "../";
                //重启软件
                AppHelper.AppJustOne.Restart();
            }
        }
    }
    public class PlcGroupItem
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }
    }
}