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

using Microsoft.SPOT;
using SensorModules;

namespace CPF.Surface_States
{
    public class InitProfile : StateBase
    {
        public InitProfile(string name)
        {
            stateName.Clear();
            stateName.Append(name);

            ECTimeout = 10 * 60 * 1000;   
        }

        private enum Action : int
        {
            initProfileActions = 1,
            collectIsusDuraConfigs,
            lastAction,
            size = lastAction,
        }
        private static Action currentAction;

        public override int doStateEntryActions()
        {
            basicStateEntry();
            /*
            sbTemp.Clear();
            sbTemp.Append("GC free memory: ");
            sbTemp.Append(Debug.GC(true));
            EngrLogger.writeToColumns(sbTemp);
            */
            currentAction = (Action)1;

            //set these values before every profile
            SV.PressureTableNum = 0;
            Program.dumpCPExceptionCounter = 0;
            Program.maxPressureThisProfile = -100.0;

            ctd1.LPFPressure(ctd1.SBE41LPF.RawPressure, SV.PressureTimeStamp.Ticks / 10000000.0, MissionConfig.SBE41CommandModeSamplePeriod, true);

            msc1.powerUp();
            msc1.ResetSequencers();

            resetEMAccumulators();

            return (1);
        }

        public override int doStateActions()
        {

            switch (currentAction)
            {
                #region Sequential state actions
                case Action.initProfileActions:
                    ctd1.sendStopProfile();

                    Thread.Sleep(1000);

                    EngrLogger.Comment("Power cycling SBE41");
                    ctd1.powerCycleSBE41();

                    SV.ProfileNum++;
                    if (SV.ProfileNum >= SV.MaxProfiles)
                    {
                        sbTemp.Clear();
                        sbTemp.Append("Exceeded max profiles, forcing recovery");
                        setForceRecovery(sbTemp);
                    }
                    else
                    {
                        sbTemp.Clear();
                        sbTemp.Append("Profile number = ");
                        sbTemp.Append(SV.ProfileNum.ToString());
                        EngrLogger.writeToColumns(sbTemp);
                    }

                    SciLogger.writeMsgHeader();
                    SciLogger.appendMscDataFileTimeStamp(DateTime.Now);
                    //MSC
                    msc1.StartGetConfigsSequencer();
                    nextAction = (int)Action.collectIsusDuraConfigs;

                    break;

                case Action.collectIsusDuraConfigs:

                    if (msc1.SequenceGetConfigs(true) != 0)
                        nextAction = (int)Action.lastAction;
                    break;

                case Action.lastAction:
                    break;
                #endregion
            }

            //examine the return of the action, continue
            currentAction = (Action)checkActionReturn(nextAction, (int)currentAction);
            return (1);
        }

        public override CPFStates checkEvents()
        {
            if (currentAction == Action.lastAction)
                return (CPFStates.ABDecBuocyFast);

            return (CPFStates.initProfile);
        }

        public override int doExitActions(bool timedOut)
        {
            if (timedOut)
            {
                basicStateExit(true);
            }
            else
            {
                basicStateExit(false);
            }

            return (1);
        }

        public override CPFStates doTimeoutAction()
        {
            return (CPFStates.surfaceOpsSetBellows);
        }
    }

}
