using CCDCount.DLL;
using CCDCount.MODEL.CameraClass;
using CCDCount.MODEL.ConfigModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace CCDCountWpf.WpfPage
{
    /// <summary>
    /// SettingPage.xaml 的交互逻辑
    /// </summary>
    public partial class SettingPage : Page
    {
        public SettingPage()
        {
            InitializeComponent();
            DataContext = ShowMessageBus.ShowBinding;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (MessageBus.NowSettingLoadMainThread != null)
            {
                CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
                if(MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == -1)
                {
                    CheckModel1RadBtn.IsChecked = true;
                }
                else if(MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 0)
                {
                    CheckModel2RadBtn.IsChecked = true;
                }
                else if (MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 1)
                {
                    CheckModel3RadBtn.IsChecked = true;
                }
                else if (MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode == 2)
                {
                    CheckModel4RadBtn.IsChecked = true;
                }
            }
            else
            {
                CamSelectConBox.SelectedValue = "";
            }
        }

        private void CameraSettingBtn_Click(object sender, RoutedEventArgs e)
        {
            CameraSettingGrid.Visibility = Visibility.Visible;
            ShuLiSettingGrid.Visibility = Visibility.Collapsed;
        }

        private void ShuLiSettingBtn_Click(object sender, RoutedEventArgs e)
        {
            CameraSettingGrid.Visibility = Visibility.Collapsed;
            ShuLiSettingGrid.Visibility = Visibility.Visible;
        }

        private void CamListFlushBtn_Click(object sender, RoutedEventArgs e)
        {
            List<CameraInfoClass> CameraInfoList = new List<CameraInfoClass>();
            MessageBus.CameraClass.GetCameraList(out CameraInfoList);
            if (CameraInfoList.Count > 0)
            {
                ShowMessageBus.ShowBinding.CameraItems.Clear();
                ShowMessageBus.ShowBinding.CameraItems.Add(new CameraCoboxItem() { Name = "请选择相机", SNValue = "" });
                foreach (var item in CameraInfoList)
                {
                    ShowMessageBus.ShowBinding.CameraItems.Add(new CameraCoboxItem()
                    {
                        Name = item.DeviceName + "_" + item.DeviceSN,
                        SNValue = item.DeviceSN
                    });
                }
                CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
            }
        }

        private void CamSelectConBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(ShowMessageBus.ShowBinding.CameraItems.Count == 0) return;
            //string ThisItemSN = ShowMessageBus.ShowBinding.CameraSNNum;
            string ThisItemSN = CamSelectConBox.SelectedValue.ToString();
            if (MessageBus.NowSettingLoadMainThread == null || ThisItemSN == MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum) return;
            if (ThisItemSN != string.Empty && ThisItemSN != null)
            {
                if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
                {
                    var confirmResult = MessageBox.Show("检测到正在识别，切换相机需要关闭识别，是否继续切换",
                                      "切换确认", MessageBoxButton.YesNo);
                    if (confirmResult == MessageBoxResult.No)
                    {
                        CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
                        return;
                    }
                    MessageBus.NowSettingLoadMainThread.StopMianThread();
                }
                if (MessageBus.NowSettingLoadMainThread.ReLoadCamera(ThisItemSN))
                {
                    MessageBus.NowSettingLoadMainThread.GetCameraConfig();
                    ShowMessageBus.ShowBinding.ImageWidth = MessageBus.NowSettingLoadMainThread.cameraConfig.Width.ToString();
                    ShowMessageBus.ShowBinding.CamUserName = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName;
                    ShowMessageBus.ShowBinding.CamOffsetX = MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX.ToString();
                    ShowMessageBus.ShowBinding.ExposureTimeValue = MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue.ToString();
                    ShowMessageBus.ShowBinding.AcquistionLineRate = MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue.ToString();
                    ShowMessageBus.ShowBinding.CameraSNNum = ThisItemSN;
                }
                else
                {
                    MessageBox.Show("切换相机失败！");
                    CamSelectConBox.SelectedItem = MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
                    return;
                }
            }
            else
            {
                if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
                {
                    var confirmResult = MessageBox.Show("检测到正在识别，切换相机需要关闭识别，是否继续切换",
                                      "切换确认", MessageBoxButton.YesNo);
                    if (confirmResult == MessageBoxResult.No)
                    {
                        CamSelectConBox.SelectedValue = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
                        return;
                    }
                    MessageBus.NowSettingLoadMainThread.StopMianThread();
                }
                else
                {
                    MessageBus.NowSettingLoadMainThread.DisposeCamera();
                }
                ShowMessageBus.ShowBinding.ImageWidth = "0";
                ShowMessageBus.ShowBinding.CamUserName = string.Empty;
                ShowMessageBus.ShowBinding.CamOffsetX = "0";
                ShowMessageBus.ShowBinding.ExposureTimeValue = "0";
                ShowMessageBus.ShowBinding.AcquistionLineRate = "0";
                ShowMessageBus.ShowBinding.CameraSNNum = null;
            }
        }

        private void CheckModel1RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            ShowMessageBus.ShowBinding.PandingCode = "-1";
            WHParaPanel.Visibility = Visibility.Collapsed;
            AreaParaPanel.Visibility = Visibility.Collapsed;
        }

        private void CheckModel2RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            ShowMessageBus.ShowBinding.PandingCode = "0";
            WHParaPanel.Visibility = Visibility.Visible;
            AreaParaPanel.Visibility = Visibility.Collapsed;
        }

        private void CheckModel3RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            ShowMessageBus.ShowBinding.PandingCode = "1";
            WHParaPanel.Visibility = Visibility.Collapsed;
            AreaParaPanel.Visibility = Visibility.Visible;
        }

        private void CheckModel4RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            ShowMessageBus.ShowBinding.PandingCode = "2";
            WHParaPanel.Visibility = Visibility.Visible;
            AreaParaPanel.Visibility = Visibility.Visible;
        }

        //private void SaveAllConfigBtn_Click(object sender, RoutedEventArgs e)
        //{
        //    List<SaveConfigModel> SaveConfig = new List<SaveConfigModel>();
        //    if(MessageBus.MainThreadS.Count <= 0)
        //    {
        //        return;
        //    }
        //    foreach (var item in MessageBus.MainThreadS)
        //    {
        //        SaveConfig.Add(new SaveConfigModel()
        //        {
        //            CameraConfig = item.cameraConfig,
        //            ShuLiConfigClass = item.shuLiConfig
        //        });
        //    }
        //    if (!Directory.Exists(".\\Config\\")) Directory.CreateDirectory(".\\Config\\");
        //    {
        //        XmlStorage.SerializeToXml(SaveConfig, ".\\Config\\CCDCountConfig.xml");
        //    }
        //}


        /// <summary>
        /// 加载配方
        /// </summary>
        /// <param name="FormulationConfig"></param>
        private void LoadFormulation(FormulationConfigClass FormulationConfig)
        {
            ShowMessageBus.ShowBinding.AcquistionLineRate = FormulationConfig.AcquistionLineRateValue.ToString();
            ShowMessageBus.ShowBinding.ExposureTimeValue = FormulationConfig.ExposureTimeValue.ToString();
            ShowMessageBus.ShowBinding.Channel = FormulationConfig.Channel.ToString();
            ShowMessageBus.ShowBinding.FormulationName = FormulationConfig.FormulationName;
            ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = FormulationConfig.MAX_Object_LENGTH.ToString();
            ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = FormulationConfig.MIN_Object_LENGTH.ToString();
            ShowMessageBus.ShowBinding.PandingCode = FormulationConfig.PandingCode.ToString();
            ShowMessageBus.ShowBinding.RegionThreshold = FormulationConfig.RegionThreshold.ToString();
        }

        private void ParametricTrainingBtn_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(KeLiW_TBX.Text)||string.IsNullOrEmpty(KeLiL_TBX.Text))
            {
                MessageBox.Show("请输入标准颗粒的宽高");
                return;
            }
            int KeliW = Convert.ToInt32(KeLiW_TBX.Text);
            int KeliL = Convert.ToInt32(KeLiL_TBX.Text);
            var paravalue = MessageBus.NowSettingLoadMainThread.ParameterTrain(KeliW, KeliL);
            ShowMessageBus.ShowBinding.NoiseFilter = paravalue.NoiseFilterThreshold.ToString();
            ShowMessageBus.ShowBinding.MaxArea = ((int)(paravalue.MaxArea*1.1)).ToString();
            ShowMessageBus.ShowBinding.MinArea = ((int)(paravalue.MinArea*0.9)).ToString();
            ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = ((int)(paravalue.MaxLength*1.1)).ToString();
            ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = ((int)(paravalue.MinLength*0.9)).ToString();
        }
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox textBox = sender as TextBox;
                if (textBox != null)
                {
                    // 强制更新源数据
                    BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
                    bindingExpression.UpdateSource();
                }
            }
        }
    }
}
