namespace HMS_MiCom2022_MACEXPERT
{
    internal class HMS_MiCom2022_MACEXPERT
    {
        static void Main()
        {
            string[] Arg = System.Environment.GetCommandLineArgs();
            string ComName = "COM19";
            int status;

            Display("HMS_MiCom2022_MACEXPERT.exe   ==> how to use MiCom_v2022 for MAC23/MAC34 with MODE EXPERT commands");
            Display();
            Display("ModeExpert documentation :");
            Display("https://www.midi-ingenierie.com/documentation/ressources/manuels_utilisateurs/mac23_34_ex_v5_um_fr.pdf");
            Display();
            Display("Usage : HMS_MiCom2022_MACEXPERT \"comName\"  ");
            
            // contrôle / recherche du COMPORT sur la ligne d'appel de l'éxécutable
            if (Arg.Length == 1)
            {
                Display("no com name speficied in the argument list , so "+ ComName + " is use");
            }
            else
            {
                ComName = Arg[1];
                Display(ComName + " will be use as COM");
            }

            // usage du myCom en tant qu'instance du type MiCom_v2022
            // the MiCom_v2022.dll assembly is referenced in the HMS_MiCom2022_MACEXPERT project:
            // so the next line is valid and 'myCom' will be the instance of the MiCom_v2022 type
            Midi_Ingenierie.MiComm_2022 myCom = new Midi_Ingenierie.MiComm_2022();
            Display();
            Display("myCom is the instance of MiCom_v2022 type , from C# new Midi_Ingenierie.MiComm_2022() ");
            Display();

            // identification de l'assembly
            Display("Check version and copyright : ");
            Display("MiComm_2022.Identity := " +  Midi_Ingenierie.MiComm_2022.Identity );
            Display();

            // initialisation d'une ligne de com en mode EXPERT
            Display("Open Com Line :");
            Display("myCom.Open(\"MACEXPERT\",\"" + ComName + "\",\"38400\")");
            status = myCom.Open("MACEXPERT", ComName, "38400");
            //status MUST be 0x0000
            if (status != 0)
            {
                Display("Status :" + status.ToString("X4"));
                Display("Bad Status !! check ComPort");
                Quit();
                return;
            }

            //commande standart vs commande mode EXPERT
            //00mr  page 5 de la doc
            Display(); 
            Display("INITIALISATION module  index =00h ==>myCom.Send(\"00000000000000\")");
            myCom.Send("00000000000000");
            status = myCom.Status;
            if (status > 0x8000)
            {
                Display("Status :" + status.ToString("X4"));
                Display("Bad Status !! check Com , alimentation, connection ....");
                Quit();
                return;
            }

            System.ConsoleKey key_ = Ask("Start motor, only if motor is FREE  [Y/N] ?");
            if (key_ == System.ConsoleKey.Y)
            {
                Display();

                //00gi 20  page 9
                Display("LIMITATION du COUPLE  index =0Ch ==> myCom.Send(\"000C000000001A\")");
                myCom.Send("000C000000001A");

                //00di     page 8
                Display("RAZ POSITION   index =0Bh ==> myCom.Send(\"000B0000000000\")");
                myCom.Send("000B0000000000");

                //00gf 3000  page 13
                Display("MVT à 300tr/mn index =15h ==> myCom.Send(\"00150000000320\")");
                myCom.Send("00150000000320");
                System.Threading.Thread.Sleep(100);

                //00gf 4000
                Display("MVT à 400tr/mn index =15h ==> myCom.Send(\"00150000000258\")");
                myCom.Send("00150000000258");
                System.Threading.Thread.Sleep(100);

                //00gf 2500
                Display("MVT à 250tr/mn index =15h ==> myCom.Send(\"001500000003C0\")");
                myCom.Send("001500000003C0");
                System.Threading.Thread.Sleep(100);

                //00gf 3000
                Display("MVT à 300tr/mn index =15h ==> myCom.Send(\"00150000000320\")");
                myCom.Send("00150000000320");
                System.Threading.Thread.Sleep(100);

                //00gf 4000
                Display("MVT à 400tr/mn index =15h ==> myCom.Send(\"00150000000258\")");
                myCom.Send("00150000000258");
                System.Threading.Thread.Sleep(100);

                //00gf 2500
                Display("MVT à 250tr/mn index =15h ==> myCom.Send(\"001500000003C0\")");
                myCom.Send("001500000003C0");
                System.Threading.Thread.Sleep(100);

                //00gf 4000
                Display("MVT à 400tr/mn index =15h ==> myCom.Send(\"00150000000258\")");
                myCom.Send("00150000000258");
                System.Threading.Thread.Sleep(100);

                //00ge  page 14
                Display("DéCéLéRATION   index =17h ==> myCom.Send(\"00170000000000\")");
                myCom.Send("00170000000000");
                System.Threading.Thread.Sleep(1000);

                //00gr  page 15
                Display("POWER OFF      index =1Ah ==> myCom.Send(\"001A0000000000\")");
                myCom.Send("001A0000000000");
            }
            Quit();
        }

        private static void Delay(int delay)           => System.Threading.Thread.Sleep(delay);
        private static void Display(string Texte = "") => System.Console.WriteLine(Texte);
        private static void Quit()                     => Ask("[ESC] = Quit the program");
        private static System.ConsoleKey Ask(string Texte) { 
          System.Console.Write(System.Environment.NewLine+Texte);
          return System.Console.ReadKey().Key;
        }
    }
}
