using System;
using System.IO.Ports;
using CPF;
using System.Threading;
using CPFUnitTests.PowerSwitchBoard;
using GHI.IO.Storage;
using HWModules;
using MFUnitTest;
using SWModules;
using System.Text;
using CPFUnitTests.Core;
using GHI.Processor;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;
using SensorModules;

namespace CPFUnitTests
{
    class Program
    {

        //public static BTConsole btConsole;
        //public static StructQueue sQueue = new StructQueue();
        public static StateBase[] cpfState;

        private static HWModules.MotherBoard mbBoard;
        private static HWModules.LheBoard lheBoard;
        private static CpfConsole cpfConsole;
        private static SDStorage sdStorage;


        //Main Function 
        public static void Main()
        {
            #region SIM_TESTS
            //TestManager.RunTest(typeof(MissionConfigUT));
            #endregion

           
            #region PROGRAMMING_DELAY
            Thread.Sleep(15000);
            mbBoard = MotherBoard.Instance;
            mbBoard.Enable5V();
            //mbBoard.WaitForLdr0();
            #endregion

            #region INITIALIZATION

            cpfState = new StateBase[(int)CPFStates.cpfStatesLength];
            Initialize();

            EngrLogger.init(new StringBuilder("UnitTest"));

            // File System Check
            if (FileSystemCheck.CheckSDCard())
                EngrLogger.Comment("File Check Passed.");
            else
                EngrLogger.Comment("File Check Failed.");

            // NonEssentialInitialize();
            lheBoard = LheBoard.Instance;
            lheBoard.EnablePumpPower(false);
            #endregion

            #region HW_TESTS
            //TestManager.RunTest(typeof(UT_Isb_IO));
            //TestManager.RunTest(typeof(UnitTest_BoschPTH));
            //TestManager.RunTest(typeof(LheBoardUnitTests), "TestLheBatteryComms");
            //TestManager.RunTest(typeof(LheBoardUnitTests), "TestLhePumpPowerAndEnergyMonitor");
            //TestManager.RunTest(typeof(ElmoTwitterUnitTest));
            //TestManager.RunTest(typeof(PumpMotorUnitTests));
            //TestManager.RunTest(typeof(LheBoardUnitTests), "TestSystemEnergyMonitors");
            //TestManager.RunTest(typeof(SBDUnitTests), "TestCreateSBDMessage");
            //TestManager.RunTest(typeof(CTDUnitTests), "TestFP");
            //TestManager.RunTest(typeof(GPSUnitTests));
            #endregion


            #region FULL_CHECKOUT
            //mbBoard.Disable12V();
            //mbBoard.Disable5V();
            //var checkout = new FullHardwareCheckout();
            //checkout.RunAllTests();
            #endregion


            #region APPLICATION_TESTS
            //TestManager.RunTest(typeof(UnitTest_MotherBoard_EnergyMonitor));
            //TestManager.RunTest(typeof(UnitTestIridiumRudicsRoboCaller));
            //TestManager.RunTest(typeof(UnitTestIridiumRUDICS));
            //TestManager.RunTest(typeof(UnitTestFileTransferWithStateMachine));
            //TestManager.RunTest(typeof(UnitTest_MotherBoard_uSD));
            //TestManager.RunTest(typeof(UnitTest_MotherBoard_EnergyMonitor));
            //TestManager.RunTest(typeof(UT_MotherBoard.UnitTest_MotherBoard_GPS));
            //TestManager.RunTest(typeof(UnitTest_MotherBoard_I2CSlots));
            //TestManager.RunTest(typeof(UnitTestIridium));
            //TestManager.RunTest(typeof(UT_BuoyancyEngine.BEUnitTests),"TestFullExtendRetract");
            //TestManager.RunTest(typeof(PumpEffTests));
            //TestManager.RunTest(typeof(MotorizedValveUnitTests));
            //TestManager.RunTest(typeof(PumpMotorUnitTests), "IncreaseBuoyancy");
            //TestManager.RunTest(typeof(PumpMotorUnitTests), "DecreaseBuoyancy");
            //TestManager.RunTest(typeof(CTDUnitTests));
            //TestManager.RunTest(typeof(OptodeUnitTests));
            //TestManager.RunTest(typeof(AscendUT));
            //TestManager.RunTest(typeof(SimpleProfileUT));
            //TestManager.RunTest(typeof(UnitTest_PSB_SN1),"TestNativeUartSlot2C");
            //TestManager.RunTest(typeof(FLBBUnitTest));
            TestManager.RunTest(typeof(UnitTest_BoschPTH));
            //TestManager.RunTest(typeof(UnitTest_PSB_SN2));
            //TestManager.RunTest(typeof(UnitTest_PSB_SN3), "TestI2CSerialPortSlot3B");
            //TestManager.RunTest(typeof(PTHUnitTest));
            //TestManager.RunTest(typeof(OCR504UnitTest));
            //TestManager.RunTest(typeof(PressureNoiseUnitTest));
            //TestManager.RunTest(typeof(ElmoTwitterUnitTest));
            //TestManager.RunTest(typeof(GPSUnitTests), "TestGPSandI2C");
            //TestManager.RunTest(typeof(GPSUnitTests), "TestGPSSciMsg");
            //TestManager.RunTest(typeof(SurfaceOpsUnitTests), "TestSurfaceOpsFileTransfer");
            //TestManager.RunTest(typeof(ProfileUnitTests));
            //TestManager.RunTest(typeof(BEUnitTests),"TestDualLeadController");
            //TestManager.RunTest(typeof(SciLoggerUT));
            //TestManager.RunTest(typeof(BEUnitTests), "TestPositionSensor");
            //TestManager.RunTest(typeof(PumpMotorUnitTests), "ControlWithDebugger");
            //TestManager.RunTest(typeof(PumpMotorUnitTests), "TortureTest");
            //TestManager.RunTest(typeof(SBDUnitTests));
            //TestManager.RunTest(typeof(ABDecreaseBuoyancyUT));
            //TestManager.RunTest(typeof(UnitTestMsc),"TestPHandNO3Sample");
            //TestManager.RunTest(typeof(UnitTestMsc),"TestGetConfigs");
            //TestManager.RunTest(typeof(ReadFile));
            //TestManager.RunTest(typeof(MsgQueueUnitTest));
            //TestManager.RunTest(typeof(StringPotUT));
            //TestManager.RunTest(typeof(BEUnitTests),"TestGMDualLead");
            //TestManager.RunTest(typeof(StartCPModeUT));
            #endregion

            #region CONSOLE
            var keepGoing = true;
            if (!keepGoing) return;

            ////DisableFLBB();

            InitConsole();

            cpfConsole.welcomeMessage();

            cpfConsole.printPrompt();
            while (true)
            {
                try
                {
                    cpfConsole.readUntilLN();
                }
                catch (Exception e)
                {
                    EngrLogger.writeToColumns("EXCEPTION: "+e.ToString());
                }
                Thread.Sleep(20);
            }
            #endregion
        }

        private static void Initialize()
        {
            Thread.Sleep(1000);
            Utility.SetLocalTime(RealTimeClock.GetDateTime());
            mbBoard = HWModules.MotherBoard.Instance;
            mbBoard.Enable12V();
            mbBoard.Enable5V();
            sdStorage = SDStorage.Instance;
            GHI.Processor.Watchdog.Disable();
            lheBoard = HWModules.LheBoard.Instance;
        }

        private static void NonEssentialInitialize()
        {
            //Let the Gt console output engr logs
            SciLogger.openNewFiles();

            Debug.Print("Stopping Buoyancy Engine motor");
            BuoyancyEngine buoyancyEngine = BuoyancyEngine.Instance;
            buoyancyEngine.init();
            buoyancyEngine.stopMotor();
        }

        private static StateBase[] states;

        private static void InitConsole()
        {
            mbBoard.EnableChannelPower(MotherBoard.ChannelNames.BluetoothChannel);
            Thread.Sleep(3000); // let the bluetooth come online
            cpfConsole = new CpfConsole(new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One));
            EngrLogger.SendConsoleLine += cpfConsole.SendLine;
        }
    }
}