using Arction.Wpf.ChartingMVVM;
using Arction.Wpf.ChartingMVVM.Axes;
using Arction.Wpf.ChartingMVVM.SeriesPolar;
using Arction.Wpf.ChartingMVVM.Views.ViewPolar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Txgy.Microseismic.BaseLib
{
    public class AmplitudeModel
    {
        public PolarSeriesPoint[] GenerateAmplitudeSeries(int pointCounter)
        {
            PolarSeriesPoint[] points = new PolarSeriesPoint[pointCounter];

            Random rand = new Random();

            double amplitude;
            for (int i = 0; i < pointCounter; i++)
            {
                amplitude = 45 + 30.0 * Math.Sin(5.0 * 2.0 * Math.PI * i / 360.0) + rand.NextDouble() * 10.0;

                if (amplitude < 0)
                    amplitude = 0;
                // points.Add(new PolarSeriesPoint(i, amplitude));
                points[i].Amplitude = amplitude;
                points[i].Angle = i;
            }

            return points;
        }

        public PolarSeriesPoint[] GenerateAmplitudeSeries(List<WaveDataModel> showWaveDataList)
        {
            PolarSeriesPoint[] points = new PolarSeriesPoint[361];
            double avrDist = showWaveDataList.Average(awd => awd.waveHeader.ToEventDist);


            double amplitude;
            for (int i = 0; i < 361; i++)
            {
                var amp = showWaveDataList.Find(swd => swd.waveHeader.ToEventAngle == i);
                if (amp != null)
                {
                    amplitude = (amp.waveHeader.MaxAmp * amp.waveHeader.ToEventDist / avrDist) * 100000;
                }
                else
                {
                    amplitude = 0;
                }

                // points.Add(new PolarSeriesPoint(i, amplitude));
                points[i].Amplitude = amplitude;
                points[i].Angle = i;
            }

            return points;
        }
        public PointLineSeriesPolarCollection GenerateData(List<WaveDataModel> showWaveDataList)
        {
            PointLineSeriesPolarCollection polarCollection1 = new PointLineSeriesPolarCollection();

            PointLineSeriesPolar lineSeriesGradient = new PointLineSeriesPolar();
            lineSeriesGradient.LineStyle.Width = 2;
            lineSeriesGradient.PointsVisible = false;
            lineSeriesGradient.Title.Visible = false;
            //lineSeriesGradient.Title.
            Comparison<WaveDataModel> MyComparison = WaveDataModelComparison;
            showWaveDataList.Sort((s1, s2) =>
            {
                if (s1.waveHeader.ToEventAngle != s2.waveHeader.ToEventAngle)
                {
                    return s2.waveHeader.ToEventAngle.CompareTo(s1.waveHeader.ToEventAngle);

                }
                else return 0;
            });
            double avrDist = showWaveDataList.Average(awd => awd.waveHeader.ToEventDist);
            //Console.WriteLine("平均距离="+avrDist);
            //showWaveDataList.Sort(swd=>swd.waveHeader.)
            lineSeriesGradient.Points = new PolarSeriesPoint[showWaveDataList.Count];
            for (int i = 0; i < showWaveDataList.Count; i++)
            {
                //double fx = showWaveDataList[i].waveHeader.MaxAmp * (showWaveDataList[i].waveHeader.ToEventDist / avrDist);
                double fx = CalValue(showWaveDataList[i], avrDist);
                lineSeriesGradient.Points[i] = new PolarSeriesPoint(showWaveDataList[i].waveHeader.ToEventAngle,
                    (fx) * 100000);
                Console.WriteLine(showWaveDataList[i].waveHeader.StationName + "\t" + Math.Round(showWaveDataList[i].waveHeader.MaxAmp, 5) +
                   "\t" + showWaveDataList[i].waveHeader.ToEventDist);
                //Console.WriteLine("台站=" + showWaveDataList[i].waveHeader.StationName + "\t振幅=" + Math.Round(showWaveDataList[i].waveHeader.MaxAmp, 5) +
                //    "\t到事件点的距离=" + showWaveDataList[i].waveHeader.ToEventDist  + "\tRn/R0=" + Math.Round(showWaveDataList[i].waveHeader.ToEventDist / avrDist, 3));
                //Console.WriteLine("台站=" + showWaveDataList[i].waveHeader.StationName + "\t振幅=" + Math.Round(showWaveDataList[i].waveHeader.MaxAmp, 5) +
                //    "\t到事件点的距离=" + Math.Round(showWaveDataList[i].waveHeader.ToEventDist/avrDist, 3));
            }
            lineSeriesGradient.ClosedLine = true;
            polarCollection1.Add(lineSeriesGradient);
            //// Create a series, which will have solid color transitions, from point-to-point
            //PointLineSeriesPolar lineSeriesSolid = new PointLineSeriesPolar();
            //lineSeriesSolid.LineStyle.Width = 5;
            //lineSeriesSolid.PointsVisible = false;

            ////Create data for series
            //lineSeriesSolid.Points = new PolarSeriesPoint[]
            //{
            //    new PolarSeriesPoint(210, 20, Colors.Red),
            //    new PolarSeriesPoint(240, 15, Colors.Yellow),
            //    new PolarSeriesPoint(270, 25, Colors.Lime),
            //    new PolarSeriesPoint(300, 18, Colors.Turquoise),
            //    new PolarSeriesPoint(330, 13, Colors.Purple),
            //};

            //polarCollection1.Add(lineSeriesSolid);

            return polarCollection1;
        }

        public double CalValue(WaveDataModel wdm,double r0)
        {
            double result = 0;
            double vn = wdm.waveHeader.MaxAmp;
            double rn = wdm.waveHeader.ToEventDist;
            double h = wdm.mmEvent.Z;

            double rup = vn * Math.Sqrt(Math.Pow(2.5 * h, 2) + rn * rn);
            double rdown = Math.Sqrt(Math.Pow(2.5 * h, 2) + r0 * r0);
            result = rup / rdown;
            return result;
        }

        public AxisPolarCollection GenerateAxes()
        {
            AxisPolarCollection axisPolars = new AxisPolarCollection();

            AxisPolar axis = new AxisPolar();
            axis.MinorGrid.Visible = false;
            axis.Title.Visible = false;
            axis.AmplitudeAxisLineVisible = false;
            axis.AmplitudeLabelsVisible = true;

            axis.TickMarkLocation = RoundGridTickmarkLocation.Outside;
            //axis.InnerCircleRadiusPercentage = 5;
            //axis.MinAmplitude = 100;
            axis.MaxAmplitude = 200;
            axis.AngleOrigin = 270;
            axis.Reversed = true;
            axis.MajorDivCount = 6;
            axis.Units.RadialOffsetPercentage = 100;

            //axis.AmplitudeAxisAngle = 90;
            axis.AngularAxisAutoDivSpacing = false;
            //axis.AmplitudeLabelsAngle = 90;

            axis.AngularAxisMajorDivCount = 16;
            axis.AutoFormatLabels = false;
            //显示单位
            axis.Units.Visible = false;
            axis.SupplyCustomAngleString += this.AngleAsString;
            //axis.SupplyCustomAmplitudeString += axis1_SupplyCustomAmplitudeString;
            axisPolars.Add(axis);
            return axisPolars;
        }
        public static int WaveDataModelComparison(WaveDataModel p1, WaveDataModel p2)
        {
            if (p1.waveHeader.ToEventAngle != p2.waveHeader.ToEventAngle)
            {
                return p1.waveHeader.ToEventAngle.CompareTo(p2.waveHeader.ToEventAngle);
            }
            else return 0;
        }

        private void axis1_SupplyCustomAmplitudeString(object sender, SupplyCustomAmplitudeStringEventArgs e)
        {
            e.ValueAsString = e.Value.ToString("F0") + " m/s";
        }

        /// <summary>
        /// Axis angle value as string.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Arguments for axis angle event.</param>
        private void AngleAsString(object sender, SupplyCustomAngleStringEventArgs e)
        {
            int degrees = (int)Math.Round(180f * e.Angle / Math.PI);

            switch (degrees)
            {
                case 0:
                    e.AngleAsString = "NORTH";
                    break;
                case 90:
                    e.AngleAsString = "EAST";
                    break;

                case 45:
                    e.AngleAsString = "NE";
                    break;

                case 360:
                    e.AngleAsString = "NORTH";
                    break;

                case 315:
                    e.AngleAsString = "NW";
                    break;

                case 270:
                    e.AngleAsString = "WEST";
                    break;

                case 225:
                    e.AngleAsString = "SW";
                    break;

                case 180:
                    e.AngleAsString = "SOUTH";
                    break;

                case 135:
                    e.AngleAsString = "SE";
                    break;

                default:
                    e.AngleAsString = "";
                    break;
            }
        }
    }
}
