using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Bombing_Math
{
    public class classPreferences
    {
        static string Filename
        {
            get
            {
                string strRetVal = System.IO.Directory.GetCurrentDirectory() + "\\bombingMath.preferences";
                return strRetVal;
            }
        }

        static int _intHighscore = 0;
        static public int Highscore
        {
            get { return _intHighscore; }
            set { _intHighscore = value; }
        }
        static int _intHighestLevelReached =1;
        static public int HighestLevelReached
        {
            get { return _intHighestLevelReached; }
            set { _intHighestLevelReached = value; }
        }
        static int _intToolCount_Bombs = 0;
        static public int ToolCount_Bombs
        {
            get { return _intToolCount_Bombs; }
            set { _intToolCount_Bombs = value; }
        }
        static int _intToolCount_ExponentialBombs = 0;
        static public int ToolCount_ExponentialBombs
        {
            get { return _intToolCount_ExponentialBombs; }
            set { _intToolCount_ExponentialBombs = value; }
        }
        static int _intToolCount_SpyGlass = 0;
        static public int ToolCount_SpyGlass
        {
            get { return _intToolCount_SpyGlass; }
            set { _intToolCount_SpyGlass = value; }
        }

        static int _intToolCount_TimePiece = 0;
        static public int ToolCount_TimePiece
        {
            get { return _intToolCount_TimePiece; }
            set { _intToolCount_TimePiece = value; }
        }
        static int _intLoadScore = 0;
        static public int LoadScore
        {
            get { return _intLoadScore; }
            set { _intLoadScore = value; }
        }
        static int _intLoadLevel = 0;
        static public int LoadLevel
        {
            get { return _intLoadLevel; }
            set { _intLoadLevel = value; }
        }

        public static void SetCurrentValues()
        {
            if (!formBombingMath.GameIsOver)
            {
                LoadScore = formBombingMath.Score;
                LoadLevel = formBombingMath.Level;
                ToolCount_Bombs = formBombingMath.arrTools_Inventory[(int)enuTools.Bomb];
                ToolCount_ExponentialBombs = formBombingMath.arrTools_Inventory[(int)enuTools.ExponentialBomb];
                ToolCount_SpyGlass = formBombingMath.arrTools_Inventory[(int)enuTools.SpyGlass];
                ToolCount_TimePiece = formBombingMath.arrTools_Inventory[(int)enuTools.TimePiece];
            }

            if (formBombingMath.Level > HighestLevelReached)
                HighestLevelReached = formBombingMath.Level;
            if (formBombingMath.Score > Highscore)
                Highscore = formBombingMath.Score;  
        }

        static public List<classMathProblem> _lstLoad_MathProblems = new List<classMathProblem>();
        static public List<classMathProblem> lstLoad_MathProblems
        {
            get { return _lstLoad_MathProblems; }
            set { _lstLoad_MathProblems = value; }
        }
        static public List<classFallingPrize> _lstLoad_FallingPrizes = new List<classFallingPrize>();
        static public List<classFallingPrize> lstLoad_FallingPrizes
        {
            get { return _lstLoad_FallingPrizes; }
            set { _lstLoad_FallingPrizes = value; }
        }

        static int _intLoad_NumProblemsRemaining = 0;
        public static int intLoad_NumProblemsRemaining
        {
            get { return _intLoad_NumProblemsRemaining; }
            set { _intLoad_NumProblemsRemaining = value; }
        }
        
        static int _intLoad_NumBonusParameterCount = 0;
        public static int intLoad_NumBonusParameterCount
        {
            get { return _intLoad_NumBonusParameterCount; }
            set { _intLoad_NumBonusParameterCount = value; }
        }
        

        public static void Load()
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream fs = null;
            
            if ( System.IO.File.Exists(Filename))
            {
                fs = new FileStream(Filename, FileMode.Open);
                try
                {
                    LoadScore = (int)binaryFormatter.Deserialize(fs);
                    Highscore = (int)binaryFormatter.Deserialize(fs);
                    LoadLevel = (int)binaryFormatter.Deserialize(fs);
                    intLoad_NumBonusParameterCount = (int)binaryFormatter.Deserialize(fs);

                    HighestLevelReached = (int)binaryFormatter.Deserialize(fs);
                    ToolCount_Bombs = (int)binaryFormatter.Deserialize(fs);
                    ToolCount_ExponentialBombs = (int)binaryFormatter.Deserialize(fs);
                    ToolCount_SpyGlass = (int)binaryFormatter.Deserialize(fs);
                    ToolCount_TimePiece = (int)binaryFormatter.Deserialize(fs);


                    formBombingMath.instance.Left = (int)binaryFormatter.Deserialize(fs);
                    formBombingMath.instance.Top= (int)binaryFormatter.Deserialize(fs);
                    formBombingMath.intHeight = (int)binaryFormatter.Deserialize(fs);

                    // load game-state falling prizes 
                    int intNumProblemsFalling = (int)binaryFormatter.Deserialize(fs);
                    intLoad_NumProblemsRemaining = (int)binaryFormatter.Deserialize(fs);
                    lstLoad_MathProblems.Clear();

                    for (int intMathProblemCounter = 0; intMathProblemCounter < intNumProblemsFalling; intMathProblemCounter++)
                    {
                        classMathProblem cMathProblem = new classMathProblem();
                        cMathProblem.Solution = (int)binaryFormatter.Deserialize(fs);
                        cMathProblem.pt.X = (int)binaryFormatter.Deserialize(fs);
                        cMathProblem.pt.Y = (int)binaryFormatter.Deserialize(fs);

                        cMathProblem.vel.X = (int)binaryFormatter.Deserialize(fs);
                        cMathProblem.vel.Y = (int)binaryFormatter.Deserialize(fs);
                        cMathProblem.strProblem = (string)binaryFormatter.Deserialize(fs);
                        lstLoad_MathProblems.Add(cMathProblem);
                    }

                    
                    // load game-state falling prizes 
                    int intNumPrizes = (int)binaryFormatter.Deserialize(fs);
                    lstLoad_FallingPrizes.Clear();

                    for (int intFallingPrizeCounter = 0; intFallingPrizeCounter < intNumPrizes; intFallingPrizeCounter++)
                    {
                        classFallingPrize cFallingPrize = new classFallingPrize();
                        cFallingPrize.pt.X = (int)binaryFormatter.Deserialize(fs);
                        cFallingPrize.pt.Y = (int)binaryFormatter.Deserialize(fs);
                        cFallingPrize.vel.X = (int)binaryFormatter.Deserialize(fs);
                        cFallingPrize.vel.Y = (int)binaryFormatter.Deserialize(fs);
                        int intToolType = (int)binaryFormatter.Deserialize(fs);
                        cFallingPrize.eToolType = (enuTools)intToolType;
                        cFallingPrize.bolParachuteDeployed = (bool) binaryFormatter.Deserialize(fs);
                        cFallingPrize.bolDrawTitle = (bool) binaryFormatter.Deserialize(fs);
                        if (cFallingPrize.bolDrawTitle)
                            cFallingPrize.cEventTicket_ShowBonus.Reschedule();
                        lstLoad_FallingPrizes.Add(cFallingPrize);
                    }


                    // level up 
                    int intNumLevelUps = (int)binaryFormatter.Deserialize(fs);
                    classLevel.lstLevelUp.Clear();

                    for (int intLevelUpCounter = 0; intLevelUpCounter < intNumLevelUps; intLevelUpCounter++)
                    {
                        classLevel.classPreference_LevelUp cLevelUp = new classLevel.classPreference_LevelUp();
                        cLevelUp.Score = (int)binaryFormatter.Deserialize(fs);
                        
                        for (int intToolCounter = 0; intToolCounter < cLevelUp.arrTools.Length; intToolCounter++)
                            cLevelUp.arrTools[intToolCounter] = (int)binaryFormatter.Deserialize(fs);
                        classLevel.lstLevelUp.Add(cLevelUp);
                    }

                }
                catch (Exception)
                {
                    ;
                }
                fs.Close();
            }
            
        }

        

        public static void Save()
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream fs = null;
            if (System.IO.File.Exists(Filename))
                fs = new FileStream(Filename, FileMode.Open);
            else
                fs = new FileStream(Filename, FileMode.Create);

            try
            {
                binaryFormatter.Serialize(fs, formBombingMath.Score);
                binaryFormatter.Serialize(fs, Highscore);
                binaryFormatter.Serialize(fs, formBombingMath.Level);
                binaryFormatter.Serialize(fs, formBombingMath.cLevel.cBonusParameters.Count);
                binaryFormatter.Serialize(fs, HighestLevelReached);
                binaryFormatter.Serialize(fs, formBombingMath.arrTools_Inventory[(int)enuTools.Bomb]);
                binaryFormatter.Serialize(fs, formBombingMath.arrTools_Inventory[(int)enuTools.ExponentialBomb]);//ToolCount_ExponentialBombs);
                binaryFormatter.Serialize(fs, formBombingMath.arrTools_Inventory[(int)enuTools.SpyGlass]); // ToolCount_SpyGlass);
                binaryFormatter.Serialize(fs, formBombingMath.arrTools_Inventory[(int)enuTools.TimePiece]);// ToolCount_TimePiece);
                binaryFormatter.Serialize(fs, formBombingMath.instance.Left);
                binaryFormatter.Serialize(fs, formBombingMath.instance.Top);
                binaryFormatter.Serialize(fs, formBombingMath.intHeight);

                // save game-state falling prizes 
                binaryFormatter.Serialize(fs, classMathProblem.lstProblems.Count);
                binaryFormatter.Serialize(fs, classMathProblem.NumProblemsRemaining);
                for (int intMathProblemCounter = 0; intMathProblemCounter < classMathProblem.lstProblems.Count; intMathProblemCounter++)
                {
                    classMathProblem cMathProblem = classMathProblem.lstProblems[intMathProblemCounter];
                    binaryFormatter.Serialize(fs, cMathProblem.Solution);
                    binaryFormatter.Serialize(fs, cMathProblem.pt.X);
                    binaryFormatter.Serialize(fs, cMathProblem.pt.Y);
                    binaryFormatter.Serialize(fs, cMathProblem.vel.X);
                    binaryFormatter.Serialize(fs, cMathProblem.vel.Y);
                    binaryFormatter.Serialize(fs, cMathProblem.strProblem);
                }

                binaryFormatter.Serialize(fs, classFallingPrize.lst.Count);
                for (int intFallingPrizeCounter = 0; intFallingPrizeCounter < classFallingPrize.lst.Count; intFallingPrizeCounter++)
                {
                    classFallingPrize cFallingPrize = classFallingPrize.lst[intFallingPrizeCounter];
                    binaryFormatter.Serialize(fs, (int)cFallingPrize.pt.X);
                    binaryFormatter.Serialize(fs, (int)cFallingPrize.pt.Y);
                    binaryFormatter.Serialize(fs, (int)cFallingPrize.vel.X);
                    binaryFormatter.Serialize(fs, (int)cFallingPrize.vel.Y);
                    binaryFormatter.Serialize(fs, (int)cFallingPrize.eToolType);
                    binaryFormatter.Serialize(fs, (bool)cFallingPrize.bolParachuteDeployed);
                    binaryFormatter.Serialize(fs, (bool)cFallingPrize.bolDrawTitle);
                }

                // level up 
                binaryFormatter.Serialize(fs, classLevel.lstLevelUp.Count);
                for(int intLevelUpCounter =0; intLevelUpCounter <  classLevel.lstLevelUp.Count; intLevelUpCounter++)
                {
                    classLevel.classPreference_LevelUp cLevelUp = classLevel.lstLevelUp[intLevelUpCounter];
                    binaryFormatter.Serialize(fs, cLevelUp.Score);
                    for(int intToolCounter =0; intToolCounter < cLevelUp.arrTools.Length; intToolCounter++)
                        binaryFormatter.Serialize(fs, cLevelUp.arrTools[intToolCounter]);
                }
            }
            catch (Exception)
            {

            }

            fs.Close();
        }
    }
}
