using Arction.Wpf.ChartingMVVM.Axes;
using Arction.Wpf.ChartingMVVM.SeriesXY;
using Arction.Wpf.ChartingMVVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;


namespace mseedChart.MainModule.Models
{
    public class WavesModel
    {
        // Constants
        double YMin = 30000;       // Minimal y-value.
        double YMax = 35000;        // Maximal y-value.
        public AxisX GetAxisX(int XMax)
        {
            AxisX axisX = new AxisX();
            axisX.ScrollPosition = 0;
            axisX.ScrollMode = XAxisScrollMode.Stepping;
            axisX.Maximum = XMax;
            axisX.SweepingGap = 1;
            //axisX.AxisColor = Colors.Blue;
            //axisX.LabelsColor = Colors.Black;
            //axisX.GridStripColor = Colors.Black;
            //axisX.
            axisX.Title.Visible = false;
            return axisX;
        }
        public List<AxisY> CreateYAxisPingPu(List<StationModel> stationModels, LightningChart curChart)
        {
            List<AxisY> axisYList = new List<AxisY>();

            foreach (var item in stationModels)
            {
                AxisY axisYZ = new AxisY(curChart.ViewXY);
                //添加Z分量波形
                if (item.dz.Count > 0)
                {
                    axisYZ.SetRange(item.dz.Min(), item.dz.Max());
                }
                axisYZ.AllowAutoYFit = false;
                axisYZ.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYZ.Title.Text = string.Format("Ch{0}.Z", item.Name);
                axisYZ.Title.Angle = 0;
                //axisYZ.Title.Color = Colors.Black;
                //axisYZ.Title.
                axisYZ.Units.Visible = false;
                axisYList.Add(axisYZ);

                //添加N分量波形
                AxisY axisYN = new AxisY(curChart.ViewXY);
                if (item.dn.Count > 0)
                {                   
                    axisYN.SetRange(item.dn.Min(), item.dn.Max());                    
                }
                axisYN.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYN.Title.Text = string.Format("Ch{0}.N", item.Name);
                axisYN.Title.Angle = 0;
                //axisYN.Title.Color = Colors.Black;
                axisYN.Units.Visible = false;
                axisYList.Add(axisYN);

                //添加E分量波形
                AxisY axisYE = new AxisY(curChart.ViewXY);
                if (item.de.Count > 0)
                {
                    
                    axisYE.SetRange(item.de.Min(), item.de.Max());
                    
                }
                axisYE.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYE.Title.Text = string.Format("Ch{0}.E", item.Name);
                axisYE.Title.Angle = 0;
                //axisYE.Title.Color = Colors.Black;
                axisYE.Units.Visible = false;
                axisYList.Add(axisYE);
            }

            return axisYList;
        }
        public List<SampleDataSeries> GetSampleDataSeriesPingPu(List<StationModel> stationModels, LightningChart curChart, AxisX xAxes, AxisYCollection axisY, double samplingFrequency)
        {
            List<SampleDataSeries> sampleList = new List<SampleDataSeries>();

            for (int i = 0; i < stationModels.Count; i++)
            {
                //Bug，在波形数据少于台站数量时，出现错误
                if (i < axisY.Count)
                {

                }
                //Z
                SampleDataSeries seriesZ = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3]);
                seriesZ.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesZ.LineStyle.Color = System.Windows.Media.Colors.Blue;

                seriesZ.SamplingFrequency = samplingFrequency;
                seriesZ.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesZ.LineStyle.Width = 1;
                seriesZ.LineStyle.AntiAliasing = LineAntialias.None;
                seriesZ.ScrollModePointsKeepLevel = 1;
                seriesZ.ScrollingStabilizing = true;
                seriesZ.AllowUserInteraction = false;

                //Set custom colored palette
                System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(seriesZ.LineStyle.Color, System.Windows.Media.Colors.Gray, 50);
                System.Windows.Media.Color colorCenter = seriesZ.LineStyle.Color;

