using MeterVision.db;
using MeterVision.Util;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace MeterVision.model
{
    public class SingleDetailItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // 触发属性变更通知
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private int _index;
        public int Index
        {
            get => _index;
            set
            {
                if (_index != value)
                {
                    _index = value;
                    OnPropertyChanged(nameof(Index));
                }
            }
        }

        private string _singleDetailId;
        public string SingleDetailId
        {
            get => _singleDetailId;
            set
            {
                if (_singleDetailId != value)
                {
                    _singleDetailId = value;
                    OnPropertyChanged(nameof(SingleDetailId));
                }
            }
        }

        private string _createTime;
        public string CreateTime
        {
            get => _createTime;
            set
            {
                if (_createTime != value)
                {
                    _createTime = value;
                    OnPropertyChanged(nameof(CreateTime));
                }
            }
        }

        private string _srcImage;
        public string SrcImage
        {
            get => _srcImage;
            set
            {
                if (_srcImage != value)
                {
                    _srcImage = value;
                    OnPropertyChanged(nameof(SrcImage));
                }
            }
        }

        private int _runFlag;
        public int RunFlag
        {
            get => _runFlag;
            set
            {
                if (_runFlag != value)
                {
                    _runFlag = value;
                    OnPropertyChanged(nameof(RunFlag));
                    OnPropertyChanged(nameof(ResultVisiable));  // 依赖 RunFlag 更新 ResultVisiable
                }
            }
        }

        private string _runTime;
        public string RunTime
        {
            get => _runTime;
            set
            {
                if (_runTime != value)
                {
                    _runTime = value;
                    OnPropertyChanged(nameof(RunTime));
                    OnPropertyChanged(nameof(RunTime_date)); // 依赖 RunTime 更新 RunTIme_date
                    OnPropertyChanged(nameof(RunTime_time)); // 依赖 RunTime 更新 RunTIme_time
                }
            }
        }

        public string RunTime_date => ThisApp.ConvertDateFormat_Date(RunTime);

        public string RunTime_time => ThisApp.ConvertDateFormat_Time(RunTime);

        private string _dstIamge;
        public string DstImage
        {
            get => _dstIamge;
            set
            {
                if (_dstIamge != value)
                {
                    _dstIamge = value;
                    OnPropertyChanged(nameof(DstImage));
                }
            }
        }

        private int _meterType;
        public int MeterType
        {
            get => _meterType;
            set
            {
                if (_meterType != value)
                {
                    _meterType = value;
                    OnPropertyChanged(nameof(MeterType));
                    OnPropertyChanged(nameof(MeterTypeName)); // 依赖 MeterType 更新 MeterTypeName
                    OnPropertyChanged(nameof(FeatureRegionName));
                    OnPropertyChanged(nameof(MeterRegionName));
                }
            }
        }

        public string MeterTypeName => Get_MeterTypeName();

        private int _digitCount;
        public int DigitCount
        {
            get => _digitCount;
            set
            {
                if (_digitCount != value)
                {
                    _digitCount = value;
                    OnPropertyChanged(nameof(DigitCount));
                    OnPropertyChanged(nameof(NumCountName)); // 依赖 DigitCount 更新 NumCountName
                }
            }
        }

        private int _pointerCount;
        public int PointerCount
        {
            get => _pointerCount;
            set
            {
                if (_pointerCount != value)
                {
                    _pointerCount = value;
                    OnPropertyChanged(nameof(PointerCount));
                    OnPropertyChanged(nameof(NumCountName)); // 依赖 PointerCount 更新 NumCountName
                }
            }
        }

        public string NumCountName => Get_NumCountName();

        private double _lastUnit;
        public double LastUnit
        {
            get => _lastUnit;
            set
            {
                if (_lastUnit != value)
                {
                    _lastUnit = value;
                    OnPropertyChanged(nameof(LastUnit));
                    OnPropertyChanged(nameof(LastUnitName)); // 依赖 LastUnit 更新 LastUnitName
                }
            }
        }

        public string LastUnitName
        {
            get
            {
                if(MeterType > 0)
                {
                    return $"尾数: {LastUnit}m³";
                }
                else
                {
                    return "";
                }
            }
        }

        private int _resultType;
        public int ResultType
        {
            get => _resultType;
            set
            {
                if (_resultType != value)
                {
                    _resultType = value;
                    OnPropertyChanged(nameof(ResultType));
                    OnPropertyChanged(nameof(ResultTypeColor)); // 依赖 ResultType 更新 ResultTypeColor
                }
            }
        }

        public string ResultTypeColor => Get_ResultTypeColor();

        private long _rawValue;
        public long RawValue
        {
            get => _rawValue;
            set
            {
                if (_rawValue != value)
                {
                    _rawValue = value;
                    if (MeterType == 0)
                    {
                        _rawValue = -1;
                    }
                    OnPropertyChanged(nameof(RawValue));
                }
            }
        }

        private long _finalValue;
        public long FinalValue
        {
            get => _finalValue;
            set
            {
                if (_finalValue != value)
                {
                    _finalValue = value;
                    //if(MeterType == 0)
                    //{
                    //    _finalValue = "";
                    //}
                    OnPropertyChanged(nameof(FinalValue));
                }
            }
        }
        private long _completeValue;
        public long CompleteValue
        {
            get => _completeValue;
            set
            {
                if(_completeValue != value)
                {
                    _completeValue = value;
                    OnPropertyChanged(nameof(CompleteValue));
                }
            }
        }

        //AI识别出的水表类型
        private int _resultMeter;
        public int ResultMeter
        {
            get => _resultMeter;
            set
            {
                if (_resultMeter != value)
                {
                    _resultMeter = value;
                    OnPropertyChanged(nameof(ResultMeter));
                }
            }
        }

        private string _aiVer;
        public string AiVer
        {
            get => _aiVer;
            set
            {
                if (_aiVer != value)
                {
                    _aiVer = value;
                    OnPropertyChanged(nameof(AiVer));
                }
            }
        }

        private string _debugInfo;
        public string DebugInfo
        {
            get => _debugInfo;
            set
            {
                if (_debugInfo != value)
                {
                    _debugInfo = value;
                    OnPropertyChanged(nameof(DebugInfo));
                    OnPropertyChanged(nameof(AI_result_array)); // 依赖 DebugInfo 更新 AI_result_array
                }
            }
        }

        public string[] AI_result_array => Get_DebugInfoArray();

        private string _logPath;
        public string LogPath
        {
            get => _logPath;
            set
            {
                if (_logPath != value)
                {
                    _logPath = value;
                    OnPropertyChanged(nameof(LogPath));
                }
            }
        }

        private string _memo;
        public string Memo
        {
            get => _memo;
            set
            {
                if (_memo != value)
                {
                    _memo = value;
                    OnPropertyChanged(nameof(Memo));
                }
            }
        }

        private double _brightVal;
        public double BrightVal
        {
            get => _brightVal;
            set
            {
                if(_brightVal != value)
                {
                    _brightVal = value;
                    OnPropertyChanged(nameof(BrightVal));
                    OnPropertyChanged(nameof(BrightValName));
                }
            }
        }

        public string BrightValName => $"亮度: {BrightVal}";

        private int _flowRate;
        public int FlowRate
        {
            get => _flowRate;
            set
            {
                if(_flowRate != value)
                {
                    _flowRate = value;
                    OnPropertyChanged(nameof(FlowRate));
                    OnPropertyChanged(nameof(FlowRateName));
                }
            }
        }

        public string FlowRateName
        {
            get => $"最大流量: {FlowRate}";
        }

        private string _meterRegion;
        public string MeterRegion
        {
            get => _meterRegion;
            set
            {
                if(_meterRegion != value)
                {
                    _meterRegion = value;
                    OnPropertyChanged(nameof(MeterRegion));
                    OnPropertyChanged(nameof(MeterRegionName));
                }
            }
        }

        public string MeterRegionName
        {
            get
            {
                if(MeterType == 1 || MeterType == 2 || MeterType == 3)
                {
                    return $"表盘: {MeterRegion}";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        private string _featureRegion;
        public string FeatureRegion
        {
            get => _featureRegion;
            set
            {
                if(_featureRegion != value)
                {
                    _featureRegion = value;
                    OnPropertyChanged(nameof(FeatureRegion));
                    OnPropertyChanged(nameof(FeatureRegionName));
                }
            }
        }

        public string FeatureRegionName
        {
            get
            {
                if(MeterType == 1 || MeterType == 3)
                {
                    return $"数字区域: {FeatureRegion}";
                }
                else if(MeterType == 2)
                {
                    return $"首位指针: {FeatureRegion}";
                }
                else
                {
                    return string.Empty;
                }
            }
        }


        private double _lastValue;
        public double LastValue
        {
            get => _lastValue;
            set
            {
                if(_lastValue != value)
                {
                    _lastValue = value;
                    OnPropertyChanged(nameof(LastValue));
                    OnPropertyChanged(nameof(LastValueName));
                }
            }
        }

        public string LastValueName => $"初值: {LastValue}";


        private string _lastTime;
        public string LastTime
        {
            get => _lastTime;
            set
            {
                if(_lastTime != value)
                {
                    _lastTime = value;
                    OnPropertyChanged(nameof(LastTime));
                    OnPropertyChanged(nameof(LastTimeName1));
                    OnPropertyChanged(nameof(LastTimeName2));
                }
            }
        }

        public string LastTimeName1 => ThisApp.ConvertDateFormat_Date(LastTime);
        public string LastTimeName2 => ThisApp.ConvertDateFormat_Time(LastTime);



        private string _deviceSn;
        public string DeviceSn
        {
            get => _deviceSn;
            set
            {
                if(_deviceSn != value)
                {
                    _deviceSn = value;
                    OnPropertyChanged(nameof(DeviceSn));
                }
            }
        }

        private string _sampleTime;
        public string SampleTime
        {
            get => _sampleTime;
            set
            {
                if(_sampleTime != value)
                {
                    _sampleTime = value;
                    OnPropertyChanged(nameof(SampleTime));
                }
            }
        }


        public Visibility ResultVisiable => (RunFlag == 1) ? Visibility.Visible : Visibility.Hidden;

        public SingleDetailItem() { }

        public SingleDetailItem(TSingleDetail singleDetail)
        {
            ObjectHelper.CopyMatchingFields(singleDetail, this);
        }

        private string Get_MeterTypeName()
        {
            switch (MeterType)
            {
                case 1:
                    return "数字+指针";
                case 2:
                    return "全指针";
                case 3:
                    return "全数字";
                case 4:
                    return "LED表";
                case 5:
                    return "压力表";
                default:
                    //return "";
                    return "非水表";
            }
        }


        private string Get_NumCountName()
        {
            switch (MeterType)
            {
                case 1:
                    return $"数字: {DigitCount} 指针: {PointerCount}";
                case 2:
                    return $"指针: {PointerCount}";
                case 3:
                    return $"数字: {DigitCount}";
                default:
                    return string.Empty;
            }
        }


        private string Get_ResultTypeColor()
        {
            switch (ResultType)
            {
                case 1:
                case 2:
                case 7:
                case 8:
                    return "#67c23a"; // 绿色
                default:
                    return "#e6a23c"; // 橙色
            }
        }
        private string[] Get_DebugInfoArray()
        {
            string[] sInfos = DebugInfo.Split(',');
            if (sInfos.Length != 24)
            {
                sInfos = Enumerable.Range(0, 24).Select(_ => "").ToArray();
            }

            return sInfos.ToArray();
            //return sInfos.Select(s => s == "88" ? "" : s).ToArray();
        }


    //    complete_value = @CompleteValue,
    //    result_meter = @ResultMeter,
    //   new SQLiteParameter("@CompleteValue", detail.CompleteValue),
    //   new SQLiteParameter("@ResultMeter", detail.ResultMeter),

    //CompleteValue = reader.GetString(reader.GetOrdinal("complete_value")),
    //ResultMeter = reader.GetInt32(reader.GetOrdinal("result_meter")),

    //DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
    //SampleTime = reader.GetString(reader.GetOrdinal("sample_time")),

        //-------------------------------------------------
    }
}
