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

namespace MeterVision.model
{
    public class PatchItem : 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));
                }
            }
        }

        public string PatchId { get; set; }
        public string CreateTime { get; set; }
        public string CreateTimeName
        {
            get => ThisApp.ConvertDateFormat(CreateTime);
        }

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

        public string PatchNameColor => GetPatchNameColor();

        public string StandId { get; set; }
        public string StandName { get; set; }

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


        private int _runCount;
        public int RunCount
        {
            get => _runCount;
            set
            {
                if(_runCount != value)
                {
                    _runCount = value;
                    OnPropertyChanged(nameof(RunCount));
                    OnPropertyChanged(nameof(RunRate));
                    OnPropertyChanged(nameof(EqualRate));
                    OnPropertyChanged(nameof(InvalidRate));
                    OnPropertyChanged(nameof(ErrorRate));
                    OnPropertyChanged(nameof(PatchState));
                    OnPropertyChanged(nameof(PatchStateColor));
                }
            }
        }


        private string GetPatchNameColor()
        {
            /*if(RunCount == DetailCount)
            {
                return "DarkGreen";
            }
            else
            {
                return "#000000";
            }*/
            return "#000000";
        }

        //已完成识别率
        public float RunRate
        {
            //get => (float)RunCount / DetailCount;   // * 100);
            get
            {
                if(DetailCount == 0)
                {
                    return 0.0f;
                }
                else
                {
                    return (float)RunCount / DetailCount;   // * 100)
                }
            }
        }

        private int _equalCount;
        public int EqualCount
        {
            get => _equalCount;
            set
            {
                if(_equalCount != value)
                {
                    _equalCount = value;
                    OnPropertyChanged(nameof(EqualRate));
                    OnPropertyChanged(nameof(EqualCount));
                }
            }
        }


        public float EqualRate
        {
            get
            {
                if(RunCount == 0)
                {
                    return 0.0f;
                }
                else
                {
                    return (float)EqualCount / RunCount;    // * 100);
                }               
            }              
        }


        private int _invalidCount;
        public int InvalidCount
        {
            get => _invalidCount;
            set
            {
                if(_invalidCount != value)
                {
                    _invalidCount = value;
                    OnPropertyChanged(nameof(InvalidCount));
                    OnPropertyChanged(nameof(InvalidRate));
                }
            }
        }

        //无效率
        public float InvalidRate
        {
            get
            {
                if(RunCount > 0)
                {
                    return (float)InvalidCount / RunCount;      
                }
                return 0.0f;
            }
        }

        public int _errorCount;
        public int ErrorCount
        {
            get => _errorCount;
            set
            {
                if(_errorCount != value)
                {
                    _errorCount = value;
                    OnPropertyChanged(nameof(ErrorCount));
                    OnPropertyChanged(nameof(ErrorRate));
                }
            }
        }
        public float ErrorRate
        {
            get
            {
                if (RunCount > 0)
                {
                    return (float)ErrorCount / RunCount;
                }
                return 0.0f;
            }
        }


        public string PatchState
        {
            get
            {
                if(RunCount == 0)
                {
                    return "🔲[未开始]";
                }
                else if(RunCount < DetailCount)
                {
                    return "⏳[进行中]";
                }
                else
                {
                    return "✔️[已完成]";
                }
            }
        }

        public string PatchStateColor
        {
            get
            {
                if (RunCount == 0)
                {
                    return "#6C757D";
                }
                else if (RunCount < DetailCount)
                {
                    return "#FFC107";
                }
                else
                {
                    return "#28A745";
                }
            }
        }

        public PatchItem()
        {
            PatchId = string.Empty;
            CreateTime = string.Empty;
            PatchName = string.Empty;
            StandId = string.Empty;
            StandName = string.Empty;
            DetailCount = 0;
            RunCount = 0;
            EqualCount = 0;
        }

        public PatchItem(VPatch vPatch)
        {
            ObjectHelper.CopyMatchingFields(vPatch, this);
        }

        /////////////////////////////////////////////////////
    }
}
