using ACadSharp.Tables;
using Arction.Wpf.ChartingMVVM;
using Arction.Wpf.ChartingMVVM.Series3D;
using Arction.Wpf.ChartingMVVM.Views.View3D;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Txgy.EWS.Client.Common;
using WW.Cad.IO;
using WW.Cad.Model;
using Colors = System.Windows.Media.Colors;
using Color = System.Windows.Media.Color;
using WW.Cad.Model.Objects;
using WW.Cad.Model.Entities;

namespace Txgy.EWS.Client.PageModule.Models
{
    public class LCPlanModel : BindableBase
    {
        public string CadFilePath { get; set; }

        public LCPlanModel()
        {
            //CadFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\resources\\" + GlobalConfig.ProjectConfig.CadFileName;
            CadFilePath = GlobalConfig.ProjectConfig.CadFileName;
        }
        public List<PointLineSeries3D> CreateBackgroudByDwg()
        {
            List<PointLineSeries3D> dwgSeries = new List<PointLineSeries3D>();
            DxfModel model = DwgReader.Read(CadFilePath);
            try
            {
                Color color = Colors.Black;
                double lineWidth = 0.05;
                int lineStyle = 0;
                bool closeLine = true;
                foreach (var item in model.Layers)
                {
                    switch (item.Name)
                    {
                        case "N1100台站位置（实际位置）":
                            break;
                        case "等高线-20":
                            color = Color.FromRgb(192, 192, 192);
                            lineWidth = 0.1;
                            lineStyle = 1;
                            closeLine = false;
                            break;
                        case "边界线":
                            color = Color.FromRgb(255, 0, 255);
                            lineWidth = 0.1;
                            lineStyle = 1;
                            closeLine = true;
                            break;
                        case "工作面标记":
                            color = Colors.LightGray;
                            lineWidth = 0.1;
                            lineStyle = 1;
                            closeLine = true;
                            break;
                        case "断层":
                            color = Colors.Red;
                            lineWidth = 0.03;
                            lineStyle = 1;
                            closeLine = true;
                            break;
                    }
                    if (item.Enabled)
                    {
                        //Console.WriteLine($"名称：{item.Name}\t开关：{item.Enabled.ToString()}\t线宽:{item.LineWeight}\t线型：{item.LineType.Name}");
                        var entitys = model.Entities.Where(et => et.Layer.Name == item.Name);
                        if (entitys != null)
                        {
                            foreach (var entity in entitys)
                            {
                                if (entity.EntityType== "LWPOLYLINE")
                                {
                                    //Console.WriteLine($"线宽：{item.LineWeight}\t颜色：{entity.Color.ToString()}");
                                    List<SeriesPoint3D> points = new List<SeriesPoint3D>();
                                    foreach (var point in ((DxfLwPolyline)entity).Vertices)
                                    {
                                        double x = (point.X - GlobalConfig.BaseX) / 1000f;
                                        double y = (point.Y - GlobalConfig.BaseY) / 1000f;
                                        points.Add(new SeriesPoint3D(x, 50, y, color));
                                    }
                                    dwgSeries.Add(CreateDxfLwPolyline(item.Name, entity.ToString(), false, lineStyle, lineWidth, points.ToArray(), closeLine));
                                 }
                                if (entity.EntityType=="TEXT")
                                {
                                    //Console.WriteLine(1);
                                }
                            }
                            //Console.WriteLine(entitys.Count());
                       
                        }
                        //Console.WriteLine(item.Model.Entities.Count);

                    }

                }
                //foreach (var item in model.Entities)
                //{
                //    if (item.Layer.Enabled)
                //    {
                //        Console.WriteLine(item.EntityType);
                //        switch (item.EntityType)
                //        {
                //            case "LWPOLYLINE":
                //                switch (item.Layer.Name)
                //                {
                //                    case "N1100台站位置（实际位置）":
                //                        break;
                //                    case "等高线-20":
                //                        color = Color.FromRgb(192, 192, 192);
                //                        lineWidth = 0.1;
                //                        lineStyle = 1;
                //                        closeLine = false;
                //                        break;
                //                    case "边界线":
                //                        color = Color.FromRgb(255, 0, 255);
                //                        lineWidth = 0.1;
                //                        lineStyle = 1;
                //                        closeLine = true;
                //                        break;
                //                    case "工作面标记":
                //                        color = Colors.LightGray;
                //                        lineWidth = 0.1;
                //                        lineStyle = 1;
                //                        closeLine = true;
                //                        break;
                //                    case "断层":
                //                        color = Colors.Red;
                //                        lineWidth = 0.03;
                //                        lineStyle = 1;
                //                        closeLine = true;
                //                        break;
                //                }
                //                break;
                //        }
                //        List<SeriesPoint3D> points = new List<SeriesPoint3D>();
                //        foreach (var point in ((DxfLwPolyline)item).Vertices)
                //        {
                //            double x = (point.X - GlobalConfig.BaseX) / 100f;
                //            double y = (point.Y - GlobalConfig.BaseY) / 100f;
                //            points.Add(new SeriesPoint3D(x, 50, y, color));
                //        }
                //        dwgSeries.Add(CreateDxfLwPolyline(item.Layer.Name, item.ToString(), false, lineStyle, lineWidth, points.ToArray(), closeLine));
                //    }
                //    //if (item.EntityType == "LWPOLYLINE" && 
                //    //    (item.Layer.Name == "巷道集合" 
                //    //    || item.Layer.Name == "工作面标记" 
                //    //    || item.Layer.Name == "断层"
                //    //    || item.Layer.Name == "边界线"
                //    //    || item.Layer.Name == "等高线-20"
                //    //    || item.Layer.Name == "N1100台站位置（实际位置）"))
                //    //{

                //    //    if (item.Layer.Name == "N1100台站位置（实际位置）")
                //    //    {
                //    //        //color = Color.FromRgb(192, 192, 192);
                //    //        //lineWidth = 0.1;
                //    //        //lineStyle = 1;
                //    //        //closeLine = false;
                //    //        Console.WriteLine(item.EntityType.ToString());
                //    //    }
                //    //    if (item.Layer.Name == "等高线-20")
                //    //    {
                //    //        color = Color.FromRgb(192, 192, 192);
                //    //        lineWidth = 0.1;
                //    //        lineStyle = 1;
                //    //        closeLine = false;
                //    //    }
                //    //    if (item.Layer.Name=="边界线")
                //    //    {
                //    //        color = Color.FromRgb(255,0,255);
                //    //        lineWidth = 0.1;
                //    //        lineStyle = 1;
                //    //    }
                //    //    if (item.Layer.Name == "工作面标记")
                //    //    {
                //    //        color = Colors.LightGray;
                //    //        lineWidth = 0.1;
                //    //        lineStyle = 1;
                //    //    }
                //    //    else if (item.Layer.Name == "断层")
                //    //    {
                //    //        color = Colors.Red;
                //    //        lineWidth = 0.03;
                //    //        lineStyle = 1;
                //    //    }
                //    //    //Console.WriteLine(entcnt++);

                //    //    //int cnt = ((DxfLwPolyline)item).Vertices.Count;
                //    //    //int num = 0;
                //    //    //SeriesPoint3D[] points3 = new SeriesPoint3D[cnt];
                //    //    //foreach (var item2 in ((DxfLwPolyline)item).Vertices)
                //    //    //{
                //    //    //    double x = (item2.X - GlobalConfig.BaseX) / 100f;
                //    //    //    double y = (item2.Y - GlobalConfig.BaseY) / 100f;
                //    //    //    points3[num] = new SeriesPoint3D(x, 50, y, color);
                //    //    //    //Console.WriteLine($"ID:{item2.Id}\tX:{x}\tY:{y}");
                //    //    //    num++;
                //    //    //    //Console.WriteLine($"ID:{item2.Id}\tPosition:{item2.Position}\tX:{item2.X}\tY:{item2.Y}\tBulge{item2.Bulge}");
                //    //    //}
                //    //    //PointLineSeries3D seriesMultiColorLine = new PointLineSeries3D();
                //    //    //seriesMultiColorLine.Title.Text = "Multi-color line";
                //    //    //seriesMultiColorLine.MultiColorLine = true;
                //    //    //seriesMultiColorLine.AllowUserInteraction = true;
                //    //    //seriesMultiColorLine.PointsVisible = false;
                //    //    //seriesMultiColorLine.LineStyle.LineOptimization = (LineOptimization)lineStyle;
                //    //    //seriesMultiColorLine.LineStyle.Width = lineWidth;
                //    //    //seriesMultiColorLine.Points = points3;
                //    //    //seriesMultiColorLine.ClosedLine= closeLine;

                //    //}
                //}

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return dwgSeries;
        }
        public List<PointLineSeries3D> CreateCadImage3DList()
        {
            List<PointLineSeries3D> pls3ds = new List<PointLineSeries3D>();

            //for (int i = 0; i < 20; i++)
            //{
            pls3ds.Add(CreatePointLine3D("i", "cad",
                    new SeriesPoint3D[]
                    {
                        new SeriesPoint3D(10,10,10),
                        new SeriesPoint3D(100,20,20),
                        new SeriesPoint3D(120,30,30),
                        new SeriesPoint3D(40,40,1),
                        new SeriesPoint3D(50,50,4),
                        new SeriesPoint3D(60,60,8),

                    }, 1, 1));
            //}
            return pls3ds;
        }

        /// <summary>
		/// Create collection of polygons
		/// </summary>
		/// <returns></returns>
		internal Polygon3DCollection CreatePolygons()
        {
            List<Polygon3D> polygons = new List<Polygon3D>(5);
            Polygon3DCollection polygon3Ds = new Polygon3DCollection();
            polygons.Add(
                CreatePolygon("Polygon A", Colors.Magenta,
                    new Polygon3DPoint[]
                    {
                        new Polygon3DPoint(0,0),
                        new Polygon3DPoint(0,10),
                        new Polygon3DPoint(8,10),
                        new Polygon3DPoint(8,8),
                        new Polygon3DPoint(4,8),
                        new Polygon3DPoint(3,7),
                        new Polygon3DPoint(3,0),
                    }, 0, 20));
            polygons.Add(CreatePolygon("Polygon B", Colors.Orange,
                    new Polygon3DPoint[]
                    {
                        new Polygon3DPoint(4,8),
                        new Polygon3DPoint(8,8),
                        new Polygon3DPoint(8,6),
                        new Polygon3DPoint(3,6),
                        new Polygon3DPoint(3,7),
                    }, 0, 30));
            polygons.Add(CreatePolygon("Polygon C", Colors.MistyRose,
                    new Polygon3DPoint[]
                    {
                        new Polygon3DPoint(3,6),
                        new Polygon3DPoint(8,6),
                        new Polygon3DPoint(8,2),
                        new Polygon3DPoint(5,2),
                        new Polygon3DPoint(6,4),
                        new Polygon3DPoint(3,4),
                    }, 0, 15));
            polygons.Add(CreatePolygon("Polygon D", Colors.CornflowerBlue,
                    new Polygon3DPoint[]
                    {
                        new Polygon3DPoint(3,4),
                        new Polygon3DPoint(6,4),
                        new Polygon3DPoint(5,2),
                        new Polygon3DPoint(8,2),
                        new Polygon3DPoint(8,0),
                        new Polygon3DPoint(3,0),
                    }, 0, 25));

            Color color = Colors.Aquamarine;

            polygons.Add(CreatePolygon("Polygon E", Color.FromArgb(100, color.R, color.G, color.B),
                    new Polygon3DPoint[]
                    {
                        new Polygon3DPoint(8, 10),
                        new Polygon3DPoint(6, 7.5),
                        new Polygon3DPoint(7.5, 3),
                        new Polygon3DPoint(10, 4),
                        new Polygon3DPoint(9, 9),
                    }, 10, 40));
            polygon3Ds.AddRange(polygons);
            return polygon3Ds;
        }
        /// <summary>
        /// Create polygons
        /// </summary>
        /// <param name="title"></param>
        /// <param name="color"></param>
        /// <param name="edgePoints"></param>
        /// <param name="minY"></param>
        /// <param name="maxY"></param>
        /// <returns></returns>
        private static Polygon3D CreatePolygon(string title, Color color, Polygon3DPoint[] edgePoints, double minY, double maxY)
        {
            Polygon3D polygon = new Polygon3D();
            polygon.Material.DiffuseColor = color;
            polygon.Material.DiffuseColor = color;
            polygon.Points = edgePoints;
            polygon.YMin = minY;
            polygon.YMax = maxY;

            return polygon;
        }
        private static PointLineSeries3D CreatePointLine3D(string title, string tag, SeriesPoint3D[] seriesPoint3Ds, int lineStyle, double lineWidth)
        {
            PointLineSeries3D pls3d = new PointLineSeries3D
            {
                Tag = tag,
                Points = seriesPoint3Ds,
                PointsVisible = true,
                MultiColorLine = true,
                AllowUserInteraction = true
            };
            pls3d.LineStyle.LineOptimization = (LineOptimization)lineStyle;
            pls3d.LineStyle.Width = lineWidth;
            pls3d.Points = seriesPoint3Ds;
            return pls3d;
        }
        public static PointLineSeries3D CreateDxfLwPolyline(string tag, string title, bool allowUser, int lineStyle, double lineWidth, SeriesPoint3D[] points, bool closeLine)
        {
            PointLineSeries3D pls3d = new PointLineSeries3D();
            pls3d.Tag = tag;
            pls3d.Title.Text = title;
            pls3d.MultiColorLine = true;
            pls3d.AllowUserInteraction = allowUser;
            pls3d.PointsVisible = false;
            pls3d.LineStyle.LineOptimization = (LineOptimization)lineStyle;
            pls3d.LineStyle.Width = lineWidth;
            pls3d.Points = points;
            pls3d.ClosedLine = closeLine;
            return pls3d;
        }
    }
}
