using MeterVision.db;
using MeterVision.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public static Dictionary<int, double> LastUnitMapping = new Dictionary<int, double>
        {
            {0,0.0001 },
            {1,0.001 },
            {2,0.01 },
            {3,0.1 },
            {4,1 },
            {5,10 },
            {6,100 },
            {7,1000 },
            {8,10000 }
        };

        // 触发属性变更通知
        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));
                }
            }
        }

        public string StandId { get; set; }

        public string StandDetailId { get; set; }

        public string SrcImage { get; set; }

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

        private string _stationId;
        public string StationId
        {
            get => _stationId;
            set
            {
                if (_stationId != value)
                {
                    _stationId = value;
                    OnPropertyChanged(nameof(StationId));
                    //OnPropertyChanged(nameof(StationIdColor));
                }
            }
        }

        public string StationIdColor
        {
            get
            {
                if (MeterType > 0)
                {
                    return "#FF6347";
                }
                return "black";
            }
        }


        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));
                    OnPropertyChanged(nameof(SampleTime1));
                    OnPropertyChanged(nameof(SampleTime2));
                }
            }
        }

        public string SampleTime1 => ThisApp.ConvertDateFormat_Date2(SampleTime);
        public string SampleTime2 => ThisApp.ConvertDateFormat_Time2(SampleTime);


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

        private int _eLastUnit;
        public int ELastUnit
        {
            get => _eLastUnit;
            set
            {
                if (_eLastUnit != value)
                {
                    _eLastUnit = value;
                    OnPropertyChanged(nameof(ELastUnit));
                    //OnPropertyChanged(nameof(ELastUnitName));
                }
            }
        }

        //public string ELastUnitName
        //{
        //    get
        //    {
        //        if(LastUnitMapping.TryGetValue(ELastUnit,out double lastUnit))
        //        {
        //            return lastUnit + "";
        //        }
        //        return "";
        //    }
        //}

        private int _meterType;
        public int MeterType
        {
            get => _meterType;
            set
            {
                if (_meterType != value)
                {
                    _meterType = value;
                    OnPropertyChanged(nameof(MeterType));
                    OnPropertyChanged(nameof(MeterTypeName));
                    OnPropertyChanged(nameof(LastUnitName));
                    OnPropertyChanged(nameof(FeatureRegionName));
                    OnPropertyChanged(nameof(CountName));
                    OnPropertyChanged(nameof(BrightValName));
                    OnPropertyChanged(nameof(LastValueName));
                    OnPropertyChanged(nameof(DialRegionName));
                    OnPropertyChanged(nameof(FlowRateName));
                    OnPropertyChanged(nameof(StationIdColor));
                }
            }
        }
        public string MeterTypeName => Get_MeterTypeName();


        private double _brightVal;
        public double BrightVal
        {
            get => _brightVal;
            set
            {
                if (_brightVal != value)
                {
                    _brightVal = value;
                    OnPropertyChanged(nameof(BrightVal));
                    OnPropertyChanged(nameof(BrightValName));
                }
            }
        }
        public string BrightValName
        {
            get
            {
                if(MeterType > 0)
                {
                    return $"亮度: {BrightVal}";
                }
                return "";
            }
        }


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

        public string FlowRateName
        {
            get
            {
                if(MeterType > 0)
                {
                    return $"小时流量: {FlowRate}";
                }
                return "";
            }
        }

        private string _dialRegion;
        public string DialRegion
        {
            get => _dialRegion;
            set
            {
                if (_dialRegion != value)
                {
                    _dialRegion = value;
                    OnPropertyChanged(nameof(DialRegion));
                    OnPropertyChanged(nameof(DialRegionName));
                }
            }
        }

        public string DialRegionName
        {
            get
            {
                if(MeterType > 0)
                {
                    return $"表盘: {DialRegion}";
                }
                return "";
            }
        }

        private int _numCount;
        public int NumCount
        {
            get => _numCount;
            set
            {
                if (_numCount != value)
                {
                    _numCount = value;
                    OnPropertyChanged(nameof(NumCount));
                    OnPropertyChanged(nameof(CountName));
                }
            }
        }


        private int _indCount;
        public int IndCount
        {
            get => _indCount;
            set
            {
                if (_indCount != value)
                {
                    _indCount = value;
                    OnPropertyChanged(nameof(IndCount));
                    OnPropertyChanged(nameof(CountName));
                }
            }
        }

        public string CountName
        {
            get
            {
                if (MeterType == 1)
                {
                    return "数字: " + NumCount + " ;指针: " + IndCount;
                }
                else if (MeterType == 2)
                {
                    return "指针: " + IndCount;
                }
                else if (MeterType == 3)
                {
                    return "数字: " + NumCount;
                }
                else
                {
                    return "";
                }
            }
        }

        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 "数字区域: \n" + FeatureRegion;
                }
                else if (MeterType == 2)
                {
                    return "首位指针: \n" + FeatureRegion;
                }
                else
                {
                    return "";
                }
            }
        }


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


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

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

        public string LastValueName
        {
            get
            {
                if (MeterType > 0)
                {
                    if (string.IsNullOrEmpty(LastTime))
                    {
                        return "";
                    }
                    else
                    {
                        return "初值: " + LastValue;
                    }
                }
                return "";
            }
        }


        private string _lastTime;
        public string LastTime
        {
            //get => ThisApp.ConvertDateFormat(_lastTime);
            get => _lastTime;
            set
            {
                if (_lastTime != value)
                {
                    _lastTime = value;
                    OnPropertyChanged(nameof(LastTime));
                    OnPropertyChanged(nameof(LastTimeName));
                    OnPropertyChanged(nameof(LastValueName));
                }
            }
        }

        public string LastTimeName => ThisApp.ConvertDateFormat(LastTime);


        public StandDetailItem()
        {

        }

        public StandDetailItem(TStandDetail standDetail)
        {
            //StandId = standDetail.StandId;
            //StandDetailId = standDetail.StandDetailId;
            //SrcImage = standDetail.SrcImage;
            //StandValue = standDetail.StandValue;
            //StationId = standDetail.StationId;
            //SampleTime = standDetail.SampleTime;
            //NumCount = standDetail.NumCount;
            //LastUnit = standDetail.LastUnit;
            ObjectHelper.CopyMatchingFields(standDetail, 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 "非水表";
            }
        }
        ///////////////////////////////////////////////////////////
    }
}
