using FLY.FilmCasting.AutoDie.Server.Model;
using GalaSoft.MvvmLight.Command;
using LiveCharts;
using LiveCharts.Configurations;
using Misc;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace FLY.FilmCasting.AutoDie.UI.Client
{
    public class PgHeatAnalyseVmUt : PgHeatAnalyseVm
    {
        public PgHeatAnalyseVmUt()
        {
            SimuData();
        }
        void SimuData()
        {

            double target = 150;
            double tolerance = target * 0.02;
            int nbolts = 88;
            double[] datas = new double[nbolts];
            Random random = new Random();
            for (int i = 0; i < nbolts; i++)
            {
                datas[i] = (Math.Sin(i * Math.PI / nbolts) * 3) * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
            }

            int kp = 3;



            double[] heats = new double[nbolts];
            for (int i = 0; i < nbolts; i++)
            {
                heats[i] = ((Math.Sin(i * Math.PI / nbolts)) * 20 + 30 + (random.NextDouble() - 0.5) * 5);
            }
            //对中！！！
            double avg = heats.Average();
            for (int i = 0; i < nbolts; i++)
            {
                heats[i] -= avg;
            }

            XMax = nbolts + 1;
            avg = datas.AverageNoNull();

            double[] thickPercents = new double[datas.Count()];

            for (int i = 0; i < datas.Count(); i++)
            {
                var data = datas[i];
                var percent = 0.0;
                if (!double.IsNaN(data))
                {
                    percent = (data - avg) / avg * 100 * Kp;
                }
                thickPercents[i] = percent;
            }
            ThickPercentDiffs.Clear();
            ThickPercentDiffs.AddRange(thickPercents);

            Kp = kp;

            HeatDiffs.Clear();
            HeatDiffs.AddRange(heats);

            //滤波后


            BoltMap.Add(new BoltMapCell() { OldNo = 20, NewNo = 20 });
            BoltMap.Add(new BoltMapCell() { OldNo = 30, NewNo = 40 });

            BoltMap.Add(new BoltMapCell() { OldNo = 60, NewNo = 60 });


            HeatEffectCurve = new int[] { 3, 4, 6, 4, 3 };
            var heatEffects = FLY.FilmCasting.AutoDie.Common.MyMath.Filter(heats, HeatEffectCurve.Select(d => (double)d));
            HeatDiffEffects.AddRange(heatEffects);
        }
    }
}
