using HWModules;
using SWModules;

namespace CPF.Surface_States
{
    //TODO P2 Put this class and ABRetractFast into one file called ABRetract.  Better yet make one ABRetract class with 2 state actions retract fast and retract slow
    public class ABDecreaseBuoyancySlow : StateBase
    {
        public ABDecreaseBuoyancySlow(string name)
        {
            stateName.Clear();
            stateName.Append(name);

            ECTimeout = 15 * 60 * 1000;   
        }

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

        public override int doStateEntryActions()
        {
            basicStateEntry();

            sbTemp.Clear();
            sbTemp.Append("AB Decreasing buoyancy to pressure = ");
            sbTemp.Append(MissionConfig.ABPressureThreshold.ToString("f2"));
            sbTemp.Append(" at ");
            sbTemp.Append(MissionConfig.ABRetractSlowJV.ToString());
            sbTemp.Append(" cps");
            EngrLogger.writeToColumns(sbTemp);

            ABRetractThresholdCount = 0;

            currentAction = (Action)1;

            return (1);
        }

        public override int doStateActions()
        {

            switch (currentAction)
            {
                case (Action.setBellows):
                    if (!buoyancyEngine.decreasingBuoyancy)
                        buoyancyEngine.decreaseBuoyancy(MissionConfig.ABRetractSlowJV);
                   
                    nextAction = (int)Action.setBellows;

                    break;

                case (Action.lastAction):
                    //do nothing here
                    break;

                default:
                    break;
            }

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

            return (1);
        }

        public override CPFStates checkEvents()
        {
            //TODO P1 use the same exceedABRetractPressureThreshold method that retractFast uses

            //Increment threshold counter on successive tests otherwise reset counter to 0
            if (ctd1.SBE41LPF.RawPressure >= MissionConfig.ABPressureThreshold)
                ABRetractThresholdCount = ABRetractThresholdCount + 1;
            else
                ABRetractThresholdCount = 0;

            //if 3 sucessive counts below threshold have been recorded, transition to descend
            if (ABRetractThresholdCount >= 3)
            {
                return (CPFStates.descend);
            }
            //otherwise stay
            else
            {
                return (CPFStates.ABDecBuocySlow);
            }

        }

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

                //reset privates
                ABRetractThresholdCount = 0;
            }
            else
            {
                basicStateExit(false);
                ABRetractThresholdCount = 0;
            }
            return (1);
        }

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