using NEIntelligentControl2.Models.Datas;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NEIntelligentControl2.Models.AGC
{
    /// <summary>
    /// AGC曲线模型
    /// </summary>
    public class ViewModel : PlotModel
    {
        private List<TsData> _ActPower; // 实发有功数据
        public List<TsData> ActPower { get => _ActPower; }
        private List<TsData> _PowerSet;// 有功设定数据
        public List<TsData> PowerSet { get => _PowerSet; }


        private LineSeries ls1;
        private LineSeries ls2;

        public DateTimeAxis AxisX;

        public ViewModel()
        {
            this.PlotAreaBorderThickness = new OxyThickness(0);
            this.Legends.Add(new OxyPlot.Legends.Legend()
            {
                Key = "power",
                LegendPosition = OxyPlot.Legends.LegendPosition.BottomCenter,
                LegendPlacement = OxyPlot.Legends.LegendPlacement.Outside,
                LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal,
                LegendTextColor = OxyColor.Parse("#FF000000"),
                //LegendFontSize = 10
            });

            var dta2 = new LinearAxis() { Position = AxisPosition.Left };
            dta2.MinorTicklineColor = OxyColors.Transparent;
            dta2.AxislineColor = OxyColors.Transparent;
            dta2.TickStyle = TickStyle.None;
            //dta2.MajorStep = 20;
            dta2.IntervalLength = 20;
            dta2.IsZoomEnabled = false;
            dta2.IsPanEnabled = false;
            dta2.TextColor = OxyColor.Parse("#FF000000");
            this.Axes.Add(dta2);

            AxisX = new DateTimeAxis { Position = AxisPosition.Bottom, StringFormat = "HH:mm" };
            AxisX.MinorTicklineColor = OxyColors.Transparent;
            AxisX.TickStyle = TickStyle.None;
            AxisX.IsZoomEnabled = false;
            AxisX.IsPanEnabled = false;
            //_AxisX.IntervalLength = 45;
            AxisX.TextColor = OxyColor.Parse("#FF000000");

            this.Axes.Add(AxisX);


            ls1 = new LineSeries() { Title = "实发有功", Color = OxyColor.Parse("#FFF4A460"), StrokeThickness = 1.5 };
            ls1.LegendKey = "power";
            ls2 = new LineSeries() { Title = "有功设定", Color = OxyColor.Parse("#FF31B9FB"), StrokeThickness = 1.5 };
            ls2.LegendKey = "power";
            Series.Add(ls1);
            Series.Add(ls2);
        }

        /// <summary>
        /// AGC曲线模型
        /// </summary>
        /// <param name="ap">实发有功</param>
        /// <param name="ps">有功设定</param>
        public ViewModel(List<TsData> ap, List<TsData> ps) : this()
        {
            _PowerSet = ps;
            AddData(ls2, ps);
            _ActPower = ap;
            AddData(ls1, ap);
        }

        internal void SetValueNow(TagInfo arg1, List<TsData> arg2)
        {
            if (arg2 == null || arg2.Count < 2) return;
            if (arg1.Type == TagType.PowerSet)
            {
                _PowerSet = arg2;
                AddData(ls2, arg2);
            }
            else
            {
                _ActPower = arg2;
                AddData(ls1, arg2);
            }
            var dt = DateTime.Now;

            AxisX.AbsoluteMinimum = DateTimeAxis.ToDouble(dt.AddHours(-8));
            AxisX.AbsoluteMaximum = DateTimeAxis.ToDouble(dt);
        }

        internal void SetValue(TagInfo arg1, List<TsData> arg2)
        {
            if (arg2 == null || arg2.Count < 2) return;
            if (arg1.Type == TagType.PowerSet)
            {
                _PowerSet = arg2;
                AddData(ls2, arg2);
            }
            else
            {
                _ActPower = arg2;
                AddData(ls1, arg2);
            }
            var dts = arg2[0].Ts.GetLongDateTime();
            var dte = arg2[arg2.Count - 1].Ts.GetLongDateTime();

            AxisX.AbsoluteMinimum = DateTimeAxis.ToDouble(dts);
            AxisX.AbsoluteMaximum = DateTimeAxis.ToDouble(dte);
        }

        private void AddData(LineSeries ls, List<TsData> vs)
        {
            ls.Points.Clear();
            if (vs == null) return;
            foreach(var v in vs)
            {
                DataPoint dp = new DataPoint(DateTimeAxis.ToDouble(v.Ts.GetLongDateTime()), Math.Round(v.Value, 2));
                ls.Points.Add(dp);
            }
        }
    }
}
