using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Storage;

namespace StrategicCombat.GamerStatistics
{

    /// <summary>
    /// This static class handles saving and loading of gamer statistics to and from a file.
    /// </summary>
    public static class FileHandler
    {
        #region Variables

        const String displayNameContainer = "StrategicCombat-GamerStatistics";
        const String filename = "saveGame.sav";

        #endregion

        /// <summary>
        /// This method is used to save gamer statistics to a XML file.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="storageDevice"></param>
        /// <param name="filename"></param>
        public static void SaveGamerStatsToFile(GamerStats data, StorageDevice storageDevice)
        {
            // Variables
            StorageContainer container = null;
            Stream file = null;

            try
            {
                // Open a storage container.
                IAsyncResult result = storageDevice.BeginOpenContainer(displayNameContainer, null, null);

                // Wait for the WaitHandle to become signaled.
                result.AsyncWaitHandle.WaitOne();

                // Get the container
                container = storageDevice.EndOpenContainer(result);

                // Close the wait handle.
                result.AsyncWaitHandle.Close();

                // Check if the file exists and delete it if it does.
                if (container.FileExists(filename))
                    container.DeleteFile(filename);

                // Declare a variable for the file.
                file = container.CreateFile(filename);

                // Convert the object to XML data and put it in the stream
                XmlSerializer serializer = new XmlSerializer(typeof(GamerStats));
                serializer.Serialize(file, data);

            }
            catch (Exception ex)
            {
                // Delete the file.
                String exceptionMessage = ex.Message;
            }
            finally
            {
                // Close the file
                if(file != null)
                    file.Close();

                // Dispose the container, to commit the data.
                if(container != null)
                    container.Dispose();
            }

            

        } // End of the SaveGamerStatsToFile method.

        /// <summary>
        /// This class is used to load gamer statistics from a file.
        /// </summary>
        /// <param name="storageDevice"></param>
        /// <param name="filename"></param>
        /// <returns>GamerStats, null if the file does not exist.</returns>
        public static GamerStats LoadGamerStatsFromFile(StorageDevice storageDevice)
        {
            // Gamer statistics
            StorageContainer container = null;
            Stream file = null;
            GamerStats gamerStatistics = null;

            try
            {
                // Begin an asynchronous operation to open the container.
                IAsyncResult result = storageDevice.BeginOpenContainer(displayNameContainer, null, null);

                // Wait for the WaitHandle to become signaled.
                result.AsyncWaitHandle.WaitOne();

                // Get the container
                container = storageDevice.EndOpenContainer(result);

                // Close the wait handle.
                result.AsyncWaitHandle.Close();

                // Check to see whether the file exists.
                if (container.FileExists(filename))
                {
                    // Open the file.
                    file = container.OpenFile(filename, FileMode.Open);

                    // Read the data from the file
                    XmlSerializer serializer = new XmlSerializer(typeof(GamerStats));
                    gamerStatistics = (GamerStats)serializer.Deserialize(file);
                }

            }
            catch (Exception ex)
            {
                // Delete the file.
                String exceptionMessage = ex.Message;
            }
            finally
            {
                // Close the file
                if(file != null)
                    file.Close();

                // Dispose the container.
                if(container != null)
                    container.Dispose();
            }
            
            // Return the gamer statistics.
            return gamerStatistics;

        } // End of the LoadGamerStatsFromFile method.

    } // End of class

} // End of namespace
