using System;
using System.Text;
using HWModules;
using SensorModules;
using SWModules;

namespace CPF
{
    public class Mission
    {
        public static PressureTable[] descendTable;
        public static PressureTable[] ascendTable;

        public static TimeSpan parkSamplePeriod;
        public static bool recoveryMode = true;
        
        public const int stdParkDepth = 85;
        public const int stdMaxPressure = 100;
        public const int stdMaxProfiles = 15;
        public const int stdMaxMissionDays = 3;
        public const int stdMaxMissionHours = 18;
        public const int stdMaxMissionMinutes = 0;
        public const int stdParkSamplePeriodMinutes = 2;
        public const int stdParkTimeHours = 6;
        public const int stdParkTimeMinutes = 0;

        //private static StringBuilder fileName = new StringBuilder(128);

        //public static StringBuilder missionFileName = new StringBuilder(128);
        private static StringBuilder sbTemp = new StringBuilder(256);

        //private static FullMission mission60mHoldAt6 = new FullMission();

        public enum Missions
        {
            mission60mHold8 = 0,
            missionHoldAt5m,
            mission60mNoHold,
            mission150mNoHold,
            mission150m40mHold
        }

        public static void createStandardMission(float parkDepth = stdParkDepth, int parkSamplePeriodMinutes = stdParkSamplePeriodMinutes,
                                                    int parkTimeHours = stdParkTimeHours, int parkTimeMinutes = stdParkTimeMinutes)
        {
            descendTable = new PressureTable[1];

            recoveryMode = true;
            parkSamplePeriod = new TimeSpan(0, parkSamplePeriodMinutes, 0);

            //First the descend table
            descendTable[0] = new PressureTable
            {
                pressure = parkDepth,                   //decibar
                PV = (float)-0.20,                      //decibar per second
                parkTime = new TimeSpan(parkTimeHours, parkTimeMinutes, 0),
                sampleList = 7
            };



            int numAscendDepths = (int)((parkDepth / 5) + 1);
            ascendTable = new PressureTable[numAscendDepths];

            float pressure = parkDepth;
            for(int i = 0; i < ascendTable.Length; i++)
            {
                ascendTable[i] = new PressureTable();

                ascendTable[i].pressure = pressure;
                ascendTable[i].PV = 0.10f;
                ascendTable[i].sampleList = (byte)7;
                ascendTable[i].parkTime = new TimeSpan(0, 0, 0);
    
                if (pressure > 20)
                    pressure = pressure - 5.0f;
                else
                    pressure = pressure - 5.0f;
            } 

            SV.ManualProfileMode = Mission.recoveryMode;

            SV.MaxProfiles = Mission.stdMaxProfiles;
            //sbTemp.Clear();
            //sbTemp.Append("Maximum number of profiles = ");
            //sbTemp.Append(SV.MaxProfiles.ToString());
            //EngrLogger.writeToColumns(sbTemp.ToString());

            SV.MissionTimeoutTS = new TimeSpan(Mission.stdMaxMissionDays, Mission.stdMaxMissionHours, Mission.stdMaxMissionMinutes, 0);
            SV.MissionStartTime = DateTime.Now;
            //sbTemp.Clear();
            //sbTemp.Append("Mission Timeout = ");
            //sbTemp.Append(SV.MissionTimeoutTS.ToString());
            //sbTemp.Append("   Mission Start Time: ");
            //sbTemp.Append(SV.MissionStartTime.ToString());
            //EngrLogger.writeToColumns(sbTemp.ToString());
        }

        public static void documentMission()
        {
            sbTemp.Clear();
            sbTemp.Append("Recovery mode = ");
            sbTemp.Append(recoveryMode.ToString());
            SciLogger.writeToLog(sbTemp);
            EngrLogger.writeToColumns(1, sbTemp);

            sbTemp.Clear();
            sbTemp.Append("Park Sample Period = ");
            sbTemp.Append(parkSamplePeriod.ToString());
            SciLogger.writeToLog(sbTemp);
            EngrLogger.writeToColumns(1, sbTemp);

            sbTemp.Clear();
            sbTemp.Append("Mission Timeout = ");
            sbTemp.Append(SV.MissionTimeoutTS.ToString());
            SciLogger.writeToLog(sbTemp);
            EngrLogger.writeToColumns(1, sbTemp);

            sbTemp.Clear();
            sbTemp.Append("Max Profiles = ");
            sbTemp.Append(SV.MaxProfiles.ToString());
            SciLogger.writeToLog(sbTemp);
            EngrLogger.writeToColumns(1, sbTemp);

            sbTemp.Clear();
            sbTemp.Append("Max Pressure = ");
            sbTemp.Append(Mission.stdMaxPressure.ToString());
            SciLogger.writeToLog(sbTemp);
            EngrLogger.writeToColumns(1, sbTemp);

            for (int i = 0; i < descendTable.Length; i++)
            {
                sbTemp.Clear();
                sbTemp.Append("Descend table ");
                sbTemp.Append(i.ToString());
                sbTemp.Append(" Pressure, PV, Park Time, sample list: ");
                sbTemp.Append(descendTable[i].pressure.ToString());
                sbTemp.Append(", ");
                sbTemp.Append(descendTable[i].PV.ToString("f3"));
                sbTemp.Append(", ");
                sbTemp.Append(descendTable[i].parkTime.ToString());
                sbTemp.Append(", ");
                sbTemp.Append(descendTable[i].sampleList.ToString());
                SciLogger.writeToLog(sbTemp);
                EngrLogger.writeToColumns(1, sbTemp);
            }

            for (int i = 0; i < ascendTable.Length; i++)
            {
                sbTemp.Clear();
                sbTemp.Append("Ascend table ");
                sbTemp.Append(i.ToString());
                sbTemp.Append(" Pressure, PV, Park Time, sample list: ");
                sbTemp.Append(ascendTable[i].pressure.ToString());
                sbTemp.Append(", ");
                sbTemp.Append(ascendTable[i].PV.ToString("f3"));
                sbTemp.Append(", ");
                sbTemp.Append(ascendTable[i].parkTime.ToString());
                sbTemp.Append(", ");
                sbTemp.Append(ascendTable[i].sampleList.ToString());
                SciLogger.writeToLog(sbTemp);
                EngrLogger.writeToColumns(1, sbTemp);
            }
        }
    }


    /*
    [Serializable]
    public class FullMission
    {
        public bool recoveryMode;
        public TimeSpan samplePeriod;
        public PressureTable[] descendTable;
        public PressureTable[] ascendTable;

        public FullMission()
        { }
    }
    */
}
