using Arction.Wpf.Charting;
using Arction.Wpf.Charting.Axes;
using Arction.Wpf.Charting.SeriesXY;
using Arction.Wpf.Charting.Views.ViewXY;
using mseedChart.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


namespace mseedChart.MainModule.Models
{
    public class WavesModel
    {
        public List<AxisY> CreateYAxisChart(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.Title.Text = string.Format("Ch{0}.Z", item.Name);
                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.Text = string.Format("Ch{0}.N", item.Name);
                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.Text = string.Format("Ch{0}.E", item.Name);
                axisYList.Add(axisYE);

            }
            return axisYList;
        }
        public List<SampleDataSeries> GetSampleDataSeriesPingPu(List<StationModel> stationModels, LightningChart curChart, AxisX xAxes, List<AxisY> 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.ShowInLegendBox = true;
                seriesZ.Title.Text = axisY[i * 3].Title.Text;
                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 = 0.5;
                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.ShowInLegendBox = true;
                seriesE.Title.Text = axisY[i * 3+1].Title.Text;
                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 = 0.5;
                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.ShowInLegendBox = true;
                seriesN.Title.Text = axisY[i * 3 + 2].Title.Text;
                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 = 0.5;
                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> CreateAxisYEventTime(MmEvent mmEvent, ViewXY chartV)
        {
            for (int i = 0; i< chartV.YAxes.Count; i++)
            {
                // lineCollection.Lines[i] = new SegmentLine(1+i, chartV.YAxes[0].Minimum, 1+i, chartV.YAxes[0].Maximum);
                LineCollection lineCollection = new LineCollection()
                {
                    AssignXAxisIndex = 0,
                    AssignYAxisIndex = i,
                    Lines = new SegmentLine[] { new SegmentLine(0.5 * i, chartV.YAxes[i].Minimum, 0.5 * i, chartV.YAxes[i].Maximum) },
                    ShowInLegendBox = false
                };
                PointLineSeries pointLine = new PointLineSeries()
                {
                    PointsVisible = true,
                    AssignXAxisIndex = 0,
                   AssignYAxisIndex = i,
                    ShowInLegendBox = false,
                    Points = new SeriesPoint[] { new SeriesPoint(0.5 * i, (chartV.YAxes[i].Minimum+ chartV.YAxes[i].Maximum)/2) },
               
                };
                // var value=  chartV.YAxes[i].ValueToCoord(chartV.YAxes[i].Minimum);
                chartV.PointLineSeries.Add(pointLine);
                lineCollection.LineStyle.Color= System.Windows.Media.Colors.Red;
               chartV.LineCollections.Add(lineCollection);
            }         
            return new List<AxisY>();
        }

        public JsonST mseedJsonResolve(string jsonFile)
        {
            var str = File.ReadAllText(jsonFile);
            var st = JsonConvert.DeserializeObject<JsonST[]>(str);

            JsonST jsonST = st[0];
            return jsonST;
        }
    }
}
