using Arction.Wpf.ChartingMVVM;
using Arction.Wpf.ChartingMVVM.Series3D;
using Arction.Wpf.ChartingMVVM.Views.View3D;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.Common.MessageEvents;
using Unity;

namespace Txgy.EWS.Client.PageModule.Views
{
    /// <summary>
    /// StereogramView.xaml 的交互逻辑
    /// </summary>
    public partial class StereogramView : UserControl
    {
        private IEventAggregator _ea;
        Dispatcher _dispatcher;
        // Delegates
        private delegate void HandleDataGeneratedDelegate();
        /// <summary>
        /// Delegate for data generation.
        /// </summary>
        private HandleDataGeneratedDelegate _handleDataGenerated;
        public SizeDoubleXYZ EventSize;
        public StereogramView(IUnityContainer unityContainer, IEventAggregator ea, EventListView elv)
        {
            InitializeComponent();
            _ea = ea;
            _dispatcher = unityContainer.Resolve<Dispatcher>();
            _handleDataGenerated = new HandleDataGeneratedDelegate(PutSamplesToChart);
            EventSize = new SizeDoubleXYZ(GlobalConfig.eventDSize, GlobalConfig.eventDSize, GlobalConfig.eventDSize);
            this._ea.GetEvent<UpdateSearchEventMessage>().Subscribe(u =>
            {
                _dispatcher.Invoke(_handleDataGenerated);
                //保存图片
                //BitmapEncoder encoder = new JpegBitmapEncoder();
                //encoder.Frames.Add(chart.CaptureToBitmap());
                //string file1 = @"e:\png.jpg";
                //using (Stream stm = File.Create(file1))
                //{
                //    encoder.Save(stm);
                //}
            });
            _dispatcher.Invoke(_handleDataGenerated);
            this.EventListContent.Content = elv;
        }
        private void PutSamplesToChart()
        {
            //(Application.Current. as EarlyWarningView).chart.Add(item);
            //Type type = Type.GetType("Txgy.EWS.Client.PageModule.Views.EarlyWarningView");
            //LightningChart lChart = (Activator.CreateInstance(type) as EarlyWarningView).chart;

            this.chart.BeginUpdate();

            List<SeriesPoint3D> sp3List = new List<SeriesPoint3D>();
            foreach (var ms in GlobalData.QueryEventList)
            {
                {
                    float sizeFactor = (float)(((4f + ms.ML) / (4f)) * 4f) + 0.1f;
                    int colorIndex = (int)(((ms.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                        / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                        * GlobalConfig.ColorCountDefault);
                    Color ec = GlobalConfig.eventDepthColor[colorIndex];

                    sp3List.Add(new SeriesPoint3D((ms.X), (GlobalConfig.BaseZ - ms.Z), (ms.Y), ec, sizeFactor));

                }
            }

            PointLineSeries3D plsEvent3d = new PointLineSeries3D();
            plsEvent3d.Title.Text = "微地震事件";
            plsEvent3d.Tag = "mmevent";
            plsEvent3d.IndividualPointColors = true;
            plsEvent3d.IndividualPointSizes = true;
            plsEvent3d.LineVisible = false;
            plsEvent3d.AllowUserInteraction = true;
            plsEvent3d.PointsVisible = true;
            plsEvent3d.PointStyle.Shape3D = PointShape3D.Sphere;
            plsEvent3d.PointStyle.Size3D = EventSize;
            plsEvent3d.AddPoints(sp3List.ToArray(), false);
            //ChartPointLineCollection.Add(plsEvent3d);
            //Console.WriteLine(ChartPointLineCollection.Count);
            this.chart.View3D.PointLineSeries3D.Add(plsEvent3d);
            this.chart.EndUpdate();
            
        }
    }
}