                seriesZ.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, -100));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorCenter, 0));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesZ);

                //E
                SampleDataSeries seriesE = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3 + 1]);
                seriesE.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesE.LineStyle.Color = System.Windows.Media.Colors.Red;

                seriesE.SamplingFrequency = samplingFrequency;
                seriesE.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesE.LineStyle.Width = 1;
                seriesE.LineStyle.AntiAliasing = LineAntialias.None;
                seriesE.ScrollModePointsKeepLevel = 1;
                seriesE.ScrollingStabilizing = true;
                seriesE.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                //seriesE.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                //seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, -100));
                //seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorCenter, 0));
                //seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesE);

                //N
                SampleDataSeries seriesN = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3 + 2]);
                seriesN.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesN.LineStyle.Color = System.Windows.Media.Colors.Green;

                seriesN.SamplingFrequency = samplingFrequency;
                seriesN.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesN.LineStyle.Width = 1;
                seriesN.LineStyle.AntiAliasing = LineAntialias.None;
                seriesN.ScrollModePointsKeepLevel = 1;
                seriesN.ScrollingStabilizing = true;
                seriesN.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                //seriesN.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                //seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, -100));
                //seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorCenter, 0));
                //seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesN);
            }
            return sampleList;
        }

        public List<AxisY> CreateYAxisPingPu(Dictionary<string, StationModel> sms, LightningChart curChart)
        //public List<AxisY> CreateYAxisPingPu(int StationCount, LightningChart curChart)
        {
            List<AxisY> axisYList = new List<AxisY>();

            foreach (var item in sms.Keys)
            {
                //add Z
                AxisY axisYZ = new AxisY(curChart.ViewXY);
                axisYZ.SetRange(YMin, YMax);
                axisYZ.AllowAutoYFit = false;
                axisYZ.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYZ.Title.Text = string.Format("Ch{0}.Z", item);
                axisYZ.Title.Angle = 0;
                //axisYZ.Title.
                axisYZ.Units.Visible = false;
                axisYList.Add(axisYZ);

                //add E
                AxisY axisYE = new AxisY(curChart.ViewXY);

                axisYE.SetRange(YMin, YMax);
                axisYE.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYE.Title.Text = string.Format("Ch{0}.E", item);
                axisYE.Title.Angle = 0;
                axisYE.Units.Visible = false;
                axisYList.Add(axisYE);

                //add N
                AxisY axisYN = new AxisY(curChart.ViewXY);

                axisYN.SetRange(YMin, YMax);
                axisYN.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisYN.Title.Text = string.Format("Ch{0}.N", item);
                axisYN.Title.Angle = 0;
                axisYN.Units.Visible = false;
                axisYList.Add(axisYN);
            }
            //for (int i = 0; i < sms.Count; i++)
            //{

            //}
            return axisYList;
        }
        public List<SampleDataSeries> GetSampleDataSeriesPingPu(Dictionary<string, StationModel> sms, LightningChart curChart, AxisX xAxes, AxisYCollection axisY, double samplingFrequency)
        {
            List<SampleDataSeries> sampleList = new List<SampleDataSeries>();

            for (int i = 0; i < sms.Count; i++)
            {
                //Z
                SampleDataSeries seriesZ = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3]);
                seriesZ.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesZ.LineStyle.Color = System.Windows.Media.Colors.Blue;

                seriesZ.SamplingFrequency = samplingFrequency;
                seriesZ.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesZ.LineStyle.Width = 1;
                seriesZ.LineStyle.AntiAliasing = LineAntialias.None;
                seriesZ.ScrollModePointsKeepLevel = 1;
                seriesZ.ScrollingStabilizing = true;
                seriesZ.AllowUserInteraction = false;

                //Set custom colored palette
                System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(seriesZ.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                System.Windows.Media.Color colorCenter = seriesZ.LineStyle.Color;

                seriesZ.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, -100));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorCenter, 0));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesZ);

                //E
                SampleDataSeries seriesE = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3 + 1]);
                seriesE.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesE.LineStyle.Color = System.Windows.Media.Colors.Red;

                seriesE.SamplingFrequency = samplingFrequency;
                seriesE.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesE.LineStyle.Width = 1;
                seriesE.LineStyle.AntiAliasing = LineAntialias.None;
                seriesE.ScrollModePointsKeepLevel = 1;
                seriesE.ScrollingStabilizing = true;
                seriesE.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                seriesE.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, -100));
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorCenter, 0));
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesE);

                //N
                SampleDataSeries seriesN = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i * 3 + 2]);
                seriesN.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesN.LineStyle.Color = System.Windows.Media.Colors.Green;

                seriesN.SamplingFrequency = samplingFrequency;
                seriesN.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesN.LineStyle.Width = 1;
                seriesN.LineStyle.AntiAliasing = LineAntialias.None;
                seriesN.ScrollModePointsKeepLevel = 1;
                seriesN.ScrollingStabilizing = true;
                seriesN.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                seriesN.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, -100));
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorCenter, 0));
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesN);
            }
            return sampleList;
        }
        public List<SampleDataSeries> GetSampleDataSeriesDieJia(List<StationModel> sms, LightningChart curChart, AxisX xAxes, AxisYCollection axisY, double samplingFrequency)
        {
            List<SampleDataSeries> sampleList = new List<SampleDataSeries>();

            for (int i = 0; i < sms.Count; i++)
            {
                //Z
                SampleDataSeries seriesZ = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i]);
                seriesZ.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesZ.LineStyle.Color = System.Windows.Media.Colors.Blue;

                seriesZ.SamplingFrequency = samplingFrequency;
                seriesZ.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesZ.LineStyle.Width = 1;
                seriesZ.LineStyle.AntiAliasing = LineAntialias.None;
                seriesZ.ScrollModePointsKeepLevel = 1;
                seriesZ.ScrollingStabilizing = true;
                seriesZ.AllowUserInteraction = false;

                //Set custom colored palette
                System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(seriesZ.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                System.Windows.Media.Color colorCenter = seriesZ.LineStyle.Color;

                seriesZ.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, -100));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorCenter, 0));
                seriesZ.ValueRangePalette.Steps.Add(new PaletteStep(seriesZ.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesZ);

                //E
                SampleDataSeries seriesE = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i]);
                seriesE.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesE.LineStyle.Color = System.Windows.Media.Colors.Red;

                seriesE.SamplingFrequency = samplingFrequency;
                seriesE.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesE.LineStyle.Width = 1;
                seriesE.LineStyle.AntiAliasing = LineAntialias.None;
                seriesE.ScrollModePointsKeepLevel = 1;
                seriesE.ScrollingStabilizing = true;
                seriesE.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                seriesE.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, -100));
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorCenter, 0));
                seriesE.ValueRangePalette.Steps.Add(new PaletteStep(seriesE.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesE);

                //N
                SampleDataSeries seriesN = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i]);
                seriesN.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                seriesN.LineStyle.Color = System.Windows.Media.Colors.Green;

                seriesN.SamplingFrequency = samplingFrequency;
                seriesN.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                seriesN.LineStyle.Width = 1;
                seriesN.LineStyle.AntiAliasing = LineAntialias.None;
                seriesN.ScrollModePointsKeepLevel = 1;
                seriesN.ScrollingStabilizing = true;
                seriesN.AllowUserInteraction = false;

                //Set custom colored palette
                //System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                //System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                seriesN.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, -100));
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorCenter, 0));
                seriesN.ValueRangePalette.Steps.Add(new PaletteStep(seriesN.ValueRangePalette, colorEdge, 100));

                sampleList.Add(seriesN);
            }
            return sampleList;
        }
        public List<AxisY> CreateYAxis(int channelCount, LightningChart curChart)
        {
            List<AxisY> axisYList = new List<AxisY>();

            for (int i = 0; i < channelCount; i++)
            {
                AxisY axisY = new AxisY(curChart.ViewXY);
                //AxisY axisY = new AxisY();
                axisY.SetRange(YMin, YMax);
                axisY.Title.Font = new WpfFont("Segoe UI", 10, false, false);
                axisY.Title.Text = string.Format("Ch {0}", i + 1);
                axisY.Title.Angle = 0;
                axisY.Units.Visible = false;
                axisYList.Add(axisY);
            }
            return axisYList;
        }
        public List<SampleDataSeries> GetSampleDataSeries(int channelCount, LightningChart curChart, AxisX xAxes, AxisYCollection axisY, double samplingFrequency)
        {
            List<SampleDataSeries> sampleList = new List<SampleDataSeries>();

            for (int i = 0; i < channelCount; i++)
            {
                SampleDataSeries series = new SampleDataSeries(curChart.ViewXY, xAxes, axisY[i]);
                series.SampleFormat = SampleFormat.DoubleFloat;
                //series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                series.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWpf[i % DefaultColors.SeriesForBlackBackgroundWpf.Length];

                series.SamplingFrequency = samplingFrequency;
                series.FirstSampleTimeStamp = 1.0 / samplingFrequency;
                series.LineStyle.Width = 1;
                series.LineStyle.AntiAliasing = LineAntialias.None;
                series.ScrollModePointsKeepLevel = 1;
                series.ScrollingStabilizing = true;
                series.AllowUserInteraction = false;

                //Set custom colored palette
                System.Windows.Media.Color colorEdge = ChartTools.CalcGradient(series.LineStyle.Color, System.Windows.Media.Colors.Black, 50);
                System.Windows.Media.Color colorCenter = series.LineStyle.Color;

                series.ValueRangePalette.Steps.Clear(); //Remove existing palette steps
                series.ValueRangePalette.Steps.Add(new PaletteStep(series.ValueRangePalette, colorEdge, -100));
                series.ValueRangePalette.Steps.Add(new PaletteStep(series.ValueRangePalette, colorCenter, 0));
                series.ValueRangePalette.Steps.Add(new PaletteStep(series.ValueRangePalette, colorEdge, 100));

                sampleList.Add(series);
            }
            return sampleList;
        }
    }
}
