using System;
using System.Collections.Generic;
using System.Text;

using GMLC01HerramientasCad.Logica;

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

namespace GMLC01HerramientasCad.Acad
{
    class Texts
    {

        private Texts()
        {
        }

        /// <summary>
        /// Verifica la existencia de un estilo de fuente y lo crea si no existiese
        /// </summary>
        /// <param name="criterio"></param>
        /// <returns></returns>
        public static void VerifyFont(CriterioEtiquetado criterio)
        {
            //string fontName = "Standard";
            string estilo = "";
            if (criterio.Negrita) estilo += "B";
            if (criterio.Cursiva) estilo += "C";
            if (criterio.Subrayado) estilo += "U";
            //criterio.Estilo = System.Drawing.FontStyle
            string fontName = (criterio.Font + "_" + criterio.Altura + "_" + estilo).Replace(",", ".");

            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TextStyleTable lt = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                if (!lt.Has(fontName))
                {
                    TextStyleTableRecord tst = new TextStyleTableRecord();
                    tst.Name = fontName;

                    Autodesk.AutoCAD.GraphicsInterface.FontDescriptor stFont;
                    stFont = tst.Font;
                    Autodesk.AutoCAD.GraphicsInterface.FontDescriptor newFont;
                    newFont = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(criterio.Font, criterio.Negrita, criterio.Cursiva, stFont.CharacterSet, stFont.PitchAndFamily);

                    tst.Font = newFont;
                    lt.UpgradeOpen();
                    lt.Add(tst);
                    trans.AddNewlyCreatedDBObject(tst, true);
                    trans.Commit();
                }
            }
            // Para que el estilo se aplique a los nuevos textos, se pone como estilo activo.
            Application.SetSystemVariable("TEXTSTYLE", fontName);
        }

        public static void InsertText(string text, Point3d insertionPoint, CriterioEtiquetado criterio)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Autodesk.AutoCAD.ApplicationServices.TransactionManager tm = doc.TransactionManager;
            using (Transaction trans = tm.StartTransaction())
            {
                using (DocumentLock docLoc = doc.LockDocument())
                {

                    try
                    {
                        // Insert the text into modelspace.
                        BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
                        BlockTableRecord msBTR = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);

                        // Create the text, set it to right align.
                        DBText tempText = new DBText();
                        tempText.TextString = text;
                        tempText.Color = Autodesk.AutoCAD.Colors.Color.FromColor(criterio.Color);
                        tempText.Height = criterio.Altura;
                        tempText.Position = insertionPoint;
                        //if (criterio.Cursiva) tempText.Oblique = 25;
                        tempText.HorizontalMode = TextHorizontalMode.TextCenter;
                        //tempText.VerticalMode = TextVerticalMode.TextBase;
                        tempText.VerticalMode = TextVerticalMode.TextBottom;
                        tempText.AlignmentPoint = insertionPoint;

                        // Add the text to modelspace.
                        msBTR.AppendEntity(tempText);
                        trans.AddNewlyCreatedDBObject(tempText, true);

                        tm.QueueForGraphicsFlush();
                        tm.FlushGraphics();
                        doc.Editor.UpdateScreen();
                        trans.Commit();

                    }
                    finally
                    {
                        trans.Dispose();
                    }
                }
            }
        }
    }
}
