using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
namespace Kingo.Plugin.MapView.Views.GoToXYUi
{
    /// 
    /// GoToXYUi.xaml 的交互逻辑
    /// 
    public partial class GoToXYUi : BaseWindow
    {
        private static GoToXYUi instance;
        public static GoToXYUi Instance(object Helpers)
        {
            if (instance == null)
                instance = new GoToXYUi(Helpers);
            else
            {
                instance.m_hookHelper = Helpers as IHookHelper;
            }
            return instance;
        }
        private IHookHelper m_hookHelper;//获取地图控件和主窗体
        public GoToXYUi(object Helpers)
        {
            try
            {
                if (this.m_hookHelper == null)
                {
                    this.m_hookHelper = Helpers as IHookHelper;
                }
            }
            catch (Exception ex)
            {
                //LogAPI.Debug("转到XY异常!" + ex.Message);
                LogAPI.Debug("初始化 转到XY页面 时异常,异常信息如下:");
                LogAPI.Debug(ex);
                LogAPI.Debug("初始化 转到XY页面 时异常信息结束");
            }
            InitializeComponent();
        }
        /// 
        /// 转到XY轴点击事件
        /// 
        /// 
        /// 
        private void Btn_GotoXy_Click(object sender, RoutedEventArgs e)
        {
            GotoXy();
        }
        /// 
        /// 定位XY方法封装
        /// 
        public void GotoXy()
        {
            try
            {
                //非空验证
                if ((Txt_X.Text).Trim() == "" || (Txt_Y.Text).Trim() == "")
                {
                    MessageHelper.Show("X或Y轴为空,请重新输入!");
                    return;
                }
                Double latitude = Double.Parse(Txt_X.Text);//获取输入的X轴坐标
                Double longitude = Double.Parse(Txt_Y.Text);//获取输入的Y轴坐标
                IPoint point = new PointClass();
                IGeometry geometry;//声明一个几何
                point.X = latitude;
                point.Y = longitude;
                geometry = (IGeometry)point;//向几何中添加一个点
                IArray geoArray = new ArrayClass();//声明一个几何的集合
                geoArray.Add(geometry);//向几何集合中添加几何对象
                IHookActions hookActions = (IHookActions)m_hookHelper;//通过IHookActions闪烁要素集合
                hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsPan);
                System.Windows.Forms.Application.DoEvents();
                m_hookHelper.ActiveView.ScreenDisplay.UpdateWindow();
                ((m_hookHelper.Hook) as IMapControl2).CenterAt(point);
                hookActions.DoActionOnMultiple(geoArray, esriHookActions.esriHookActionsFlash);
            }
            catch (Exception ex)
            {
                //LogAPI.Debug(ex.Message);
                LogAPI.Debug("执行 定位XY方法 时异常,异常信息如下:");
                LogAPI.Debug(ex);
                LogAPI.Debug("执行 定位XY方法 时异常信息结束");
            }
        }
        /// 
        /// 设置XY的值
        /// 
        /// x轴的值
        /// y轴的值
        public void SetXYValue(int x, int y)
        {
            Txt_X.Text = x.ToString();
            Txt_Y.Text = y.ToString();
        }
        /// 
        /// 窗口关闭事件
        /// 
        /// 
        /// 
        //public Action Closeds;
        private void BaseWindow_Closed(object sender, EventArgs e)
        {
            //this.Closeds();
            instance = null;
            this.Close();
        }
        /// 
        /// 验证Y轴输入是否合法
        /// 
        /// 
        /// 
        private void Txt_Y_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex re = new Regex("[^0-9.-]+");
                e.Handled = re.IsMatch(e.Text);
            }
            catch (Exception ex)
            {
                //LogAPI.Debug(ex.Message);
                LogAPI.Debug("验证Y轴输入是否合法 时异常,异常信息如下:");
                LogAPI.Debug(ex);
                LogAPI.Debug("验证Y轴输入是否合法 时异常信息结束");
            }
        }
        /// 
        ///  验证X轴输入是否合法
        /// 
        /// 
        /// 
        private void Txt_X_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex re = new Regex(@"[^0-9.-]+");
                e.Handled = re.IsMatch(e.Text);
            }
            catch (Exception ex)
            {
                //LogAPI.Debug(ex.Message);
                LogAPI.Debug("验证X轴输入是否合法 时异常,异常信息如下:");
                LogAPI.Debug(ex);
                LogAPI.Debug("验证X轴输入是否合法 时异常信息结束");
            }
        }
    }
}