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

namespace ConVX.VXData
{
    public class ConfigStore
    {

        public object ReadConfigFromFile(Type objectType, string filePath)
        {
            try
            {

                object mxml = new object();

                XmlSerializer serializer = new XmlSerializer(objectType);

                StreamReader reader = new StreamReader(filePath);

                mxml = serializer.Deserialize(reader);

                reader.Close();
                return mxml;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void WriteConfigToFile(object config, Type objectType, string filePath)
        {
            try
            {
                object mConfig = new object();
                mConfig = config;
                XmlSerializer serializer = new XmlSerializer(objectType);

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.Encoding = Encoding.Unicode;

                StringWriter txtwriter = new StringWriter();
                XmlWriter xmlwtr = XmlWriter.Create(txtwriter, settings);
                serializer.Serialize(xmlwtr, mConfig);

                StreamWriter writer = new StreamWriter(filePath);
                writer.Write(txtwriter.ToString());

                writer.Close();
                txtwriter.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
