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

namespace SurfacePlot3D.Services
{
    internal class FileService : CustomBindableBase
    {
        public FileService()
        {

        }

        public T LoadProjectFile<T>(String fileName, Type[] types = null) where T : class
        {
            Object data = null;
            String xml = String.Empty;
            String path = String.Empty;
            try
            {
                path = fileName;
                xml = path.ReadFile();

                XmlSerializer deSerializer = new XmlSerializer(typeof(T), types);

                StringReader reader = new StringReader(xml);
                data = (T)deSerializer.Deserialize(reader);
                reader.Close(); reader.Dispose(); reader = null;
            }
            catch (Exception ex)
            {
                var msg = String.Empty;
                if (String.IsNullOrEmpty(xml))
                    msg += "Xml is empty from file: " + path;
                var innerMessage = String.Empty;
                if (ex.InnerException != null) innerMessage = ex.InnerException.Message;
                msg += "  " + ex.Message + " : " + innerMessage;

                //ShellViewModel.SendMessage(msg);
            }

            return data as T;
        }

        private Boolean IsXml(String xml)
        {
            try
            {
                XmlDocument myDoc = new XmlDocument();
                myDoc.LoadXml(xml);
                return true;
            }
            catch (XmlException ex)
            {
                return false;
            }
        }

        public T LoadFromXml<T>(String xml, Type[] types = null) where T : class
        {
            if (!String.IsNullOrEmpty(xml) && IsXml(xml))
            {
                Object data = null;
                XmlSerializer deSerializer = new XmlSerializer(typeof(T), types);
                StringReader reader = new StringReader(xml);
                data = (T)deSerializer.Deserialize(reader);
                reader.Close(); reader.Dispose(); reader = null;
                return (T)data;
            }

            return null;
        }

        public String SaveToXml<T>(Object rsd, Type[] types) where T : class
        {
            String xml = String.Empty;

            if (rsd != null)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), types);
                StringBuilder stringBuilder = new StringBuilder();
                using (StringWriter writer = new StringWriter(stringBuilder))
                {
                    xmlSerializer.Serialize(writer, rsd as T);
                    xml = stringBuilder.ToString();
                }
            }

            return xml;
        }

        public void SaveProjectFile<T>
        (
              Object rsd
            , Type[] types
            , String fileName
        ) where T : class
        {
            if (rsd == null) return;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), types);
            StringBuilder stringBuilder = new StringBuilder();
            using (StringWriter writer = new StringWriter(stringBuilder))
            {
                xmlSerializer.Serialize(writer, rsd as T);
                String xml = stringBuilder.ToString();
                xml.WriteFile(fileName, false);
                writer.Close();
                writer.Flush();
            }
        }

        public String SaveProjectFileToXml<T>
        (
                Object rsd
            , Type[] types
        ) where T : class
        {
            if (rsd == null) return String.Empty;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), types);
            StringBuilder stringBuilder = new StringBuilder();
            using (StringWriter writer = new StringWriter(stringBuilder))
            {
                xmlSerializer.Serialize(writer, rsd as T);
                return stringBuilder.ToString();
            }
        }
    }
}