using System;
using Sicentury.Core;

namespace MECF.Framework.RT.Core.Managers.PDS
{
    public class ProcessDataStatPerRun : BindableBase
    {
        #region Variables

        private DateTime _statStart;
        private DateTime? _statEnd;


        #endregion

        #region Constructors

        public ProcessDataStatPerRun(int index, Guid uid, string module, string recipeName, string waferUid, DateTime beginTime, DateTime endTime, double h2,
            double ar,
            double pn2, double hcl, double sih4, double cxhx, double tcs, double tma, double heaterPowerConsumption) : this(module)
        {
            Index = index;
            Module = module;
            StatisticsUid = uid;
            RecipeName = recipeName;
            WaferId = waferUid;
            StatisticsStart = beginTime;
            StatisticsEnd = endTime;
            H2.Total = h2;
            Ar.Total = ar;
            PN2.Total = pn2;
            HCL.Total = hcl;
            SiH4.Total = sih4;
            C2H4.Total = cxhx;
            TCS.Total = tcs;
            TMA.Total = tma;
            HeaterPowerConsumption.Total = heaterPowerConsumption;
        }

        public ProcessDataStatPerRun(string module)
        {
            StatisticsUid = Guid.NewGuid();
            Module = module;
            WaferId = string.Empty;
            RecipeName = string.Empty;
            StatisticsStart = DateTime.Now;

            H2 = new ProcessDataStatCounter(nameof(H2), module, FilterSccmToSccs);
            Ar = new ProcessDataStatCounter(nameof(Ar), module, FilterSccmToSccs);
            PN2 = new ProcessDataStatCounter(nameof(PN2), module, FilterSccmToSccs);
            HCL = new ProcessDataStatCounter(nameof(HCL), module, FilterSccmToSccs);
            SiH4 = new ProcessDataStatCounter(nameof(SiH4), module, FilterSccmToSccs);
            C2H4 = new ProcessDataStatCounter(nameof(C2H4), module, FilterSccmToSccs);
            TCS = new ProcessDataStatCounter(nameof(TCS), module, FilterSccmToSccs);
            TMA = new ProcessDataStatCounter(nameof(TMA), module, FilterSccmToSccs);

            // 加热器瞬时功率单位为KW，需转换为KWs再进行积分
            HeaterPowerConsumption = new ProcessDataStatCounter(nameof(HeaterPowerConsumption), module,
                FilterKwhToKws, new[]
                {
                    $"{module}.PSU1.OutputPowerFeedBack",
                    $"{module}.PSU2.OutputPowerFeedBack",
                    $"{module}.PSU3.OutputPowerFeedBack",
                    $"{module}.SCR1.PowerFeedBack",
                    $"{module}.SCR2.PowerFeedBack",
                    $"{module}.SCR3.PowerFeedBack"
                });
        }
        
        public ProcessDataStatPerRun(string module, string waferId) : this(module)
        {
            WaferId = waferId;
        }

        #endregion

        #region Properties

        public int Index { get; set; }

        public string Module { get; set; }

        public string WaferId { get; }

        public string RecipeName { get; internal set; }

        public Guid StatisticsUid { get; }

        public DateTime StatisticsStart
        {
            get => _statStart;
            set => Set(ref _statStart, value);
        }

        public DateTime? StatisticsEnd
        {
            get => _statEnd;
            set => Set(ref _statEnd, value);
        }

        public ProcessDataStatCounter H2 { get; }

        public ProcessDataStatCounter Ar { get; }

        public ProcessDataStatCounter PN2 { get; }

        public ProcessDataStatCounter HCL { get; }

        public ProcessDataStatCounter SiH4 { get; }

        public ProcessDataStatCounter C2H4 { get; }

        public ProcessDataStatCounter TCS { get; }

        public ProcessDataStatCounter TMA { get; }

        public ProcessDataStatCounter HeaterPowerConsumption { get; }

        #endregion

        #region Methods

        private double FilterSccmToSccs(double sccm)
        {
            return sccm / 60.0d;
        }

        private double FilterKwhToKws(double kwh)
        {
            return kwh / 3600.0d;
        }


        /// <summary>
        /// 累加工艺数据。
        /// </summary>
        public void Accumulate()
        {
            H2.Accumulate();
            Ar.Accumulate();
            PN2.Accumulate();
            HCL.Accumulate();
            SiH4.Accumulate();
            C2H4.Accumulate();
            TCS.Accumulate();
            TMA.Accumulate();
            HeaterPowerConsumption.Accumulate();
        }

        #endregion
    }
}
