using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace Solitaire
{
    public partial class formSolitaire : Form
    {
        public static formSolitaire instance = null;

        public static classEventController cEC = new classEventController();

        Point ptLoadInfo_Location = new Point(10, 10);
        Size szLoadInfo = new Size(500, 800);
        float fltLoadInfo_Zoom = 1f;

        pictureboxTable picTable = null;
        public static bool bolDebug = false;
        public formSolitaire()
        {
            instance = this;
            InitializeComponent();

            string strWorkingDirectory = System.IO.Directory.GetCurrentDirectory();
            string strDeveloperDirectory = "C:\\Users\\chris\\OneDrive\\Solitaire\\Solitaire\\bin\\Debug";
            bolDebug = strDeveloperDirectory == strWorkingDirectory;

            Text = "Solitaire";
            ControlBox = true;
            classPlayingCard.BuildSet();

            FormClosing += FormSolitaire_FormClosing;
            Activated += FormSolitaire_Activated;
        }

        bool bolActivated = false;
        private void FormSolitaire_Activated(object sender, EventArgs e)
        {
            if (bolActivated) return;

            picTable=new pictureboxTable();
            Controls.Add(picTable);
            picTable.Dock = DockStyle.Fill;

            classStatistics.File_Load();

            if (formLoad())
            {
                pictureboxTable.Zoom = fltLoadInfo_Zoom;
                instance.Location = ptLoadInfo_Location;
                instance.Size = szLoadInfo;
                picTable.bolDealingCards = false;
            }
            else
                picTable.NewGame();

            pictureboxTable.launch_Intro();
            bolActivated = true;
        }

        bool formLoad()
        {
            if (System.IO.File.Exists(FormLoadInfo_Filename))
            {
                try
                {
                    XmlDocument xDoc = new XmlDocument();

                    xDoc.Load(FormLoadInfo_Filename);

                    XmlNode xRoot = xDoc.FirstChild;
                    XmlNode xLeft = xRoot.FirstChild;
                    XmlNode xTop = xLeft.NextSibling;
                    XmlNode xWidth = xTop.NextSibling;
                    XmlNode xHeight = xWidth.NextSibling;
                    XmlNode xZoom = xHeight.NextSibling;
                    XmlNode xGameRecord = xZoom.NextSibling;

                    int intLeft = Convert.ToInt32(xLeft.InnerText);
                    int intTop = Convert.ToInt32(xTop.InnerText);
                    int intWidth = Convert.ToInt32(xWidth.InnerText);
                    int intHeight = Convert.ToInt32(xHeight.InnerText);
                    float fltZoom = (float)Convert.ToDouble(xZoom.InnerText);

                    ptLoadInfo_Location = new Point(intLeft, intTop);
                    szLoadInfo = new Size(intWidth, intHeight);
                    fltLoadInfo_Zoom = fltZoom;
                     
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
            else
                return false;
        }
        void formSave()
        {
            XmlDocument xDoc = new XmlDocument();

            XmlNode xRoot = xDoc.CreateElement("root");
            xDoc.AppendChild(xRoot);
            {
                XmlNode xLeft = xDoc.CreateElement("Left");
                {
                    xLeft.InnerText = Left.ToString();
                }
                xRoot.AppendChild(xLeft);

                XmlNode xTop = xDoc.CreateElement("Top");
                {
                    xTop.InnerText = Top.ToString();
                }
                xRoot.AppendChild(xTop);

                XmlNode xWidth = xDoc.CreateElement("Width");
                {
                    xWidth.InnerText = Width.ToString();
                }
                xRoot.AppendChild(xWidth);

                XmlNode xHeight = xDoc.CreateElement("Height");
                {
                    xHeight.InnerText = Height.ToString();
                }
                xRoot.AppendChild(xHeight);

                XmlNode xZoom = xDoc.CreateElement("Zoom");
                {
                    xZoom.InnerText = pictureboxTable.Zoom.ToString();
                }
                xRoot.AppendChild(xZoom);

                //xRoot.AppendChild(pictureboxTable.xml_GetGameRecord(ref xDoc));
            }
            xDoc.Save(FormLoadInfo_Filename);
        }


        //public static Semaphore semFormClosing = new Semaphore(1, 1);
        private void FormSolitaire_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (picTable.bolDealingCards)
            {
                pictureboxTable.FillPiles();
            }
            
            formSave();
            classStatistics.File_Save();
        }

        string FormLoadInfo_Filename
        {
            get { return System.IO.Directory.GetCurrentDirectory() + "\\formInitData.xml"; }
        }


    }
}
