using System;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Xml;
using Earthblend.Tts.Shell.Interfaces;
using Earthblend.Nts.Common.MeshGenerators;
using Earthblend.Tts.Common.Core.MeshGenerators.Base;
using Earthblend.Tts.Common.Core.Extentions;
using Earthblend.Tts.Common.Core.Members;
using Earthblend.Tts.Common.MeshGenerators;
using Earthblend.Tts.Common.Core.MeshGenerators;
using System.Windows.Media.Media3D;
using Earthblend.Nts.Common;

namespace Earthblend.Tts.Common.Core.Services
{
    public class FileService : CustomBindableBase
    {
        public FileService()
        {

        }

        //public FileService(IShellViewModel shellViewModel)
        //{
        //    ShellViewModel = shellViewModel;
        //}

        //private IShellViewModel ShellViewModel { get; set; }

        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)
            {
                String msg = ex.Message;
                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();
            }
        }
    }
}