using System.IO;
using System.Xml.Serialization;

namespace Monkey.Configuration
{

    public static class ConfigurationManager
    {

        public static PluginConfiguration PluginConfiguration
        {
            get => _pluginConfiguration;
        }

        private static XmlSerializer _xmlSerializer;

        private static PluginConfiguration _pluginConfiguration;

        static ConfigurationManager()
        {
            _xmlSerializer = new XmlSerializer(typeof(PluginConfiguration));

            _pluginConfiguration = null;
        }

        public static void Initialize()
        {
            if (File.Exists(Settings.ConfigurationFilePath))
            {
                LoadConfigurationFromDisk();
            }
            else
            {
                _pluginConfiguration = new PluginConfiguration();

                SaveConfigurationToDisk();
            }
        }

        public static void LoadConfigurationFromDisk()
        {
            using (FileStream fileStream = new FileStream(Settings.ConfigurationFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
                _pluginConfiguration = (_xmlSerializer.Deserialize(fileStream) as PluginConfiguration);
        }

        public static void SaveConfigurationToDisk()
        {
            using (FileStream fileStream = new FileStream(Settings.ConfigurationFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                _xmlSerializer.Serialize(fileStream, _pluginConfiguration);
        }

    }

}
