using System;
using Microsoft.SPOT;

using HWModules;
using SWModules;

namespace CPF.Surface_States
{
    public class PreMissionDelay : StateBase
    {
        private enum Action : int
        {
            wait = 1,
            lastAction,
            size = lastAction
        }
        private static Action currentAction;

        private static TimeSpan ecExtraTime = new TimeSpan(0, 5, 0);

        public PreMissionDelay(string name)
        {
            stateName.Clear();
            stateName.Append(name);

            ECTimeout = (int)(MissionConfig.PreMissionDelayPeriod.Ticks / TimeSpan.TicksPerMillisecond);
        }

        private static DateTime startTime = new DateTime();

        public override int doStateEntryActions()
        {
            basicStateEntry();

            startTime = DateTime.Now;

            sbTemp.Clear();
            sbTemp.Append("Pre-mission delay period = ");
            sbTemp.Append(MissionConfig.PreMissionDelayPeriod.ToString());
            EngrLogger.writeToColumns(sbTemp);

            currentAction = (Action)1;
            return (1);
        }

        public override int doStateActions()
        {

            switch (currentAction)
            {
                case (Action.wait):

                    nextAction = wait();

                    break;

                case (Action.lastAction):
                    //cycle back to wait, even tho we shouldn't get here
                    currentAction = Action.wait;
                    break;
            }

            currentAction = (Action)checkActionReturn(nextAction, (int)currentAction);

            return (1);
        }

        public override CPFStates checkEvents()
        {
            //TODO The commented line is appropriate for production deployments, the uncommented line is being used during development to require user interaction to start profiling
            //if (SV.SurfaceOpsGo || ((DateTime.Now - startTime) > missionConfig.PreMissionDelayPeriod))
            if (SV.SurfaceOpsGo)
            {
                SV.SurfaceOpsGo = false;
                return (CPFStates.initProfile);
            }

            return (CPFStates.preMissionDelay);
        }

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

            return (1);
        }

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

        private static int wait()
        {
            return ((int)Action.wait);
        }
    }

}
