using System;
using System.ComponentModel;
using System.Drawing.Imaging;
using AntdUI;
using DH.Commons.Enums;
using OpenCvSharp;

namespace DH.Commons.Base
{
    public class CameraBase : NotifyProperty
    {
        // 私有字段 + 带通知的属性(与DetectionLabel风格一致)
        private bool _isEnabled = false;
        private bool _isallPicEnabled = true;//默认全画幅
        private bool _isContinueMode = false;
        private bool _isSavePicEnabled = false;
        private string _imageSaveDirectory;
        private EnumCamType _CamType;
        private ImageFormat _imageFormat = ImageFormat.Jpeg;
        private bool _isHardwareTrigger = true;
        private string _serialNumber = string.Empty;
        private string _cameraName = string.Empty;
        private string _cameraIP = string.Empty;
        private string _computerIP = string.Empty;
        private bool _isDirectHardwareTrigger = false;
        private float _gain =6;
        private float _rotateImage = 0;
        private float _exposure = 200;
        private float _triggerDelay = 0;
        private decimal _roiX = 0;
        private decimal _roiY = 0;
        private decimal _roiW = 2448;
        private decimal _roiH = 2048;
        private int _lineDebouncerTime = 0;

        public volatile int SnapshotCount = 0;

        [Category("采图模式")]
        [DisplayName("连续模式")]
        [Description("是否连续模式。true:连续模式采图;false:触发模式采图")]
        public bool IsContinueMode
        {
            get => _isContinueMode;
            set
            {
                if (_isContinueMode == value) return;
                _isContinueMode = value;
                OnPropertyChanged(nameof(IsContinueMode));
            }
        }

        public virtual bool IsEnabled
        {
            get => _isEnabled;
            set
            {
                if (_isEnabled == value) return;
                _isEnabled = value;
                OnPropertyChanged(nameof(IsEnabled));
            }
        }
        public virtual bool IsAllPicEnabled
        {
            get => _isallPicEnabled;
            set
            {
                if (_isallPicEnabled == value) return;
                _isallPicEnabled = value;
                OnPropertyChanged(nameof(IsAllPicEnabled));
            }
        }
        public virtual bool IsSavePicEnabled
        {
            get => _isSavePicEnabled;
            set
            {
                if (_isSavePicEnabled == value) return;
                _isSavePicEnabled = value;
                OnPropertyChanged(nameof(IsSavePicEnabled));
            }
        }

        [Category("图片保存")]
        [DisplayName("图片保存文件夹")]
        [Description("图片保存文件夹")]
        public virtual string ImageSaveDirectory
        {
            get => _imageSaveDirectory;
            set
            {
                if (_imageSaveDirectory == value) return;
                _imageSaveDirectory = value;
                OnPropertyChanged(nameof(ImageSaveDirectory));
            }
        }

        [Category("图片保存")]
        [DisplayName("图片保存格式")]
        [Description("图片保存格式")]
        public ImageFormat ImageFormat
        {
            get => _imageFormat;
            set
            {
                if (_imageFormat == value) return;
                _imageFormat = value;
                OnPropertyChanged(nameof(ImageFormat));
            }
        }
        [Category("设备配置")]
        [DisplayName("相机类型")]
        [Description("相机类型")]
        public EnumCamType  CamType
        {
            get => _CamType;
            set
            {
                if (_CamType == value) return;
                _CamType = value;
                OnPropertyChanged(nameof(CamType));
            }
        }

        [Category("采图模式")]
        [DisplayName("硬触发")]
        [Description("是否硬触发模式。true:硬触发;false:软触发")]
        public bool IsHardwareTrigger
        {
            get => _isHardwareTrigger;
            set
            {
                if (_isHardwareTrigger == value) return;
                _isHardwareTrigger = value;
                OnPropertyChanged(nameof(IsHardwareTrigger));
            }
        }

        public string SerialNumber
        {
            get => _serialNumber;
            set
            {
                if (_serialNumber == value) return;
                _serialNumber = value;
                OnPropertyChanged(nameof(SerialNumber));
            }
        }

        public string CameraName
        {
            get => _cameraName;
            set
            {
                if (_cameraName == value) return;
                _cameraName = value;
                OnPropertyChanged(nameof(CameraName));
            }
        }

