using System;
using Microsoft.SPOT;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.Threading;
using CPF;
using HWModules;
using SWModules;
using CPF.Profile_States;
using SensorModules;

namespace CPFUnitTests
{
    class SciLoggerUT
    {
        [TestMethod]
        public void TestSciLogger()
        {
            StringBuilder sbTemp = new StringBuilder(64);

            HWModules.MotherBoard motherboard = HWModules.MotherBoard.Instance;

            CTD1 ctd1 = CTD1.Instance;
            FLBB1 flbb1 = FLBB1.Instance;
            Optode1 optode1 = Optode1.Instance;
            Radiometer1 radiometer1 = Radiometer1.Instance;
            MSC1 msc1 = MSC1.Instance;

            EnergyMonitorHotel emBatteryBus = EnergyMonitorHotel.Instance;

            Debug.Print("Getting battery bus volts");
            EnergyMonitorLTC2946.EM_VandI emData = emBatteryBus.getVoltsAndMilliAmps();
            Thread.Sleep(1000);

            MessageQueue messageQ = MessageQueue.Instance;
            QRecord qRecord = new QRecord();
            qRecord.errorCode = 1;
            qRecord.timeStamp = DateTime.Now;
            qRecord.type = QRecordType.SBE41;
            qRecord.byteArray = new byte[32];

            Ascend stateAscend = new Ascend("Ascend UT");

            Debug.Print("Enabling Power");
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.CTDChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.FLBBChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.OptodeChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.RadiometerChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.MSCChannel);

            Debug.Print("Initializing Instruments");
            ctd1.init();
            flbb1.init();
            optode1.init();
            radiometer1.init();
            msc1.InitMsc();

            Thread.Sleep(2000);

            Mission.createStandardMission(100, 1, 0, 4);
            Mission.documentMission();

            SciLogger.openNewLogFile();

            String[] cpString = new String[10];
            cpString[0] = "start junk";
            cpString[1] = "S>dah";
            cpString[2] = "0";
            cpString[3] = "001534467E7615  ";
            cpString[4] = "002833A17EBA16  ";
            cpString[5] = "003C326F7F6D16  ";
            cpString[6] = "0050319F803A14  ";
            cpString[7] = "0064314480B314  ";
            cpString[8] = "upload complete  ";
            cpString[9] = "end junk";

            for (int i = 0; i < 10; i++)
            {
                Array.Clear(qRecord.byteArray, 0, qRecord.byteArray.Length);
                Array.Copy(UTF8Encoding.UTF8.GetBytes(cpString[i]), qRecord.byteArray, cpString[i].Length);
                ctd1.processMessage(qRecord);
            }

            int seqStateMsc;

            Debug.Print("Processing message Q");
            for (int i = 0; i < 10; i++)
            {
                messageQ.processQ();
                Thread.Sleep(1000);
            }

            Debug.Print("Starting park points sample loop");

            for (int i = 0; i < 2; i++)
            {
                //stateAscend.sampleInstruments(1);
                SV.CurrentState = CPFStates.anchor;
                SciLogger.startNewSampleCycle(SciLogger.Instruments.AllInstruments);

                if (i == 0)
                {
                    Debug.Print("Posting anchor comment");
                    sbTemp.Clear();
                    sbTemp.Append("Anchor Points");
                    SciLogger.writeMsgComment(sbTemp);
                }

                int j = 45;

                do
                {
                    messageQ.processQ();
                    seqStateMsc = msc1.SequencePHandNO3Samples();
                    Thread.Sleep(1000);

                } while (j-- > 0);
            }
            SciLogger.endSampleCycle();

            Debug.Print("Posting ascend comment");
            sbTemp.Clear();
            sbTemp.Append("Ascend Profile Points");
            SciLogger.writeMsgComment(sbTemp);

            Debug.Print("Starting ascend sample loop");
            for (int i = 0; i < 3; i++)
            {
                //stateAscend.sampleInstruments(1);
                SV.CurrentState = CPFStates.ascend;
                SciLogger.startNewSampleCycle(SciLogger.Instruments.AllInstruments);

                int j = 45;
                do
                {
                    messageQ.processQ();
                    seqStateMsc = msc1.SequencePHandNO3Samples();
                    Thread.Sleep(1000);

                } while (j-- > 0);
            }
            SciLogger.endSampleCycle();


            Debug.Print("Closing .msg File");
            SciLogger.closeMsgFile();

            Debug.Print("Printing Science File");
            SciLogger.printLastMsgFile();

            Debug.Print("SciLoggerUT Done");

        }
    }
}
