﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

#if NCAD
    using Teigha.DatabaseServices;
    using Teigha.Runtime;
    using Teigha.Geometry;
    using HostMgd.ApplicationServices;
    using HostMgd.EditorInput;
#else
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
#endif

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(CadPrototypesDesigner.Commands))]

namespace CadPrototypesDesigner
{
    public static partial class Commands
    {
        [CommandMethod("proto")]
        public static void ProtoCommand()
        {
            Global.paletteSet.show();
        }

        private static IEnumerator<string> makeCommands = null;

        [CommandMethod("protomake", CommandFlags.Session)]
        public static void ProtoMake()
        {
            // new building
            if (makeCommands == null)
            {
                if (!drawingSaved())
                {
                    Global.print("Current document not saved");
                    return;
                }
                if ( Application.DocumentManager.Count > 1 )
                {
                    Global.print("Lot of docs");
                    return;
                }
                string CurrentDirectory = Global.CurrentDirectory;
                if (CurrentDirectory == "")
                {
                    Global.print("Current document not saved");
                    return;
                }
                string makefile = System.IO.Path.Combine(CurrentDirectory, "makefile.cmd");
                if (!System.IO.File.Exists(makefile))
                {
                    Global.print("makefile.cmd not found");
                    return;
                }
                Parser p = new Parser(makefile);
                PathStackSet targets = p.parse();
                Builder builder = new Builder(targets, CurrentDirectory);
                makeCommands = builder.build();
                ProtoMake();
                return;
            }
            // continue building
            if (makeCommands.MoveNext())
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                string command = makeCommands.Current;
                command += "\n\x03\x03\x03\x03protomake\n";
                doc.SendStringToExecute(command, false, false, true);
                return;
            }
            // end building
            makeCommands = null;
            if (Builder.lastBuildedDocument != null 
                && Builder.lastBuildedDocument != ""
                && System.IO.File.Exists(Builder.lastBuildedDocument)
            )
            {
                Application.DocumentManager.MdiActiveDocument = Application.DocumentManager.Open(Builder.lastBuildedDocument, true);
            }
            Global.print("THE END");
        }

//        [LispFunction("protoInvokeNextBuildCommand")]
//        public static object ProtoBuild(ResultBuffer rbArgs)
//        {
//            return "hello";
//            
//            return "THE END";
//        }

        [CommandMethod("protocompile")]
        public static void CompileCommand()
        {
            Global.print("Command not implemented");
        }

        [CommandMethod("-protocompile")]
        public static void CompileCommandConsoleMode()
        {
            Global.print("Command not implemented");
        }

        private static bool drawingSaved()
        {
            #if NCAD
                return true;
            #endif
            // Check the value of DBMOD, if 0 then the drawing has no unsaved changes
            object obj = Application.GetSystemVariable("DBMOD");
            if (System.Convert.ToInt16(obj) == 0) { return true; }
            return false;
        }
    }
}