        public string CameraIP
        {
            get => _cameraIP;
            set
            {
                if (_cameraIP == value) return;
                _cameraIP = value;
                OnPropertyChanged(nameof(CameraIP));
            }
        }

        public string ComputerIP
        {
            get => _computerIP;
            set
            {
                if (_computerIP == value) return;
                _computerIP = value;
                OnPropertyChanged(nameof(ComputerIP));
            }
        }

        [Category("采图模式")]
        [DisplayName("是否传感器直接硬触发")]
        [Description("是否传感器直接硬触发。true:传感器硬触发,不通过软件触发;false:通过软件触发IO 的硬触发模式")]
        public bool IsDirectHardwareTrigger
        {
            get => _isDirectHardwareTrigger;
            set
            {
                if (_isDirectHardwareTrigger == value) return;
                _isDirectHardwareTrigger = value;
                OnPropertyChanged(nameof(IsDirectHardwareTrigger));
            }
        }

        [Category("相机设置")]
        [DisplayName("增益")]
        [Description("Gain:增益,-1:不设置,不同型号相机的增益,请参考mvs")]
        public float Gain
        {
            get => _gain;
            set
            {
                if (_gain.Equals(value)) return;
                _gain = value;
                OnPropertyChanged(nameof(Gain));
            }
        }

        [Category("图像旋转")]
        [DisplayName("默认旋转")]
        [Description("默认旋转,相机开启后默认不旋转")]
        public virtual float RotateImage
        {
            get => _rotateImage;
            set
            {
                if (_rotateImage.Equals(value)) return;
                _rotateImage = value;
                OnPropertyChanged(nameof(RotateImage));
            }
        }

        [Category("取像配置")]
        [DisplayName("曝光")]
        [Description("曝光")]
        public virtual float Exposure
        {
            get => _exposure;
            set
            {
                if (_exposure.Equals(value)) return;
                _exposure = value;
                OnPropertyChanged(nameof(Exposure));
            }
        }

        [Category("相机设置")]
        [DisplayName("硬触发后的延迟")]
        [Description("TriggerDelay:硬触发后的延迟,单位:us 微秒")]
        public float TriggerDelay
        {
            get => _triggerDelay;
            set
            {
                if (_triggerDelay.Equals(value)) return;
                _triggerDelay = value;
                OnPropertyChanged(nameof(TriggerDelay));
            }
        }

        public decimal ROIX
        {
            get => _roiX;
            set
            {
                if (_roiX == value) return;
                _roiX = value;
                OnPropertyChanged(nameof(ROIX));
            }
        }

        public decimal ROIY
        {
            get => _roiY;
            set
            {
                if (_roiY == value) return;
                _roiY = value;
                OnPropertyChanged(nameof(ROIY));
            }
        }

        public decimal ROIW
        {
            get => _roiW;
            set
            {
                if (_roiW == value) return;
                _roiW = value;
                OnPropertyChanged(nameof(ROIW));
            }
        }

        public decimal ROIH
        {
            get => _roiH;
            set
            {
                if (_roiH == value) return;
                _roiH = value;
                OnPropertyChanged(nameof(ROIH));
            }
        }

        [Category("相机设置")]
        [DisplayName("滤波时间")]
        [Description("LineDebouncerTime:I/O去抖时间 单位:us")]
        public int LineDebouncerTime
        {
            get => _lineDebouncerTime;
            set
            {
                if (_lineDebouncerTime == value) return;
                _lineDebouncerTime = value;
                OnPropertyChanged(nameof(LineDebouncerTime));
            }
        }

        // 其他方法保持原有逻辑
        public Action<DateTime, CameraBase, Mat> OnHImageOutput { get; set; }

        public virtual bool CameraConnect() { return false; }

        public virtual bool CameraDisConnect() { return false; }

        public virtual void SetExposure(int exposureTime, string cameraName) { }

        public virtual void SetGain(int gain, string cameraName) { }

        internal virtual void SetAcquisitionMode(int mode) { }

        internal virtual void SetAcqRegion(int offsetV, int offsetH, int imageH, int imageW, string cameraName) { }
    }
}