using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Solitaire
{
    public class classStatistics
    {
        public static BinaryFormatter formatter = new BinaryFormatter();
        static string Filename
        {
            get
            {
                return System.IO.Directory.GetCurrentDirectory() + "\\" + "statistics.fs"; 
            }
        }

        public static void File_Load()
        {
            if (System.IO.File.Exists(Filename))
            {
                FileStream fs = new FileStream(Filename, FileMode.Open);

                try
                {
                    GamesPlayed = (int)formatter.Deserialize(fs);
                    Wins = (int)formatter.Deserialize(fs);
                    WinStreak  = (int)formatter.Deserialize(fs);
                    BestWinStreak  = (int)formatter.Deserialize(fs);
                    int intBestStreak_Year = (int)formatter.Deserialize(fs);
                    int intBestStreak_Month = (int)formatter.Deserialize(fs);
                    int intBestStreak_Day = (int)formatter.Deserialize(fs);
                    int intBestStreak_Hour = (int)formatter.Deserialize(fs);
                    int intBestStreak_Minue = (int)formatter.Deserialize(fs);
                    dtWinningStreak = new DateTime(intBestStreak_Year, intBestStreak_Month, intBestStreak_Day, intBestStreak_Hour, intBestStreak_Minue, 0);
                    
                    if (!pictureboxTable.fs_Game_Load(ref fs))
                    {
                        pictureboxTable.FillPiles();
                    }
                }
                catch (Exception)
                {

                }
             
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
            }
        }

        static DateTime _dtWinningStreak = DateTime.Now;
        public static DateTime dtWinningStreak
        {
            get { return _dtWinningStreak; } 
            set { _dtWinningStreak = value; }
        }

        public static void File_Save()
        {
            FileStream fs = null;
            if (System.IO.File.Exists(Filename))
            {
                System.IO.File.Delete(Filename);
            }

            fs = new FileStream(Filename, FileMode.Create);

            try
            {
                formatter.Serialize(fs, GamesPlayed);
                formatter.Serialize(fs, Wins);
                formatter.Serialize(fs, WinStreak);
                formatter.Serialize(fs, BestWinStreak);;
                formatter.Serialize(fs, dtWinningStreak.Year);
                formatter.Serialize(fs, dtWinningStreak.Month);
                formatter.Serialize(fs, dtWinningStreak.Day);
                formatter.Serialize(fs, dtWinningStreak.Hour);
                formatter.Serialize(fs, dtWinningStreak.Minute);

                pictureboxTable.fs_Game_Save(ref fs);
            }
            catch (Exception)
            {

            }

            if (fs != null)
            {
                fs.Close();
                fs = null;
            }

        }
  
        static int _intGamesPlayed = 0;
        static public int GamesPlayed
        {
            get { return _intGamesPlayed; }
            set { _intGamesPlayed = value; }
        }

        static int _intWins = 0;
        static public int Wins
        {
            get { return _intWins; }
            set { _intWins = value; }
        }
        
        static public int Losses
        {
            get { return GamesPlayed - Wins; }
        }

        static int _intWinStreak = 0;
        static public int WinStreak
        {
            get { return _intWinStreak; }
            set 
            {
                if (_intWinStreak != value)
                {
                    _intWinStreak = value;
                    if (WinStreak >= BestWinStreak)
                    {
                        BestWinStreak = WinStreak;
                        dtWinningStreak = DateTime.Now;
                    }
                }
            }
        }

        static int _intBestWinStreak = 0;
        static public int BestWinStreak
        {
            get { return _intBestWinStreak; }
            set { _intBestWinStreak = value; }
        }

    }
}
