using System;
using Microsoft.SPOT;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using CPF;

using System.Text;
using System.Threading;
using HWModules;
using SWModules;

using CPF.Surface_States;
using Microsoft.SPOT.Hardware;
using SensorModules;

namespace CPFUnitTests
{
    [TestClass]
    public class SurfaceOpsUnitTests
    {
        [TestMethod]
        public void TestSurfaceOpsSciLogger()
        {
#region INIT
            MotherBoard motherboard = MotherBoard.Instance;
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.CTDChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.GPSChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.OptodeChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.RadiometerChannel);

            Thread.Sleep(1000);

            MessageQueue messageQ = MessageQueue.Instance;

            SurfaceOps stateSurfaceOps = new SurfaceOps("Surface Ops");

            //BTConsole btConsole = BTConsole.Instance;
            //btConsole.powerUp();
            //btConsole.init();
            //EngrLogger.SendConsoleLine += btConsole.SendLine;

            CTD1 ctd1 = CTD1.Instance;
            ctd1.init();

            Optode1 optode = Optode1.Instance;
            optode.init();

            Radiometer1 radiometer1 = Radiometer1.Instance;
            radiometer1.init();
#endregion

            messageQ.processQ();

            //need to comment/uncomment standard state entry
            //stateSurfaceOps.doStateEntryActions();

            int actionReturn = 0;
            do
            {
                actionReturn = stateSurfaceOps.doStateActions();
            } while (actionReturn <= 3);
        }

        [TestMethod]
        public void TestSurfaceOpsFileTransfer()
        {
            #region INIT
            MotherBoard motherboard = MotherBoard.Instance;
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.CTDChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.GPSChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.IridiumChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.OptodeChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.RadiometerChannel);
            //motherboard.EnableChannelPower(MotherBoard.ChannelNames.PTHSensorChannel);//CANNOT SWITCH PTH POWER WITH ISB
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.FLBBChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.PositionSensorChannel);

            Thread.Sleep(1000);

            MessageQueue messageQ = MessageQueue.Instance;

            SurfaceOps stateSurfaceOps = new SurfaceOps("Surface Ops");

            //BTConsole btConsole = BTConsole.Instance;
            //btConsole.powerUp();
            //btConsole.init();
            //EngrLogger.SendConsoleLine += btConsole.SendLine;

            CTD1 ctd1 = CTD1.Instance;
            ctd1.init();

            BuoyancyEngine buoyancyEngine = BuoyancyEngine.Instance;
            buoyancyEngine.init();

            IridiumModem1 iModem = IridiumModem1.Instance;
            iModem.init();

            GPS1 gps = GPS1.Instance;

            Optode1 optode = Optode1.Instance;
            optode.init();

            Radiometer1 radiometer1 = Radiometer1.Instance;
            radiometer1.init();

            RMG185_PTHSensor pthSensor = new RMG185_PTHSensor();
            pthSensor.init();
            pthSensor.getPTH();

            FLBB1 flbbSensor = FLBB1.Instance;
            flbbSensor.init();

            #endregion

            Mission.createStandardMission();
            Mission.documentMission();

            SV.MissionStartTime = DateTime.Now;

            TimeSpan SMCycleStartTime;

            messageQ.processQ();

            //Let's manually put some files in queue
            //SciLogger.EnqueueFileForXfer(@"\SD\data\2018-8-10T0-12-57.eng");
            //SciLogger.EnqueueFileForXfer(@"\SD\data\2018-8-10T0-30-15.eng");
            //SciLogger.EnqueueFileForXfer(@"\SD\data\2018-8-10T0-34-32.eng");

            EngrLogger.Comment("Starting Surface Ops" );
            SV.CurrentState = CPFStates.surfaceOps;

            stateSurfaceOps.doStateEntryActions();

            int stateReturn = 0;
            short ctr = 0;

            do {
                SMCycleStartTime = Utility.GetMachineTime();

                stateSurfaceOps.doStateActions();
                if (++ctr == 10)
                {
                    ctr = 0;
                    EngrLogger.Comment("Surface Ops current action = " + stateSurfaceOps.CurrentAction);
                }
                messageQ.processQ();

                SV.NextState = stateSurfaceOps.checkEvents();
                //SV.NextState = stateSurfaceOps.checkGlobalIssues();

                StateBase.StateMachinePacer(SMCycleStartTime);

            } while (SV.NextState == SV.CurrentState && stateSurfaceOps.CurrentAction < (int)SurfaceOps.Action.lastAction);

        }
    }
}