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<AxisY> CreateAxisYEventTime(StationEventJson stationEventJson, ViewXY chartV, List<StationModel> stationModels)
        {
            for (int i = 0; i < stationEventJson.phases.Count; i++)
            {
                phases ph= stationEventJson.phases[i];
                var index=  stationModels.FindIndex(a =>ph.id.Contains(a.Name));
                if(index == -1)
                {
                    continue;
                }
                int assignYAxisIndex = index * 3;
                LineCollection lineCollection = new LineCollection()
                {
                    AssignXAxisIndex = 0,
                    AssignYAxisIndex = assignYAxisIndex,
                    Tag=ph.id,    //用于排序otime,只需要排更SHZ通道
                    Lines = new SegmentLine[] { new SegmentLine(chartV.XAxes[0].DateTimeToAxisValue(ph.atime), chartV.YAxes[assignYAxisIndex].Minimum, chartV.XAxes[0].DateTimeToAxisValue(ph.atime), chartV.YAxes[assignYAxisIndex].Maximum) },
                    ShowInLegendBox = false

                };
                
                lineCollection.LineStyle.Color = System.Windows.Media.Colors.Red;
                lineCollection.Title.Text = ph.atime.ToString();
                lineCollection.Title.Visible = true;
                lineCollection.Title.Font.Size = 8;
                lineCollection.Title.Shadow.DropColor = System.Windows.Media.Colors.Transparent;
                lineCollection.Title.Shadow.ContrastColor = System.Windows.Media.Colors.Transparent;
                lineCollection.Title.AutoPlacement = false;
                lineCollection.Title.HorizontalAlign = AlignmentHorizontal.Left;
                var b= (ph.atime- stationEventJson.otime).TotalSeconds*100;
                lineCollection.Title.Offset= new PointIntXY((int)b, 0);
                 lineCollection.Title.Color = System.Windows.Media.Colors.Red;

                lineCollection.Title.AutoPlacement = false;
                chartV.LineCollections.Add(lineCollection);
            }

            for (int i = 0; i < stationEventJson.amps.Count; i++)
            {
                amps am = stationEventJson.amps[i];
                var index = stationModels.FindIndex(a => am.id.Contains(a.Name));
                if (index == -1)
                {
                    continue;
                }
                int assignYAxisIndex = index * 3;
                assignYAxisIndex = am.id.Contains("SHE") ? assignYAxisIndex + 1 : assignYAxisIndex + 2;
                LineCollection lineCollection = new LineCollection()
                {
                    AssignXAxisIndex = 0,
                    AssignYAxisIndex = assignYAxisIndex,
                    Lines = new SegmentLine[] { new SegmentLine(chartV.XAxes[0].DateTimeToAxisValue(am.atime), chartV.YAxes[assignYAxisIndex].Minimum, chartV.XAxes[0].DateTimeToAxisValue(am.atime), chartV.YAxes[assignYAxisIndex].Maximum) },
                    ShowInLegendBox = false
                };
                lineCollection.LineStyle.Color = System.Windows.Media.Colors.Red;
                lineCollection.Title.Text = am.atime.ToString();
                lineCollection.Title.Visible = true;
                lineCollection.Title.Font.Size = 8;
                lineCollection.Title.Shadow.DropColor = System.Windows.Media.Colors.Transparent;
                lineCollection.Title.Shadow.ContrastColor = System.Windows.Media.Colors.Transparent;
                lineCollection.Title.AutoPlacement = false;
                lineCollection.Title.HorizontalAlign = AlignmentHorizontal.Left;
                var b = (am.atime - stationEventJson.otime).TotalSeconds * 100;
                lineCollection.Title.Offset = new PointIntXY((int)b, 0);
                lineCollection.Title.Color = System.Windows.Media.Colors.Red;
                // lineCollection.Title.AllowDragging = false;
                chartV.LineCollections.Add(lineCollection);
            }
            return new List<AxisY>();
        }

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

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