
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;

using GMLC01HerramientasCad.Logica;
using COMUN = GMLC01HerramientasCad.Logica;
using System;

namespace GMLC01HerramientasCad.Acad
{
    public class PLine3DJigger : DrawJig, System.IDisposable
    {  // local members  
        private Point3d mPoint;
        private Point3dCollection mPoints;

        private bool pIsUndoing;
        private string deshacer;

        //-----------------------------------------
        public Point3dCollection Coordinates
        {
            get
            { return mPoints; }
        }
        //-------------------------------------------------------------------
        public PLine3DJigger()
        {
            try
            {
                mPoints = new Point3dCollection();
                deshacer = COMUN.Properties.Resources.SiguientePuntoODeshacer.Split('[')[1].Split(']')[0];
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new NotImplementedException("", ex);
            }
        }
        //-------------------------------------------------------------------

        public int DoPolyLineDrawJigmojo()
        {
            try
            {
                return IntDoPolyLineDrawJigmojo();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new NotImplementedException("", ex);
            }
        }
        private int IntDoPolyLineDrawJigmojo()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptResult jigResult = null;
            bool iContinue = true;
            do
            {
                jigResult = ed.Drag(this);
                if (jigResult.Status == PromptStatus.OK)
                {
                    mPoints.Add(mPoint);
                }
                else if (jigResult.Status == PromptStatus.Keyword)
                {
                    if (jigResult.StringResult == deshacer)
                    {
                        if (mPoints.Count > 0)
                        {
                            RemoveDummyVertex();
                        }
                    }
                }
                else if (jigResult.Status == PromptStatus.None)
                {
                    break;
                }
                else if (jigResult.Status == PromptStatus.Cancel)
                {
                    mPoints.Clear();
                    break;
                }
            }
            while (iContinue);
            return mPoints.Count;
        }
        //-------------------------------------------------------------------
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOptions = new JigPromptPointOptions();
            jigOptions.UserInputControls =
              (UserInputControls.Accept3dCoordinates |
              UserInputControls.NullResponseAccepted |
              UserInputControls.NoNegativeResponseAccepted
              );

            pIsUndoing = false;

            if (mPoints.Count == 0)
            {
                // For the first vertex, just ask for the point
                jigOptions.Message =
                  "\n" + COMUN.Properties.Resources.PuntoInicialDeLaLinea + " ";
            }
            else
            {
                jigOptions.SetMessageAndKeywords(
                  "\n" + COMUN.Properties.Resources.SiguientePuntoODeshacer + " ",
                  deshacer);
            }

            PromptPointResult jigPointResult = prompts.AcquirePoint(jigOptions);

            if (jigPointResult.Status == PromptStatus.Keyword)
            {
                return SamplerStatus.OK;
            }
            else if (jigPointResult.Status == PromptStatus.OK)
            {
                Point3d pt = jigPointResult.Value;
                if (pt == mPoint)
                {
                    return SamplerStatus.NoChange;
                }
                mPoint = pt;
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }
        //-------------------------------------------------------------------
        public bool IsUndoing()
        {
            try
            {
                return pIsUndoing;
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new NotImplementedException("", ex);
            }
        }
        //-------------------------------------------------------------------
        public void RemoveDummyVertex()
        {
            try
            {
                mPoints.RemoveAt(mPoints.Count);
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new NotImplementedException("", ex);
            }
        }
        //-------------------------------------------------------------------
        protected override bool WorldDraw(WorldDraw draw)
        {
            if (mPoints.Count == 0)
            {
                return true;
            }
            Point3dCollection vertices = new Point3dCollection();
            foreach (Point3d point in mPoints)
            {
                vertices.Add(point);
            }
            vertices.Add(mPoint);

            draw.Geometry.Polyline(vertices, Vector3d.ZAxis, new System.IntPtr(0)); 

            return true;
        }
        //-------------------------------------------------------------------
        #region Miembros de IDisposable

        public void Dispose()
        {
            this.Dispose(true);
            System.GC.SuppressFinalize(this);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.mPoints != null) this.mPoints.Dispose();
            }
        }

        #endregion
    }
}
