using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Txgy.Microseismic.BaseLib.Models;
using System.Runtime.CompilerServices;

namespace Txgy.EWS.Client.Models
{
    public class CadLayerModel
    {
        /// <summary>
        /// 层名称
        /// </summary>
        public string name { get; set; }
        [JsonProperty(PropertyName = "linecolor")]
        /// <summary>
        /// 层线颜色字符串
        /// </summary>        
        public string linecolorStr { get; set; }
        [JsonIgnore]
        /// <summary>
        /// 线颜色
        /// </summary>
        public Color lineColor { get; set; }
        /// <summary>
        /// 层线宽度
        /// </summary>
        public double linewidth { get; set; }
        /// <summary>
        /// 线是否闭合
        /// </summary>
        public bool lineClose { get; set; }
        /// <summary>
        /// 层高度
        /// </summary>
        public int layerHeight { get; set; }
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool isShow { get; set; }
        /// <summary>
        /// 文字大小
        /// </summary>
        public double fontSize { get; set; }
        /// <summary>
        /// 是否填充
        /// </summary>
        public bool isFill { get; set; }
        public CadLayerModel()
        {
            
        }
        public CadLayerModel(string name,Color lineColor,double lineWidth,bool isClose,int layerHeight)
        {
            
        }
        public void SetColor()
        {
            string[] sts = linecolorStr.Split(',');
            bool noError=true;
            byte[] data = new byte[sts.Length];
            if (!byte.TryParse(sts[0], out data[0])) noError = false;
            if (!byte.TryParse(sts[1], out data[1])) noError = false;
            if (!byte.TryParse(sts[2], out data[2])) noError = false;
            lineColor = noError ? Color.FromRgb(data[0], data[1], data[2]) : Colors.Black;
        }
        public static List<CadLayerModel> ReadDwgTxtConfig(string configPath)
        {
            List<CadLayerModel> lds = new List<CadLayerModel>();
            string[] rows, cols;
            using (StreamReader sr = new StreamReader(configPath))
            {
                rows = sr.ReadToEnd().Trim().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            }

            for (int i = 1; i < rows.Length; i++)
            {
                cols = rows[i].Trim('\r').Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                string name = cols[0];
                string[] ci = cols[1].Split(',');
                Color color = Color.FromRgb(byte.Parse(ci[0]), byte.Parse(ci[1]), byte.Parse(ci[2]));
                double lineW = double.Parse(cols[2]);
                bool closeLine = true;
                if (cols[3] == "0")
                {
                    closeLine = false;
                }
                int layerHeight = int.Parse(cols[4]);
                lds.Add(new CadLayerModel(name, color, lineW, closeLine, layerHeight));
            }
            return lds;
        }

        public static List<CadLayerModel> CreateFromJson(string jsonFile)
        {
            List<CadLayerModel> cadLayerModels = new List<CadLayerModel>();
            StreamReader streamReader = new StreamReader(jsonFile);
            string jsonRoot = streamReader.ReadToEnd();  //读全部json        
                                                         //jsonRoot.Dump();
            JArray jA = JArray.Parse(jsonRoot);
            foreach (var element in jA)
            {
                CadLayerModel la = JsonConvert.DeserializeObject<CadLayerModel>(element.ToString());
                cadLayerModels.Add(la);
            }
            return cadLayerModels;
        }
    }
}
