using CCDCount.MODEL.ConfigModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CCDCountWpf
{
    public class CameraCoboxItem
    {
        public string Name { get; set; }  // 显示的名称
        public string SNValue { get; set; }  // 对应的编号
    }

    public class ShowBindingClass : INotifyPropertyChanged
    {
        /// <summary>
        /// 累计数量
        /// </summary>
        private string allActiveNum = "0";
        public string AllActiveNum
        {
            get { return allActiveNum; }
            set
            {
                allActiveNum = value;
                OnPropertyChanged("AllActiveNum");
            }
        }

        /// <summary>
        /// 累计OK
        /// </summary>
        private string allOkNum = "0";
        public string AllOkNum
        {
            get { return allOkNum; }
            set
            {
                allOkNum = value;
                OnPropertyChanged("AllOkNum");
            }
        }

        /// <summary>
        /// 累计NG
        /// </summary>
        private string allNgNum = "0";
        public string AllNgNum
        {
            get { return allNgNum; }
            set
            {
                allNgNum = value;
                OnPropertyChanged("AllNgNum");
            }
        }

        /// <summary>
        /// 数粒速度
        /// </summary>
        private string shuLiSpeed = "0";
        public string ShuLiSpeed
        {
            get { return shuLiSpeed; }
            set
            {
                shuLiSpeed = value;
                OnPropertyChanged("ShuLiSpeed");
            }
        }

        /// <summary>
        /// 内存监控
        /// </summary>
        private string ramMonitor = "";
        public string RamMonitor
        {
            get { return ramMonitor; }
            set
            {
                ramMonitor = value;
                OnPropertyChanged("RamMonitor");
            }
        }

        /// <summary>
        /// CPU监控
        /// </summary>
        private string _CPUMonitor = "";
        public string CPUMonitor
        {
            get { return _CPUMonitor; }
            set
            {
                _CPUMonitor = value;
                OnPropertyChanged("CPUMonitor");
            }
        }

        /// <summary>
        /// 相机运行状态
        /// </summary>
        private string camRunStatic = "未运行";
        public string CamRunStatic
        {
            get { return camRunStatic; }
            set
            {
                camRunStatic = value;
                OnPropertyChanged("CamRunStatic");
            }
        }

        /// <summary>
        /// 结果图像
        /// </summary>
        private BitmapImage bitmapImage;
        public BitmapImage BitmapImage
        {
            get { return bitmapImage; }
            set
            {
                bitmapImage = value;
                OnPropertyChanged("BitmapImage");
            }
        }

        // <summary>
        /// 缩略图1
        /// </summary>
        private BitmapImage thumbnailImage1;
        public BitmapImage ThumbnailImage1
        {
            get { return thumbnailImage1; }
            set
            {
                thumbnailImage1 = value;
                OnPropertyChanged("ThumbnailImage1");
            }
        }

        // <summary>
        /// 缩略图2
        /// </summary>
        private BitmapImage thumbnailImage2;
        public BitmapImage ThumbnailImage2
        {
            get { return thumbnailImage2; }
            set
            {
                thumbnailImage2 = value;
                OnPropertyChanged("ThumbnailImage2");
            }
        }

        // <summary>
        /// 缩略图3
        /// </summary>
        private BitmapImage thumbnailImage3;
        public BitmapImage ThumbnailImage3
        {
            get { return thumbnailImage3; }
            set
            {
                thumbnailImage3 = value;
                OnPropertyChanged("ThumbnailImage3");
            }
        }

        // <summary>
        /// 缩略图4
        /// </summary>
        private BitmapImage thumbnailImage4;
        public BitmapImage ThumbnailImage4
        {
            get { return thumbnailImage4; }
            set
            {
                thumbnailImage4 = value;
                OnPropertyChanged("ThumbnailImage4");
            }
        }

        /// <summary>
        /// 相机运行状态字体颜色
        /// </summary>
        private SolidColorBrush cameraStaticColor = Brushes.Red;
        public SolidColorBrush CameraStaticColor
        {
            get { return cameraStaticColor; }
            set
            {
                cameraStaticColor = value;
                OnPropertyChanged("CameraStaticColor");
            }
        }

        /// <summary>
        /// 相机曝光时间
        /// </summary>
        public string ExposureTimeValue
        {
            get
            {
                // 从MessageBus获取最新值（保证读取时同步）
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue.ToString();
                }
                return "";
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null && float.TryParse(value, out float newValue))
                {
                    // 同步更新到MessageBus
                    MessageBus.NowSettingLoadMainThread.cameraConfig.ExposureTimeValue = newValue;
                    OnPropertyChanged("ExposureTimeValue");
                }
            }
        }

        /// <summary>
        /// 相机行频
        /// </summary>
        public string AcquistionLineRate
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue.ToString();
                }
                else
                {
                    return "";
                }
            }
            set 
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.cameraConfig.AcquistionLineRateValue = int.Parse(value);
                        OnPropertyChanged("AcquistionLineRate");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("AcquistionLineRate-SetError");
                    }
                    
                }
            }
        }

        /// <summary>
        /// 图像高度
        /// </summary>
        public string ImageHeight
        {
            get
            {
                if(MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.Height.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.cameraConfig.Height = int.Parse(value);
                        OnPropertyChanged("ImageHeight");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("ImageHeight-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 图像宽度
        /// </summary>
        public string ImageWidth
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.Width.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.cameraConfig.Width = int.Parse(value);
                        OnPropertyChanged("ImageWidth");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("ImageWidth-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 相机名称（用户自定义）
        /// </summary>
        public string CamUserName
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName;
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.cameraConfig.CameraName = value;
                        OnPropertyChanged("CamUserName");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("CamUserName-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 图像偏移X
        /// </summary>
        public string CamOffsetX
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.cameraConfig.OffsetX = int.Parse(value);
                        OnPropertyChanged("CamOffsetX");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("CamOffsetX-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 相机列表
        /// </summary>
        public ObservableCollection<CameraCoboxItem> CameraItems { get; } = new ObservableCollection<CameraCoboxItem>();

        /// <summary>
        /// 当前选择的相机SN
        /// </summary>
        public string SelectedCameraSN { get; set;}

        /// <summary>
        /// 预处理阈值
        /// </summary>
        public string RegionThreshold
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.RegionThreshold.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.RegionThreshold = int.Parse(value);
                        OnPropertyChanged("RegionThreshold");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("RegionThreshold-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 数粒通道数
        /// </summary>
        public string Channel
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.Channel.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.Channel = int.Parse(value);
                        OnPropertyChanged("Channel");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Channel-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 噪声滤波阈值
        /// </summary>
        public string NoiseFilter
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.NoiseFilter_Threshold.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.NoiseFilter_Threshold = int.Parse(value);
                        OnPropertyChanged("NoiseFilter");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("NoiseFilter-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最小宽
        /// </summary>
        public string MIN_OBJECT_WIDTH
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MIN_OBJECT_WIDTH.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MIN_OBJECT_WIDTH = int.Parse(value);
                        OnPropertyChanged("MIN_OBJECT_WIDTH");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MIN_OBJECT_WIDTH-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最大宽
        /// </summary>
        public string MAX_OBJECT_WIDTH
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MAX_OBJECT_WIDTH.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MAX_OBJECT_WIDTH = int.Parse(value);
                        OnPropertyChanged("MAX_OBJECT_WIDTH");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MAX_OBJECT_WIDTH-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最小高
        /// </summary>
        public string MIN_OBJECT_HEIGHT
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MIN_OBJECT_HEIGHT.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MIN_OBJECT_HEIGHT = int.Parse(value);
                        OnPropertyChanged("MIN_OBJECT_HEIGHT");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MIN_OBJECT_HEIGHT-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最大高
        /// </summary>
        public string MAX_OBJECT_HEIGHT
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MAX_OBJECT_HEIGHT.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MAX_OBJECT_HEIGHT = int.Parse(value);
                        OnPropertyChanged("MAX_OBJECT_HEIGHT");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MAX_OBJECT_HEIGHT-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最小面积
        /// </summary>
        public string MinArea
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MinArea.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MinArea = int.Parse(value);
                        OnPropertyChanged("MinArea");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MinArea-SetError");
                    }

                }
            }
        }

        /// <summary>
        /// 异常粒最大面积
        /// </summary>
        public string MaxArea
        {
            get
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    return MessageBus.NowSettingLoadMainThread.shuLiConfig.MaxArea.ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (MessageBus.NowSettingLoadMainThread != null)
                {
                    try
                    {
                        MessageBus.NowSettingLoadMainThread.shuLiConfig.MaxArea = int.Parse(value);
                        OnPropertyChanged("MaxArea");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("MaxArea-SetError");
                    }

                }
            }
        }

        //数据绑定核心实现
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
