using LiveCharts;
using LiveCharts.Wpf;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.Common.MessageEvents;
using Txgy.EWS.Client.Entity;
using Txgy.EWS.Client.IBLL;
using Txgy.EWS.Client.Models;
using Txgy.Microseismic.BaseLib.Models;
using Unity;

namespace Txgy.EWS.Client.PageModule.ViewModels
{
    internal class EventStatViewModel : BindableBase
    {
        Dispatcher _dispatcher;
        private readonly IEventAggregator _ea;
        private readonly ISearchMsEventBLL _searchMsEventBLL;
        public string[] Labels { get; set; }
        private int _selectIndex;
        public int SelectIndex
        {
            get { return _selectIndex; }
            set
            {
                SetProperty(ref _selectIndex, value);
            }
        }
        private SeriesCollection _seriesEventOfDay;

        public SeriesCollection SeriesEventOfDay
        {
            get { return _seriesEventOfDay; }
            set
            {
                SetProperty(ref _seriesEventOfDay, value);
            }
        }
        private ObservableCollection<string> _statDay;


        public ObservableCollection<string> StatDay
        {
            get { return _statDay; }
            set
            {
                SetProperty(ref _statDay, value);
            }
        }
        public Func<double, string> Formatter { get; set; }
        public Func<ChartPoint, string> PointLabel { get; set; }
        public ICommand StatCommand { get; set; }
        public ICommand SelectCommand { get; set; }
        public SeriesCollection PieMLSeriesCollection { get; set; }
        public SeriesCollection PieGradeSeriesCollection { get; set; }
        public List<GridItemEventResult> Events { get; set; } = new List<GridItemEventResult>();

