using CCDCount.MODEL.CameraClass;
using CCDCount.MODEL.ConfigModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

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

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            CamNumCoBox.Items.Add("单相机");
            CamNumCoBox.Items.Add("双相机");
            CamNumCoBox.Items.Add("三相机");
            CamNumCoBox.Items.Add("四相机");
            CamNumCoBox.SelectedIndex = 0;
            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 CamNumCoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(CamNumCoBox.SelectedItem.ToString() == "单相机")
            {
                Cam1Btn.Visibility = Visibility.Visible;
                Cam2Btn.Visibility = Visibility.Collapsed;
                Cam3Btn.Visibility = Visibility.Collapsed;
                Cam4Btn.Visibility = Visibility.Collapsed;
                if(!(MessageBus.SeetingNowLoadInMaThreadsIndex>0&&MessageBus.SeetingNowLoadInMaThreadsIndex < 1))
                {
                    Cam1Btn_Click(null, null);
                }
            }
            else if(CamNumCoBox.SelectedItem.ToString() == "双相机")
            {
                Cam1Btn.Visibility = Visibility.Visible;
                Cam2Btn.Visibility = Visibility.Visible;
                Cam3Btn.Visibility = Visibility.Collapsed;
                Cam4Btn.Visibility = Visibility.Collapsed;
                if (!(MessageBus.SeetingNowLoadInMaThreadsIndex > 0 && MessageBus.SeetingNowLoadInMaThreadsIndex < 2))
                {
                    Cam1Btn_Click(null, null);
                }
            }
            else if(CamNumCoBox.SelectedItem.ToString() == "三相机")
            {
                Cam1Btn.Visibility = Visibility.Visible;
                Cam2Btn.Visibility = Visibility.Visible;
                Cam3Btn.Visibility = Visibility.Visible;
                Cam4Btn.Visibility = Visibility.Collapsed;
                if (!(MessageBus.SeetingNowLoadInMaThreadsIndex > 0 && MessageBus.SeetingNowLoadInMaThreadsIndex < 3))
                {
                    Cam1Btn_Click(null, null);
                }
            }
            else if(CamNumCoBox.SelectedItem.ToString() == "四相机")
            {
                Cam1Btn.Visibility = Visibility.Visible;
                Cam2Btn.Visibility = Visibility.Visible;
                Cam3Btn.Visibility = Visibility.Visible;
                Cam4Btn.Visibility = Visibility.Visible;
            }
        }

        private void Cam1Btn_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBus.MainThreadS == null|| MessageBus.MainThreadS.Count<1|| MessageBus.MainThreadS[0] == null)
                return;
            MessageBus.SeetingNowLoadInMaThreadsIndex = 0;
            MessageBus.NowSettingLoadMainThread = MessageBus.MainThreadS[0];
        }

        private void Cam2Btn_Click(object sender, RoutedEventArgs e)
        {
            //MessageBus.ShowBondage.ExposureTimeValue = "300";
            MessageBox.Show($"NowThread.ETime:{MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue},Thread[0].ETime:{MessageBus.MainThreadS[0].cameraConfig.ExposureTimeValue}");
        }

        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)
            {
                MessageBus.ShowBinding.CameraItems.Clear();
                MessageBus.ShowBinding.CameraItems.Add(new CameraCoboxItem() { Name = "请选择相机", SNValue = "" });
                foreach (var item in CameraInfoList)
                {
                    MessageBus.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(MessageBus.ShowBinding.CameraItems.Count == 0) return;
            string ThisItemSN = MessageBus.ShowBinding.SelectedCameraSN;
            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();
                    MessageBus.ShowBinding.ImageWidth = MessageBus.NowSettingLoadMainThread.cameraConfig.Width.ToString();
                    MessageBus.ShowBinding.ImageHeight = MessageBus.NowSettingLoadMainThread.cameraConfig.Height.ToString();
                    MessageBus.ShowBinding.CamUserName = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName;
                    MessageBus.ShowBinding.CamOffsetX = MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX.ToString();
                    MessageBus.ShowBinding.ExposureTimeValue = MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue.ToString();
                    MessageBus.ShowBinding.AcquistionLineRate = MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue.ToString();
                    MessageBus.NowSettingLoadMainThread.cameraConfig.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();
                }
                MessageBus.ShowBinding.ImageWidth = "0";
                MessageBus.ShowBinding.ImageHeight = "0";
                MessageBus.ShowBinding.CamUserName = string.Empty;
                MessageBus.ShowBinding.CamOffsetX = "0";
                MessageBus.ShowBinding.ExposureTimeValue = "0";
                MessageBus.ShowBinding.AcquistionLineRate = "0";
                MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum = null;
            }
        }

        private void CheckModel1RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode = -1;
            WHParaPanel.Visibility = Visibility.Collapsed;
            AreaParaPanel.Visibility = Visibility.Collapsed;
        }

        private void CheckModel2RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode = 0;
            WHParaPanel.Visibility = Visibility.Visible;
            AreaParaPanel.Visibility = Visibility.Collapsed;
        }

        private void CheckModel3RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            MessageBus.NowSettingLoadMainThread.shuLiConfig.PandingCode = 1;
            WHParaPanel.Visibility = Visibility.Collapsed;
            AreaParaPanel.Visibility = Visibility.Visible;
        }

        private void CheckModel4RadBtn_Checked(object sender, RoutedEventArgs e)
        {
            MessageBus.NowSettingLoadMainThread.shuLiConfig.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");
            }
        }
    }
}
