using ESRI.ArcGIS.Geometry;
using KGIS.Framework.AE;
using KGIS.Framework.DBOperator;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Utils.Interface;
using KGIS.Framework.Views;
using Kingo.Plugin.MapView.Common;
using Kingo.Plugin.MapView.Interface;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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;
namespace Kingo.Plugin.MapView.Views.LookPhoto
{
    /// 
    /// UCLookPhoto.xaml 的交互逻辑
    /// 
    public partial class UCLookPhoto : UserControl, IDockPanel2, INotifyPropertyChanged
    {
        public bool showDetail { get; set; }
        private int m_DataIndex { get; set; }
        private int m_DataCount { get; set; }
        public Action RowNext;
        ProjectInfo info = (MapsManager.Instance.CurrProjectInfo as ProjectInfo);
        string hcjgPath = string.Empty;
        string tbybh = string.Empty;
        public UCLookPhoto()
        {
            DevExpress.Xpf.Core.ThemeManager.SetTheme(this, DevExpress.Xpf.Core.Theme.Office2013LightGray);
            InitializeComponent();
            this.DockAreas = DockStyle.DockRight;
            this.FloatSize = new System.Drawing.Size(520, 800);
            this.DefaultArea = DockStyle.DockRight;
            this.ShowCloseButton = true;
            this.ShowAutoHideButton = false;
            this.DockWidth = 520;
            this.Title = "举证信息";
            hcjgPath = info.ProjDir + "//jz_hcjg.sqlite";
            ucImgXZZP.ChangedPointSymbol = ChangedPointSymbol;
        }
        private void ChangedPointSymbol(string fjmc)
        {
            EnvironmentVariables.Instance.cameraPoint.ChangedPointSymbol(fjmc);
        }
        Dictionary lstFj = null;
        List lstTBYBH = null;
        public void InitData(DataRow drSelect)
        {
            try
            {
                tbybh = drSelect["JCBH"].ToString();
                lblJCBH.Content = tbybh;
                lblWYBZ.Content = drSelect["BZ"].ToString();
                lblBGDL.Content = drSelect["BGDL"].ToString();
                DataTable dt = SQLiteDBOperate.Instance.ExceDataTable(info.DBPath, string.Format("select TBYBH,FJMC,XZB,YZB,Longitude,Latitude,PSJD,FJLX from FJ where TBYBH='{0}'", tbybh));
                DataTable dtHCJG = SQLiteDBOperate.Instance.ExceDataTable(hcjgPath, string.Format("select * from JZHCJG where TBYBH='{0}'", tbybh));
                if (dtHCJG != null && dtHCJG.Rows.Count > 0)
                {
                    txtHCJG.Text = dtHCJG.Rows[0]["HCJG"].ToString();
                }
                else
                {
                    txtHCJG.Text = "";
                }
                DataTable dtCheck = SQLiteDBOperate.Instance.ExceDataTable(hcjgPath, string.Format("select * from JZZP where TBYBH='{0}'", tbybh));
                if (dt != null && dt.Rows.Count > 0)
                {
                    List lstAttach = new List();
                    ImageView.AttachEntity entity = null;
                    lstFj = new Dictionary();
                    lstTBYBH = new List();
                    foreach (DataRow dr in dt.Rows)
                    {
                        entity = new ImageView.AttachEntity();
                        if (lstFj.ContainsKey(dr["FJMC"].ToString()))
                        {
                            continue;
                        }
                        lstFj.Add(dr["FJMC"].ToString(), "");
                        if (dtCheck != null && dtCheck.Rows.Count > 0)
                        {
                            DataRow[] drSet = dtCheck.Select(string.Format(" FJMC='{0}' ", dr["FJMC"].ToString()));
                            if (drSet != null && drSet.Length > 0)
                            {
                                entity.isCheck = true;
                            }
                        }
                        if (!lstTBYBH.Contains(dr["TBYBH"].ToString()))
                            lstTBYBH.Add(dr["TBYBH"].ToString());
                        entity.FJMC = dr["FJMC"].ToString();
                        entity.AttachLocationPath = info.ProjDir + "\\Picture\\" + dr["FJMC"].ToString();
                        double xzb, yzb;
                        if (double.TryParse(dr["Longitude"].ToString(), out xzb) && double.TryParse(dr["Latitude"].ToString(), out yzb))
                        {
                            IPoint mapPointC = new PointClass() { X = xzb, Y = yzb };
                            if (mapPointC.X != 0)
                            {
                                ISpatialReferenceFactory NewSpatialReference = new SpatialReferenceEnvironmentClass();
                                mapPointC.SpatialReference = NewSpatialReference.CreateGeographicCoordinateSystem(4490);
                                //创建图层
                                ISpatialReference spatiaReference = GeoDBAPI.CreteSpatialReference(info.PrjFileStr);
                                mapPointC.Project(spatiaReference);
                                entity.X = mapPointC.X;
                                entity.Y = mapPointC.Y;
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(NewSpatialReference);
                                NewSpatialReference = null;
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(mapPointC);
                                mapPointC = null;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (dr["XZB"] != DBNull.Value)
                            {
                                entity.X = Convert.ToDouble(dr["XZB"]);
                            }
                            if (dr["YZB"] != DBNull.Value)
                            {
                                entity.Y = Convert.ToDouble(dr["YZB"]);
                            }
                        }
                        if (dr["PSJD"] != DBNull.Value)
                        {
                            entity.Angle = Convert.ToDouble(dr["PSJD"]);
                        }
                        if (dr["FJLX"].ToString() != "V")
                            lstAttach.Add(entity);
                    }
                    ExportPic(lstTBYBH, info.DBPath);
                    EnvironmentVariables.Instance.cameraPoint.AddCameraPoint((object)lstAttach as List