        public EventStatViewModel(IUnityContainer unityContainer, IEventAggregator ea, ISearchMsEventBLL searchMsEventBLL)
        {
            InitPieML();
            InitPieLevel();
            _dispatcher = unityContainer.Resolve<Dispatcher>();
            PointLabel = chartPoint =>
               string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
            SeriesEventOfDay = new SeriesCollection();
            StatDay = new ObservableCollection<string>();
            Formatter = formatter => formatter.ToString();
            this._ea = ea;
            this._searchMsEventBLL = searchMsEventBLL;
            _ea = ea;
            this._ea.GetEvent<UpdateSearchEventMessage>().Subscribe(u =>
            {
                //Events.Clear();
                Events = new List<GridItemEventResult>(u);
                //foreach (var item in GlobalData.QueryEventList)
                //{
                //    GridItemEventResult mm = new GridItemEventResult();
                //    mm.EventTime=DateTime.Parse(item.EventTime);
                //    mm.X = item.Y;
                //    mm.Y=item.X;
                //    mm.Z= item.Z;
                //    mm.ML= item.ML;
                //    mm.Energy= item.Energy;
                //    mm.EventTimeStr = item.EventTime;
                //    Events.Add(mm);
                //}
                CreateEventList();
                //showEvents = new List<MmEvent>();
                //foreach (var item in GlobalData.EventResults)
                //{
                //    showEvents.Add(item);
                //}
            });
            CreateEventList();
        }
        private void SelectEvent(object obj)
        {
            //List<MmEvent> events = this._searchMsEventBLL.GetDateEvent(StartTime, EndTime);
            //if (events != null)
            //{
            //    ShowEvents = new ObservableCollection<MmEvent>();
            //    foreach (MmEvent e in events)
            //        ShowEvents.Add(e);
            //    SelectResult = ShowEvents.Count;
            //    UpdateEventOfDayPlot(events);
            //    UpdateEventOfML(events);
            //    CreateEventOfGrade(events);
            //}
        }
        public void CreateEventList()
        {
            //需重写20230204
            if (GlobalData.QueryEventList != null)
            {
                Events = new List<GridItemEventResult>(GlobalData.QueryEventList);
                UpdateEventOfDayPlot(Events);
                UpdateEventOfML(Events);
                CreateEventOfGrade(Events);
            }
            //if (Events.Count > 0)
            //{
            //    //SelectResult = GlobalData.EventResults.Count;
            //    UpdateEventOfDayPlot(Events);
            //    UpdateEventOfML(Events);
            //    CreateEventOfGrade(Events);
            //}
        }
        public void InitPieML()
        {
            PieMLSeriesCollection = new SeriesCollection();

            ChartValues<double> chartValue = new ChartValues<double>();
            chartValue.Add(0);
            PieSeries pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "0级至-1级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
            string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieMLSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(0);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "-1级至-2级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
            string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieMLSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(0);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "-2级至-3级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
            string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieMLSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(0);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "-3级至-4级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
            string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieMLSeriesCollection.Add(pieSeries);
        }
        public void InitPieLevel()
        {
            PieGradeSeriesCollection = new SeriesCollection();
            ChartValues<double> chartValue = new ChartValues<double>();
            chartValue.Add(0);
            PieSeries pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "A级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
             string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieGradeSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(0);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "B级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
             string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieGradeSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(0);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "C级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
             string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieGradeSeriesCollection.Add(pieSeries);

            chartValue = new ChartValues<double>();
            chartValue.Add(100);
            pieSeries = new PieSeries();
            pieSeries.DataLabels = true;
            pieSeries.Title = "D级";
            pieSeries.Values = chartValue;
            pieSeries.LabelPoint = cp =>
              string.Format("{0} ({1:P})", cp.Y, cp.Participation);
            PieGradeSeriesCollection.Add(pieSeries);
        }
        public void UpdateEventOfDayPlot(List<GridItemEventResult> mms)
        {
            var listDay = mms.GroupBy(a => new DateTime(a.OTime.Year, a.OTime.Month, a.OTime.Day)).
                Select(g => (new { day = g.Key, count = g.Count() }));
            //XL = new ObservableCollection<string>();
            SeriesEventOfDay = new SeriesCollection();
            StatDay = new ObservableCollection<string>();

            //_dispatcher.Invoke( ()=>{
            //    ColumnSeries cs = new ColumnSeries();
            //    cs.Title = "事件数量";

            //    cs.Values = new ChartValues<int>();
            //    foreach (var m in listDay)
            //    {
            //        cs.Values.Add(m.count);
            //        StatDay.Add(m.day.ToString("MM-dd"));
            //    }
            //    Formatter = formatter => formatter.ToString();

            //    SeriesEventOfDay.Add(cs);
            //});
            ColumnSeries cs = null;
            _dispatcher.Invoke(() =>
            {
                cs = new ColumnSeries();
                cs.Title = "事件数量";

                cs.Values = new ChartValues<int>();
                foreach (var m in listDay)
                {
                    cs.Values.Add(m.count);
                    StatDay.Add(m.day.ToString("MM-dd"));
                }
            });

            Formatter = formatter => formatter.ToString();

            SeriesEventOfDay.Add(cs);






        }
        public void UpdateEventOfML(List<GridItemEventResult> mms)
        {
            var mlGroup = mms.GroupBy(item =>
            {
                switch ((double)item.ML)
                {
                    case double n when (n >= -1):
                        return "0级至-1级";
                    case double n when (n >= -2):
                        return "-1级至-2级";
                    case double n when (n >= -3):
                        return "-2级至-3级";
                    default:
                        return "-3级至-4级";

                }
            }).Select(g => (new { ML = g.Key, count = g.Count() }));
            _dispatcher.Invoke(() =>
            {
                foreach (var item in mlGroup)
                {
                    var pgs = PieMLSeriesCollection.Where(g => g.Title == item.ML);

                    if (pgs != null)
                    {
                        pgs.ToList()[0].Values = new ChartValues<double>() { item.count };
                    }
                }
            });
        }
        public void CreateEventOfGrade(List<GridItemEventResult> mms)
        {
            var mlGroup = mms.GroupBy(item =>
            {
                switch ((int)item.LocSta)
                {
                    case int n when (n < 8):
                        return "D级";
                    case int n when (n < 10):
                        return "C级";
                    case int n when (n < 12):
                        return "B级";
                    case int n when (n < 24):
                        return "A级";
                    default:
                        return "A级";

                }
            }).Select(g => (new { Grade = g.Key, count = g.Count() }));
            _dispatcher.Invoke(() =>
            {
                foreach (var item in mlGroup)
                {
                    var pgs = PieGradeSeriesCollection.Where(g => g.Title == item.Grade);

                    if (pgs != null)
                    {
                        pgs.ToList()[0].Values = new ChartValues<double>() { item.count };
                    }
                }
            });
        }
    }
}
