using FLY.Thick.FilmCasting.IService;
using LiveCharts;
using LiveCharts.Configurations;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    public class TrendGraphVmUt : TrendGraphVm
    {
        public TrendGraphVmUt()
        {
            #region 与数据无关界面参数


            AreaColors = new List<Brush>();
            AreaColors.AddRange(FLY.ControlLibrary.Themes.Styles.AreaColors);

            YFormatter = (y) =>
            {
                double percent = y * 100;
                string text = "";
                if (percent == 0d)
                {
                    text = "0.0%";
                }
                else if (percent > 0)
                {
                    text = $"+{Math.Abs(percent):F1}%";
                }
                else
                {
                    text = $"-{Math.Abs(percent):F1}%";
                }
                return $"{text,6}";
            };
            XFormatter = (x) =>
            {
                int index = (int)x;
                if (index < Values.Count() && index >= 0)
                    return Values[index].Time.ToString("HH:mm");
                else
                    return "";
            };

            MapperPositive = Mappers.Xy<TrendValue>()
                .X((value, index) =>
                {
                    return index;
                })
                .Y(value => value.Sigma / value.Value)
                .Fill((value) =>
                {
                    double percent = value.Sigma / value.Value;
                    if ((percent > Tolerance2YMax) || (percent < -Tolerance2YMax))
                    {
                        return AreaColors[0];
                    }
                    else if ((percent > ToleranceYMax) || (percent < -ToleranceYMax))
                    {
                        return AreaColors[1];
                    }
                    else
                    {
                        return AreaColors[2];
                    }
                });

            MapperNegative = Mappers.Xy<TrendValue>()
                .X((value, index) =>
                {
                    return index;
                })
                .Y(value => -value.Sigma / value.Value)
                .Fill((value) =>
                {
                    double percent = value.Sigma / value.Value;
                    if ((percent > Tolerance2YMax) || (percent < -Tolerance2YMax))
                    {
                        return AreaColors[0];
                    }
                    else if ((percent > ToleranceYMax) || (percent < -ToleranceYMax))
                    {
                        return AreaColors[1];
                    }
                    else
                    {
                        return AreaColors[2];
                    }
                });

            MapperValue = Mappers.Xy<TrendValue>()
                .X((value, index) =>
                {
                    return index;
                })
                .Y(value => value.Value);

            #endregion

            Values.Clear();

            #region 数据
            double target = 150;

            double tolerancePercent = 0.05;
            double yRangePercent = 3;
            int len = 200;
            DateTime startTime = DateTime.Now;
            TimeSpan tsInterval = TimeSpan.FromSeconds(30);
            DateTime time = startTime;
            int id = 312456;
            Random random = new Random();
            for (int i = 0; i < len; i++, time += tsInterval)
            {
                TrendValue tv = new TrendValue();
                tv.Time = time;
                tv.Value = target + (random.NextDouble() - 0.5) * target * tolerancePercent * 0.5;
                tv.Sigma = (Math.Sin(i * Math.PI / len) * 3) * target * tolerancePercent * random.NextDouble();

                Values.Add(tv);
            }


            #endregion

            #region 界面数据
            XMax = len;
            Mix = 2;
            BeginTime = Values.First().Time;
            EndTime = Values.Last().Time;
            Id = id;
            var sigma2Percent = Values.Select(v => 2 * v.Sigma / v.Value);
            Average = sigma2Percent.AverageNoNull();
            Max = sigma2Percent.Max();
            Min = sigma2Percent.Min();

            ToleranceYMax = tolerancePercent;
            ToleranceYMin = -ToleranceYMax;
            Tolerance2YMax = ToleranceYMax * 2;
            Tolerance2YMin = -Tolerance2YMax;
            YMax = ToleranceYMax * yRangePercent;
            YMin = -YMax;

            #endregion
        }
    }
}
