using ESRI.ArcGIS.Geometry;
using KGIS.Framework.Utils;
using Kingo.PluginServiceInterface.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text;
namespace Kingo.PluginServiceInterface
{
    /// 
    /// Model:工程结构
    /// Description:用于记录工程信息
    /// Author:李进营
    /// Finished:2017年11月18日
    /// 
    [Serializable]
    public class ProjectInfo : INotifyPropertyChanged
    {
        /// 
        /// =======临时增加,用于控制图形合并提示框=========
        /// 是否再次提醒(true:下次继续提醒;false:下次不再提醒)
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public bool IsPromptAgain = true;
        /// 
        /// 工程名称
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public string ProjName { get; set; }
        /// 
        /// 单图斑建库-工程使用人
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public string ProjUserName { get; set; }
        /// 
        /// 默认方案
        /// 
        //public string DefaultScheme { get; set; }
        ///// 
        ///// 当前方案
        ///// 
        //public string CurrentScheme { get; set; }
        ///// 
        ///// 记录所有方案
        ///// 
        //public List SchemeList { get; set; }
        /// 
        /// 数据库名称
        /// 
        public string DataBaseName { get; set; }
        /// 
        /// 变更数据库
        /// 
        public string BGDatabase { get; set; }
        /// 
        /// 变更成果导出路径
        /// 
        public string BGResultPath { get; set; }
        /// 
        /// 监测图层数据库
        /// 
        public string JCDatabase { get; set; }
        /// 
        /// 单图斑基础库图层数据库
        /// 
        public string DTBJKDatabase { get; set; }
        /// 
        /// 单图斑变更更新图层图层数据库
        /// 
        public string DTBBGGXDatabase { get; set; }
        /// 
        /// 单图斑数据检查库
        /// 
        public string DTBCheckDatabase { get; set; }
        /// 
        /// 单图斑预处理数据库
        /// 
        public string DTBYCLDatabase { get; set; }
        /// 
        /// 单图斑预处理预审数据库
        /// 
        public string DTBYCLYHDatabase { get; set; }
        /// 
        /// 增量数据库
        /// 
        public string ZLDatabase { get; set; }
        /// 
        /// 年末数据库
        /// 
        public string NMDatabase { get; set; }
        /// 
        /// 变化图斑数据库
        /// 
        public string BHTBDatabase { get; set; }
        /// 
        /// 举证成果包地址
        /// 
        public string DBPath { get; set; }
        /// 
        /// 工程后缀名
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public string ProjSuffix { get; set; }
        /// 
        /// 行政代码
        /// 
        public string CODE { get; set; }
        /// 
        /// 行政区名称
        /// 
        public string CodeName { get; set; }
        /// 
        /// 陆地调查面积
        /// 
        public double DCMJ { get; set; }
        /// 
        /// 海岛调查面积
        /// 
        public double DCMJHD { get; set; }
        private KZMJInfo _kzmj;
        public KZMJInfo KZMJ
        {
            get => _kzmj; set
            {
                _kzmj = value;
                RaisePropertyChanged("KZMJ");
            }
        }
        /// 
        /// 比例尺
        /// 
        public string Scale { get; set; }
        /// 
        /// 比例尺代码
        /// 
        public string ScaleCode { get; set; }
        /// 
        /// 容差
        /// 
        public double XYResolution { get; set; }
        /// 
        /// 陆地控制面积公顷
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public double DCMJ2 { get; set; }
        /// 
        /// 海岛控制面积公顷
        /// 
        [Newtonsoft.Json.JsonIgnore]
        public double DCMJHD2 { get; set; }
        /// 
        /// 分度带
        /// 
        public string FDD { get; set; }
        //private string 
        /// 
        /// 图形带号
        /// 
        public int TXDH { get; set; }
        /// 
        /// 中央经度
        /// 
        public int ZYJD { get; set; }
        /// 
        /// 影像路径
        /// 
        public string[] ImgPath { get; set; }
        /// 
        /// 坐标参考文件路径(已经过时,请使用PrjFileStr)
        /// 
        public string PrjFilePath { get; set; }
        /// 
        /// 坐标参考字符串
        /// 
        public string PrjFileStr { get; set; }
        /// 
        /// 工程所在文件夹
        /// 
        public string ProjDir { get; set; }
        /// 
        /// 工程目录文件夹名称(行政区名称+代码)
        /// 
        public string ProWorkDir { get; set; }
        /// 
        /// 模板数据
        /// 
        public string TempData { get; set; }
        public string MulitMapData { get; set; }
        public string DicData { get; set; }
        /// 
        /// 是否启用快捷键
        /// 
        public bool EnableShortcut { get; set; }
        /// 
        /// 工作目录-基础库路径
        /// 
        public string JCKPath { get; set; }
        /// 
        /// 工作目录-国检年末库路径
        /// 
        public string GJNMKPath { get; set; }
        /// 
        /// 工作目录-变更范围成果
        /// 
        public string BGFWCGPath { get; set; }
        /// 
        /// 工作目录-监测图斑路径
        /// 
        public string JCTBPath { get; set; }
        /// 
        /// 工作目录-临时用地范围路径
        /// 
        public string LSYDFWPath { get; set; }
        /// 
        /// 工作目录-历年耕地范围路径
        /// 
        public string LNGDFWPath { get; set; }
        /// 
        /// 工作目录-用地管理信息路径
        /// 
        public string YDGLXXPath { get; set; }
        /// 
        /// 工作目录-专项数据路径
        /// 
        public string ZXSJPath { get; set; }
        /// 
        /// 工作目录-坡度图路径
        /// 
        public string PDTPath { get; set; }
        /// 
        /// 工作目录-合法建设用地范围
        /// 
        public string HFJSYDPath { get; set; }
        /// 
        /// 工作目录-耕地及恢复属性图斑路径
        /// 
        public string GDJHFSXPath { get; set; }
        /// 
        /// 工作目录-城镇村等用地路径
        /// 
        public string CZCDYDPath { get; set; }
        /// 
        /// 工作目录-影像路径
        /// 
        public string YXPath { get; set; }
        /// 
        /// 工作目录-5级坡度图
        /// 
        public string txtfivePDT { get; set; }
        /// 
        /// 工作目录-承诺耕地参考图层
        /// 
        public string CNGDPath { get; set; }
        /// 
        /// 工作目录-国家下发监测图层
        /// 
        public string GJXFJCPath { get; set; }
        /// 
        /// 工作目录-撂荒耕地参考图层
        /// 
        public string LHGDPath { get; set; }
        /// 
        /// 工作目录-后备耕地图层
        /// 
        public string HBGDPath { get; set; }
        /// 
        /// 工作目录-参考数据
        /// 
        public string CKSJPath { get; set; }
        /// 
        /// 工作目录-上期单图斑建库成果
        /// 
        public string SQDTBJKCGPath { get; set; }
        /// 
        /// 工作目录-工作底图
        /// 
        public string GZDTPath { get; set; }
        /// 
        /// 工作目录-林草湿园
        /// 
        public string LCSYPath { get; set; }
        public List ListTaskPackage { get; set; }
        public string TaskPath { get; set; }
        public int WKID { get; set; }
        /// 
        /// 设置的用户名
        /// 
        public string UserName { get; set; }
        /// 
        /// 用户密码
        /// 
        public string UserPassword { get; set; }
        /// 
        /// 任务包密码
        /// 
        //public string Pathpassword = "; password = 009FA8952F59C8853AF0CC8DA4394D00FEDCD51361C7F1C9C2629EBEB8FDE5B2A1";
        public string Pathpassword = "";
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        /// 
        /// 批量赋属性值列表
        /// 
        public List ListBatchAttributes { get; set; } = new List();
        /// 
        /// 创建投影坐标系
        /// 
        /// esriSRProjCS4Type
        /// 
        public ISpatialReference GetCurentProjectedCoordinate()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(PrjFileStr))
                {
                    //创建一个要素集创建一个投影  
                    ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass();
                    ISpatialReference pSpatialReference;
                    int s;
                    PrjFileStr = PrjFileStr.Replace("'", "\"");
                    pSpatialRefFac.CreateESRISpatialReference(PrjFileStr, out pSpatialReference, out s);
                    return pSpatialReference;
                }
                return null;
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
                return null;
            }
        }
        public ProjectInfo()
        {
            //DefaultScheme = "初始方案";
            //CurrentScheme = "初始方案";
        }
        public static ProjectInfo OpenProject(string pFilePath)
        {
            if (!string.IsNullOrWhiteSpace(pFilePath) && File.Exists(pFilePath))
            {
                string strBase64 = string.Empty;
                //读取文件内容到字节数组
                using (FileStream stream = new FileStream(pFilePath, FileMode.Open))
                {
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    stream.Close();
                    //将字节数组转换成Base64字符串
                    strBase64 = Convert.ToBase64String(bytes);
                }
                //将Base64字符串进行解密,得到原始文件数据
                string strData = Helper.AuthorizationVerification.Decrypt(strBase64, "58ef12f9891a4cd89fa7bdd181ef13a7");
                //将原始文件数据反序列化为对象
                ProjectInfo data = JsonConvert.DeserializeObject(strData);
                if (data == null)
                {
                    data = SerializeAPI.DeserializeToObject(strData.Substring(1));
                }
                if (data != null)
                {
                    data.ProjName = System.IO.Path.GetFileNameWithoutExtension(pFilePath);
                    data.ProjSuffix = System.IO.Path.GetExtension(pFilePath);
                    data.ProjDir = System.IO.Path.GetDirectoryName(pFilePath);
                    data.BGDatabase = System.IO.Path.Combine(data.ProjDir, "BGDB.gdb");
                    if (!Directory.Exists(data.BGDatabase))
                        data.BGDatabase = string.Empty;
                    data.ZLDatabase = System.IO.Path.Combine(data.ProjDir, "ZLDB.gdb");
                    if (!Directory.Exists(data.ZLDatabase))
                        data.ZLDatabase = string.Empty;
                    data.NMDatabase = System.IO.Path.Combine(data.ProjDir, "NMDB.gdb");
                    if (!Directory.Exists(data.NMDatabase))
                        data.NMDatabase = string.Empty;
                    FileInfo fIf = new FileInfo(data.ProjDir);
                    //data.ProWorkDir=
                    data.DCMJ2 = Math.Round(data.DCMJ * 0.0001, 2);
                    data.DCMJHD2 = Math.Round(data.DCMJHD2 * 0.0001, 2);
                    //if (string.IsNullOrEmpty(data.PrjFileStr) && !string.IsNullOrEmpty(data.PrjFilePath) && System.IO.File.Exists(data.PrjFilePath))
                    //{
                    //    // 兼容老的坐标参考系
                    //    SetPrjFileStrByOldPrjFilePath(data);
                    //}
                }
                return data;
            }
            else
            {
                return null;
            }
        }
        /// 
        /// 复制工程信息(过滤TempData)
        /// 
        /// 
        public ProjectInfo CopyProject()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.ProjDir))
                    return null;
                ProjectInfo ProInfo = new ProjectInfo()
                {
                    ProjDir = this.ProjDir,
                    ProjName = this.ProjName,
                    XYResolution = 0.0001,//默认万分之一容差
                    CODE = this.CODE,
                    ProjSuffix = ProjSuffix,//默认后缀
                    PrjFileStr = this.PrjFileStr,//坐标系参考字符串
                    ScaleCode = "I",//默认比例尺
                    Scale = "1:2000",
                    FDD = FDD,
                    WKID = this.WKID,
                    DCMJ = this.DCMJ,
                    DCMJHD = this.DCMJHD,
                    DataBaseName = "PrjDB.gdb",
                    BGDatabase = this.BGDatabase,
                    ZLDatabase = this.ZLDatabase,
                    NMDatabase = this.NMDatabase,
                    JCKPath = this.JCKPath,
                    BGResultPath = this.BGResultPath,
                    ListZLXXInfo = this.ListZLXXInfo,
                    LessThan30Missing = this.LessThan30Missing,
                    CZC20MCTSH = this.CZC20MCTSH,
                    KZMJ = new KZMJInfo()
                    {
                        TZQ_HDMJ = this.KZMJ.TZQ_HDMJ,
                        TZQ_HDMJ_GQ = this.KZMJ.TZQ_HDMJ_GQ,
                        TZH_HDMJ = this.KZMJ.TZH_HDMJ,
                        TZH_HDMJ_GQ = this.KZMJ.TZH_HDMJ_GQ,
                        TZQ_LDMJ = this.KZMJ.TZQ_LDMJ,
                        TZQ_LDMJ_GQ = this.KZMJ.TZQ_LDMJ_GQ,
                        TZH_LDMJ = this.KZMJ.TZH_LDMJ,
                        TZH_LDMJ_GQ = this.KZMJ.TZH_LDMJ_GQ
                    }
                };
                return ProInfo;
            }
            catch (Exception ex)
            {
                CommonHelper.RecordsErrLog("赋值当前工程失败:", ex);
                throw ex;
            }
        }
        /// 
        /// 获取工程文件路径
        /// 
        /// 
        public string GetProjFilePath()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ProjDir))
                    return string.Empty;
                string filePath = ProjDir + "\\" + ProjName + ProjSuffix;
                if (File.Exists(filePath))
                    return filePath;
                else
                    return "";
            }
            catch (Exception ex)
            {
                LogAPI.Debug("获取工程文件路径异常:" + ex);
                return "";
            }
        }
        /// 
        /// 获取数据字典文件路径
        /// 
        /// 
        public string GetDicDataPath()
        {
            if (!string.IsNullOrWhiteSpace(ProjDir))
            {
                string dbPath = System.IO.Path.Combine(ProjDir, "BGTJ.sqlite");
                if (File.Exists(dbPath))
                {
                    return dbPath;
                }
            }
            return string.Empty;
        }
        /// 
        /// 获取检查结果DB路径
        /// 
        /// 
        public string GetDataCheckrResultPath()
        {
            if (!string.IsNullOrWhiteSpace(ProjDir))
            {
                string dbPath = System.IO.Path.Combine(ProjDir, "DataCheckrResult.db");
                if (File.Exists(dbPath))
                {
                    return dbPath;
                }
            }
            return string.Empty;
        }
        public string GetDHZSDBPath()
        {
            if (!string.IsNullOrWhiteSpace(ProjDir))
            {
                string dbPath = System.IO.Path.Combine(ProjDir, "外业助手.gdb");
                if (Directory.Exists(dbPath))
                {
                    return dbPath;
                }
            }
            return string.Empty;
        }
        /// 
        /// 地图范围
        /// 
        public MapExtent MapExtent { get; set; }
        public bool Save()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.ProjDir))
                {
                    return false;
                }
                //将对象序列化成字符串
                //string Str = SerializeAPI.SerializeToXML(this);
                string sss = JsonConvert.SerializeObject(this);
                //对字符串进行加密
                Byte[] resultArray = AesEncrypt(sss, "58ef12f9891a4cd89fa7bdd181ef13a7");
                //修改人:李进营  修改时间  增加using引用
                using (Stream stream = new FileStream(this.ProjDir + "\\" + this.ProjName + ProjSuffix, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    stream.Write(resultArray, 0, resultArray.Length);
                    stream.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
                return false;
            }
        }
        public string GetPRJName()
        {
            string result = string.Empty;
            if (!string.IsNullOrWhiteSpace(PrjFileStr))
            {
                try
                {
                    string beginstr = "[";
                    int leftindex = PrjFileStr.IndexOf(beginstr) + beginstr.Length;
                    string spatialrefrightstr = PrjFileStr.Substring(leftindex);
                    int length = spatialrefrightstr.IndexOf(",");
                    result = spatialrefrightstr.Substring(0, length).Replace("\"", "");
                }
                catch
                {
                }
            }
            return result;
        }
        /// 
        ///  AES 加密
        /// 
        /// 需要加密的字符串
        /// 秘钥
        /// 
        public Byte[] AesEncrypt(string str, string strKey)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(strKey))
                    throw new Exception("加密秘钥为空!");
                byte[] Key = Encoding.UTF8.GetBytes(strKey);
                if (Key.Length != 32)
                    throw new Exception("秘钥长度不符合要求!");
                if (string.IsNullOrEmpty(str)) return null;
                Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
                string strBase64 = Convert.ToBase64String(toEncryptArray);
                byte[] contentArray = Encoding.UTF8.GetBytes(strBase64);
                #region 加密
                System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
                {
                    Key = Key,
                    Mode = System.Security.Cryptography.CipherMode.ECB,
                    Padding = System.Security.Cryptography.PaddingMode.PKCS7
                };
                System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(contentArray, 0, contentArray.Length);
                #endregion
                return resultArray;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private string ByteToString(byte[] arrByte)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in arrByte)
            {
                sb.Append(b > 15 ? Convert.ToString(b, 16) : '0' + Convert.ToString(b, 16));
            }
            return sb.ToString();
        }
        #region 工程信息增加
        /// 
        /// 项目类型
        /// 
        public EnumProjType ProjType { get; set; }
        /// 
        /// 县级行政区代码发生变化
        /// 
        public bool XZQDMChange { get; set; }
        /// 
        /// 城镇村等用地碎图斑小于30平的是否自动修复
        /// 
        public bool LessThan30Missing { get; set; }
        /// 
        /// 城镇村20名称特殊化
        /// 
        public bool CZC20MCTSH { get; set; }
        /// 
        /// 村级调查区名称发生变化
        /// 
        public bool ZLDWMCChange { get; set; }
        /// 
        /// 调入村级调查区
        /// 
        public string DR_CJDCQPath { get; set; }
        /// 
        /// 调入行政区
        /// 
        public string DR_XZQPath { get; set; }
        /// 
        /// 调入其他区县基础数据库
        /// 
        public List DR_DataKPaths { get; set; }
        /// 
        /// 坐落变更信息
        /// 
        public ObservableCollection ListZLXXInfo { get; set; }
        #endregion
        public string DataCheckPath { get; set; }
        /// 
        /// 调入CZC
        /// 
        public string DR_CZCPath { get; set; }
    }
    /// 
    /// 属性赋值配置类
    /// 
    [Serializable]
    public class KZMJInfo : INotifyPropertyChanged
    {
        private double tZQ_LDMJ;
        private double tZQ_LDMJ_GQ;
        private double tZQ_HDMJ;
        private double tZQ_HDMJ_GQ;
        private double tZH_LDMJ;
        private double tZH_LDMJ_GQ;
        private double tZH_HDMJ;
        private double tZH_HDMJ_GQ;
        /// 
        /// 调整前陆地面积
        /// 
        public double TZQ_LDMJ
        {
            get => tZQ_LDMJ;
            set
            {
                tZQ_LDMJ = value;
                TZQ_LDMJ_GQ = Math.Round(value * 0.0001, 2);
                RaisePropertyChanged("TZQ_LDMJ");
            }
        }
        /// 
        /// 调整前陆地面积(公顷)
        /// 
        public double TZQ_LDMJ_GQ
        {
            get => tZQ_LDMJ_GQ; set
            {
                tZQ_LDMJ_GQ = value;
                RaisePropertyChanged("TZQ_LDMJ_GQ");
            }
        }
        /// 
        /// 调整前海岛面积
        /// 
        public double TZQ_HDMJ
        {
            get => tZQ_HDMJ; set
            {
                tZQ_HDMJ = value;
                TZQ_HDMJ_GQ = Math.Round(value * 0.0001, 2);
                RaisePropertyChanged("TZQ_HDMJ");
            }
        }
        /// 
        /// 调整前海岛面积(公顷)
        /// 
        public double TZQ_HDMJ_GQ
        {
            get => tZQ_HDMJ_GQ; set
            {
                tZQ_HDMJ_GQ = value;
                RaisePropertyChanged("TZQ_HDMJ_GQ");
            }
        }
        /// 
        /// 调整后陆地面积
        /// 
        public double TZH_LDMJ
        {
            get => tZH_LDMJ; set
            {
                tZH_LDMJ = value;
                TZH_LDMJ_GQ = Math.Round(value * 0.0001, 2);
                RaisePropertyChanged("TZH_LDMJ");
            }
        }
        /// 
        /// 调整后陆地面积(公顷)
        /// 
        public double TZH_LDMJ_GQ
        {
            get => tZH_LDMJ_GQ; set
            {
                tZH_LDMJ_GQ = value;
                RaisePropertyChanged("TZH_LDMJ_GQ");
            }
        }
        /// 
        /// 调整后海岛面积
        /// 
        public double TZH_HDMJ
        {
            get => tZH_HDMJ; set
            {
                tZH_HDMJ = value;
                TZH_HDMJ_GQ = Math.Round(value * 0.0001, 2);
                RaisePropertyChanged("TZH_HDMJ");
            }
        }
        /// 
        /// 调整后海岛面积(公顷)
        /// 
        public double TZH_HDMJ_GQ
        {
            get => tZH_HDMJ_GQ; set
            {
                tZH_HDMJ_GQ = value;
                RaisePropertyChanged("TZH_HDMJ_GQ");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    /// 
    /// 地图范围
    /// 
    [Serializable]
    public class MapExtent
    {
        public double XMax { get; set; }
        public double YMax { get; set; }
        public double XMin { get; set; }
        public double YMin { get; set; }
    }
    public enum EnumProjType
    {
        NDBG,
        RCBG_ZJG,
        RCBG_BCGD,
        BHTBTQ,
        DTBJK,
        YCLJK,
        WYZS
    }
}