using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using KGIS.Framework.Utils.Enum;
using System.Drawing;
using System.Windows.Forms;
namespace Kingo.Plugin.EngineEditor
{
    /// 
    /// 定义工具命令接口
    /// 
    public interface IToolCommand : IBaseCommand, ICommand, ITool
    {
        object CurrentTool { get; set; }
    }
    /// 
    /// 定义抽象工具类
    /// 
    public abstract class BaseToolCommand : IToolCommand
    {
        public IGeometry CheckGeometry { get; set; }
        private ControlsEditingSketchToolClass editSketchTool = new ControlsEditingSketchToolClass();
        public abstract enumProductType AttachProductType
        {
            get;
        }
        public enumProductType CurrentProduct
        {
            get;
        }
        public object hook
        {
            get;
            set;
        }
        public object CurrentTool
        {
            get;
            set;
        }
        #region BaseTool
        public virtual int Cursor
        {
            get { return CurrentTool is ITool ? (CurrentTool as ITool).Cursor : m_cursor.Handle.ToInt32(); }
        }
        public virtual bool Deactivate()
        {
            if (!(this.CurrentTool is ControlsEditingSketchToolClass))
            {
                editSketchTool.Deactivate();
            }
            if (CurrentTool is ITool)
            {
                (CurrentTool as ITool).Deactivate();
            }
            m_checked = false;
            CurrentTool = null;
            return true;
        }
        public virtual bool OnContextMenu(int x, int y)
        {
            return CurrentTool is ITool ? (CurrentTool as ITool).OnContextMenu(x, y) : false;
        }
        public virtual void OnDblClick()
        {
            //if (!base.ValidateAuthorize(this.CheckGeometry))
            //{
            //    throw new Exception("绘制图形不在授权区域范围内!");
            //}
            if (CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnDblClick();
            }
        }
        public virtual void OnKeyDown(int keyCode, int shift)
        {
            //判断当前工具是否是ControlsEditingSketchToolClass,如果不是,则调用editSketchTool的相应事件,以实现GIS原生功能
            //此处判断是为了避免重复调用
            if (!(this.CurrentTool is ControlsEditingSketchToolClass) && editSketchTool.Enabled)
            {
                editSketchTool.OnKeyDown(keyCode, shift);
            }
            if (this.CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnKeyDown(keyCode, shift);
            }
        }
        public virtual void OnKeyUp(int keyCode, int shift)
        {
            //判断当前工具是否是ControlsEditingSketchToolClass,如果不是,则调用editSketchTool的相应事件,以实现GIS原生功能
            //此处判断是为了避免重复调用
            if (!(this.CurrentTool is ControlsEditingSketchToolClass) && editSketchTool.Enabled)
            {
                editSketchTool.OnKeyUp(keyCode, shift);
            }
            if (this.CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnKeyUp(keyCode, shift);
            }
        }
        public virtual void OnMouseDown(int button, int shift, int x, int y)
        {
            if (this.CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnMouseDown(button, shift, x, y);
            }
        }
        public virtual void OnMouseMove(int button, int shift, int x, int y)
        {
            //判断当前工具是否是ControlsEditingSketchToolClass,如果不是,则调用editSketchTool的相应事件,以实现GIS原生功能
            //此处判断是为了避免重复调用
            if (!(this.CurrentTool is ControlsEditingSketchToolClass) && editSketchTool.Enabled)
            {
                editSketchTool.OnMouseMove(button, shift, x, y);
            }
            if (this.CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnMouseMove(button, shift, x, y);
            }
        }
        public virtual void OnMouseUp(int button, int shift, int x, int y)
        {
            if (this.CurrentTool is ITool)
            {
                if ((CurrentTool as ICommand).Enabled)
                    (this.CurrentTool as ITool).OnMouseUp(button, shift, x, y);
            }
        }
        public virtual void Refresh(int hdc)
        {
            if (this.CurrentTool is ITool)
                (this.CurrentTool as ITool).Refresh(hdc);
        }
        #endregion
        protected Bitmap m_bitmap;
        protected string m_caption;
        protected string m_category;
        protected bool m_checked;
        protected bool m_enabled;
        protected string m_helpFile;
        protected int m_helpID;
        protected string m_message;
        protected string m_name;
        protected string m_toolTip;
        protected Cursor m_cursor = Cursors.Arrow;
        #region BaseCommand
        public virtual int Bitmap
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Bitmap : 0; }
        }
        public virtual string Caption
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Caption : m_caption; }
        }
        public virtual string Category
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Category : m_category; }
        }
        public virtual bool Checked
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Checked : m_checked; }
            //get { return (CurrentTool is IToolCommand ? (CurrentTool as IToolCommand).Checked:m_checked)||(CurrentTool is ICommand ? (CurrentTool as ICommand).Checked : m_checked); }
        }
        public virtual bool Enabled
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Enabled : m_enabled; }
        }
        public virtual int HelpContextID
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).HelpContextID : m_helpID; }
        }
        public virtual string HelpFile
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).HelpFile : m_helpFile; }
        }
        public virtual string Message
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Message : m_message; }
        }
        public virtual string Name
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Name : m_name; }
        }
        public virtual void OnClick()
        {
            //判断当前工具是否是ControlsEditingSketchToolClass,如果不是,则调用editSketchTool的相应事件,以实现GIS原生功能
            //此处判断是为了避免重复调用
            if (!(this.CurrentTool is ControlsEditingSketchToolClass))
            {
                editSketchTool.OnClick();
            }
            if (CurrentTool is ICommand)
                (CurrentTool as ICommand).OnClick();
            m_checked = true;
        }
        public virtual void OnCreate(object hook)
        {
            //判断当前工具是否是ControlsEditingSketchToolClass,如果不是,则调用editSketchTool的相应事件,以实现GIS原生功能
            //此处判断是为了避免重复调用
            if (!(this.CurrentTool is ControlsEditingSketchToolClass))
            {
                editSketchTool.OnCreate(hook);
            }
            if (CurrentTool is ICommand)
                (CurrentTool as ICommand).OnCreate(hook);
        }
        public virtual string Tooltip
        {
            get { return CurrentTool is ICommand ? (CurrentTool as ICommand).Tooltip : m_toolTip; }
        }
        #endregion
        IPoint m_SnapPoint = null;
        bool m_isHasSnap;
        public void SnapPoint(IActiveView activeView, IPoint inPoint, IEngineSnapEnvironment m_editEnvironment)
        {
            if (this.m_isHasSnap && (this.m_SnapPoint != null))
            {
                this.DrawSnapPoint(activeView, this.m_SnapPoint);
            }
            this.m_isHasSnap = ((IEngineSnapEnvironment)m_editEnvironment).SnapPoint(inPoint);
            if (this.m_isHasSnap)
            {
                this.m_SnapPoint = inPoint;
                this.DrawSnapPoint(activeView, this.m_SnapPoint);
            }
        }
        private void DrawSnapPoint(IActiveView activeView, IPoint point)
        {
            IScreenDisplay screenDisplay = activeView.ScreenDisplay;
            screenDisplay.StartDrawing(screenDisplay.hDC, -1);
            IEngineEditProperties engProperties = new EngineEditorClass();
            screenDisplay.SetSymbol((ISymbol)engProperties.SnapSymbol);
            screenDisplay.DrawPoint(point);
            screenDisplay.FinishDrawing();
            screenDisplay = null;
        }
    }
    public interface IBaseCommand
    {
        /// 
        /// 当前Command所属的模块
        /// 
        enumProductType AttachProductType { get; }
        /// 
        /// 当前有效模块
        /// 
        enumProductType CurrentProduct { get; }
        object hook { set; get; }
    }
}