using System;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Enum;
namespace Kingo.Plugin.EngineEditor.Commands.Tools
{
    public class SelectFwjCmd : BaseToolCommand
    {
        private IHookHelper m_hookHelper;
        private EngineEditorClass m_editor = new EngineEditorClass();
        INewEnvelopeFeedback pFeedBack = null;
        IGeometry pGeometry = null;
        bool IsDrawing = false;
        public override enumProductType AttachProductType
        {
            get { return enumProductType.KDB | enumProductType.KAP; }
        }
        public override void OnCreate(object hook)
        {
            try
            {
                if (hook == null)
                {
                    return;
                }
                m_hookHelper = new HookHelperClass() { Hook = hook };
                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                    return;
                }
            }
            catch
            {
                m_hookHelper = null;
            }
        }
        public override void OnClick()
        {
            pFeedBack = new NewEnvelopeFeedbackClass();
            pFeedBack.Display = m_hookHelper.ActiveView.ScreenDisplay;
            this.OnCreate(base.hook);
        }
        IPoint startPoint = null;
        public override void OnMouseDown(int button, int shift, int x, int y)
        {
            try
            {
                if (button != 1)
                {
                    return;
                }
                if (IsDrawing == false)
                {
                    pFeedBack = new NewEnvelopeFeedbackClass();
                    pFeedBack.Display = m_hookHelper.ActiveView.ScreenDisplay;
                    startPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                    pFeedBack.Start(startPoint);
                    IsDrawing = true;
                }
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
            }
        }
        public override void OnMouseMove(int button, int shift, int x, int y)
        {
            try
            {
                if (pFeedBack == null)
                {
                    return;
                }
                IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                pFeedBack.MoveTo(pPoint);
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
            }
        }
        public override void OnMouseUp(int button, int shift, int x, int y)
        {
            try
            {
                IsDrawing = false;
                IEnvelope pEnvelope = pFeedBack.Stop();
                if (pEnvelope.IsEmpty)
                {
                    Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { MsgType = "SelectFWJ", Content = new PointClass() { X = x, Y = y } });
                }
                else
                {
                    pGeometry = GetPolygon(pEnvelope.Envelope.XMin, pEnvelope.Envelope.XMax, pEnvelope.Envelope.YMin, pEnvelope.Envelope.YMax);
                    pFeedBack = null;
                    Platform.Instance.SendMsg(new KGIS.Framework.Utils.Interface.NotifyMsgPackage() { MsgType = "SelectFWJ", Content = pGeometry });
                }
                base.OnMouseUp(button, shift, x, y);
            }
            catch (Exception ex)
            {
                LogAPI.Debug(ex);
            }
        }
        public override bool Enabled
        {
            get
            {
                ESRI.ArcGIS.Carto.ILayer layer = (m_editor as EngineEditorClass).TargetLayer;
                if (m_editor.EditState == esriEngineEditState.esriEngineStateEditing)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// 
        /// 切换工具,释放关闭窗体
        /// 
        /// 
        public override bool Deactivate()
        {
            if (pGeometry != null)
            {
                ComReleaser.ReleaseCOMObject(this.pGeometry);
            }
            pFeedBack = null;
            IsDrawing = false;
            return base.Deactivate();
        }
        public override int Cursor
        {
            get
            {
                return base.Cursor;
            }
        }
        private IPolygon GetPolygon(double XMin, double XMax, double YMin, double YMax)
        {
            IPolygon pPolygon = new PolygonClass();
            IPointCollection pPointCollection = pPolygon as IPointCollection;
            IPoint point1 = new PointClass();
            point1.PutCoords(XMin, YMin);
            pPointCollection.AddPoint(point1);
            IPoint point2 = new PointClass();
            point2.PutCoords(XMin, YMax);
            pPointCollection.AddPoint(point2);
            IPoint point3 = new PointClass();
            point3.PutCoords(XMax, YMax);
            pPointCollection.AddPoint(point3);
            IPoint point4 = new PointClass();
            point4.PutCoords(XMax, YMin);
            pPointCollection.AddPoint(point4);
            IPoint point5 = new PointClass();
            point5.PutCoords(XMin, YMin);
            pPointCollection.AddPoint(point5);
            pPolygon = pPointCollection as IPolygon;
            pPolygon.Close();
            return pPolygon;
        }
    }
}