using Arction.Wpf.Charting;
using Arction.Wpf.Charting.Axes;
using Arction.Wpf.Charting.Series3D;
using Arction.Wpf.Charting.Views.View3D;
using Aspose.Cells.Charts;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Txgy.EWS.Client.Models;

namespace Txgy.EWS.Client.Common.Helpers
{
    public class ReportStereogramImage : IDisposable
    {
        string stereoImagePath;
        public LightningChart lightChart = new LightningChart();

        View3D view3D = new View3D();
        int _widthRS = 100;
        int _heightRS = 50;
        int _depthRS = 100;
        double _sideRotationRS = 0;
        double _horizontalRotationRS = -30;
        double _verticalRotationRS = 30;
        WallXZ _topWall;
        WallYZ _leftWall;
        WallYZ _rightWall;
        WallXZ _bottomWall;
        WallXY _backWall;
        WallXY _frontWall;
        AxisX3D _axisXPri3D;
        AxisY3D _axisYPri3D;
        AxisZ3D _axisZPri3D;
        public double _scale = 1;
        SizeDoubleXYZ eventSize;
       

        public ReportStereogramImage()
        {
            stereoImagePath = AppDomain.CurrentDomain.BaseDirectory + "\\resources\\stereoImageTemp.png";
            eventSize = new SizeDoubleXYZ(GlobalConfig.eventDSize, GlobalConfig.eventDSize, GlobalConfig.eventDSize);

            _bottomWall = new WallXZ();
            _bottomWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _bottomWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _bottomWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));
            _topWall = new WallXZ();
            _topWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _topWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _topWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));
            _leftWall = new WallYZ();
            _leftWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _leftWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _leftWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));
            _rightWall = new WallYZ();
            _rightWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _rightWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _rightWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));
            _frontWall = new WallXY();
            _frontWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _frontWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _frontWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));
            _backWall = new WallXY();
            _backWall.Material = new Material3D() { DiffuseColor = Color.FromArgb(0, 248, 248, 255) };
            _backWall.SetGridStripColor1(Color.FromArgb(0, 248, 248, 255));
            _backWall.SetGridStripColor2(Color.FromArgb(0, 248, 248, 255));

            _axisXPri3D = new AxisX3D();
            _axisXPri3D.Maximum = GlobalConfig.ProjectConfig.WorkArea.EMax / _scale;
            _axisXPri3D.Minimum = GlobalConfig.ProjectConfig.WorkArea.EMin / _scale;
            _axisXPri3D.Title.Text = "西东";
            _axisXPri3D.LabelsVisible = true;
            _axisXPri3D.LabelsColor = Colors.Black;
            _axisXPri3D.Orientation = PlaneXAxis3D.XZ;
            _axisXPri3D.Location = AxisXLocation3D.BottomFront;
            _axisXPri3D.LabelsNumberFormat = "00000000";
            _axisXPri3D.AutoFormatLabels = false;

            _axisYPri3D = new AxisY3D();
            _axisYPri3D.Title.Text = "海拔";
            _axisYPri3D.Minimum = (GlobalConfig.ProjectConfig.WorkArea.ZMin / _scale);
            _axisYPri3D.Maximum = ((GlobalConfig.ProjectConfig.WorkArea.ZMax + 50) / _scale);
            _axisYPri3D.Reversed = true;
            _axisYPri3D.LabelsColor = Colors.Black;
            //AxisYPri3D.Visible = false;

            _axisZPri3D = new AxisZ3D();
            _axisZPri3D.Maximum = GlobalConfig.ProjectConfig.WorkArea.NMax / _scale;
            _axisZPri3D.Minimum = GlobalConfig.ProjectConfig.WorkArea.NMin / _scale;
            _axisZPri3D.Title.Text = "南北";
            _axisZPri3D.Orientation = PlaneZAxis3D.XZ;
            _axisZPri3D.Location = AxisZLocation3D.BottomRight;
            _axisZPri3D.LabelsColor = Colors.Black;
            _axisZPri3D.LabelsNumberFormat = "0000000";
            _axisZPri3D.AutoFormatLabels = false;

            lightChart = new LightningChart();

            lightChart.ChartBackground = new Fill() { GradientFill = GradientFill.Solid, GradientColor = Colors.White };
           

            lightChart.BeginUpdate();
            lightChart.ActiveView = ActiveView.View3D;
            lightChart.Name = "StereogramImage";
            lightChart.Title.Text = "三维视图";
            lightChart.View3D.ZoomPanOptions.AllowWheelZoom = false;
            lightChart.View3D.ZoomPanOptions.AxisWheelAction = AxisWheelAction.None;

            lightChart.View3D.Lights = (List<Light3D>)View3D.CreateDefaultLights();
            lightChart.View3D.Dimensions = new SizeDoubleXYZ(_widthRS, _heightRS, _depthRS);
            lightChart.View3D.Camera = new Camera3D() { RotationX = _verticalRotationRS, RotationY = _horizontalRotationRS, RotationZ = _sideRotationRS };
            lightChart.View3D.OrientationArrows = new OrientationArrowsOptions3D() { ArrowLength = 50, Visible = true };
            lightChart.View3D.ClipContents = true;
            //lightChart.View3D.PointLineSeries3D = ChartPointLineCollection;
            //lightChart.View3D.Annotations = ChartAnnotationCollection;
            lightChart.View3D.WallOnBottom = _bottomWall;
            lightChart.View3D.WallOnTop = _topWall;
            lightChart.View3D.WallOnLeft = _leftWall;
            lightChart.View3D.WallOnRight = _rightWall;
            lightChart.View3D.WallOnFront = _frontWall;
            lightChart.View3D.WallOnBack = _backWall;
            lightChart.View3D.XAxisPrimary3D = _axisXPri3D;
            lightChart.View3D.YAxisPrimary3D = _axisYPri3D;
            lightChart.View3D.ZAxisPrimary3D = _axisZPri3D;
            lightChart.EndUpdate();

            //lightChart.SaveToStream(mStream, TargetImageFormat.Png, 1000, 1000);
            //byte[] data = new byte[mStream.Length];
            //BitmapImage bimage= BitmapHelper.ConvertToBitmap(data);
            //BitmapEncoder encoder = new JpegBitmapEncoder();
            //encoder.Frames.Add(BitmapFrame.Create(bimage));
            //string file1 = @"c:\png.jpg";
            //using (Stream stm = File.Create(file1))
            //{
            //    encoder.Save(stm);
            //}

            //lightChart.SaveToFile("e:\\6.png",1000,1000);

        }
        public void SaveImage()
        {
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(lightChart.CaptureToBitmap());
            string file1 = @"e:\png.jpg";
            using (Stream stm = File.Create(file1))
            {
                encoder.Save(stm);
            }
        }
        public void DrawStereoImage(List<GridItemEventResult> events)
        {
            lightChart.BeginUpdate();
            View3D v = lightChart.View3D;
            v.PointLineSeries3D.Clear();

            PointLineSeries3D plsEvent3d = new PointLineSeries3D(v, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
            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);

            List<SeriesPoint3D> sp3List = new List<SeriesPoint3D>();
            foreach (var ms in events)
            {
                {
                    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));

                }
            }
            plsEvent3d.Points = sp3List.ToArray();
            v.PointLineSeries3D.Add(plsEvent3d);
            lightChart.EndUpdate();
        }
        public byte[] DrawStereoImageToBytes(List<GridItemEventResult> events)
        {
            lightChart.BeginUpdate();
            View3D v = lightChart.View3D;
            v.PointLineSeries3D.Clear();

            PointLineSeries3D plsEvent3d = new PointLineSeries3D(v, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
            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;


            List<SeriesPoint3D> sp3List = new List<SeriesPoint3D>();
            foreach (var ms in events)
            {
                {
                    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));

                }
            }
            plsEvent3d.Points = sp3List.ToArray();
            v.PointLineSeries3D.Add(plsEvent3d);
            lightChart.EndUpdate();
            //lightChart.SaveToFile(stereoImagePath);
            MemoryStream mStream = new MemoryStream();
            lightChart.SaveToStream(mStream);
            BinaryReader br = new BinaryReader(mStream);
            return br.ReadBytes((int)mStream.Length);
        }
        public void SaveToFile(string fileName, int width, int height)
        {
            lightChart.SaveToFile(fileName, width, height);
        }
        public void Dispose()
        {
            if (lightChart != null)
            {
                lightChart.Dispose();
                lightChart = null;
            }
        }
    }
}
