using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Nappat.Testing.WPF
{
    class SaveLoad
    {
        #region Private Fields
        DataSender dataSender;
        string initFileName = "nappatini";
        string extension = ".ini";
        string initPath = null;
        #endregion

        #region Constructors
        public SaveLoad(DataSender dataSender)
        {
            this.dataSender = dataSender;
            initPath = System.Environment.CurrentDirectory + "\\" + initFileName + extension;
        }
        #endregion

        #region Private Methods
        private void LoadLogger()
        {
            this.dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Start interpreting file: " + this.dataSender.TestNames, null);
        }
        #endregion

        #region Public Methods
        public void SaveInit()
        {
            if (File.Exists(initPath))
                File.SetAttributes(initPath, FileAttributes.Normal);

            XmlSerializer xs = new XmlSerializer(typeof(DataSender));
            using (StreamWriter wr = new StreamWriter(initPath))
            {
                xs.Serialize(wr, dataSender);
            }
            File.SetAttributes(initPath, FileAttributes.Hidden);
        }
        public void Save(string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(DataSender));
            using (StreamWriter wr = new StreamWriter(path))
            {
                xs.Serialize(wr, dataSender);
            }
            this.dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Parameters was saved.", null);
        }
        public DataSender LoadInit()
        {
            if (File.Exists(initPath))
            {
                DataSender retDataSender;
                XmlSerializer xs = new XmlSerializer(typeof(DataSender));
                using (StreamReader rd = new StreamReader(initPath))
                {
                    retDataSender = xs.Deserialize(rd) as DataSender;
                }
                this.dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Parameters was loaded from Init file.", null);
                return retDataSender;
            }
            else
                throw new Exception("Init file was not found.");
        }
        public DataSender Load(string path)
        {
            if (File.Exists(path))
            {
                DataSender retDataSender;
                XmlSerializer xs = new XmlSerializer(typeof(DataSender));
                using (StreamReader rd = new StreamReader(path))
                {
                    retDataSender = xs.Deserialize(rd) as DataSender;
                }
                this.dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Parameters was loaded from saved file.", null);
                return retDataSender;
            }
            else
                throw new Exception("No or wrong file to load.");
        }
        public void DataTransporter(DataSender originDataSender, DataSender newDataSender)
        {
            if (newDataSender.DriverPaths != null)
                originDataSender.DriverPaths = newDataSender.DriverPaths;
            if (newDataSender.CsFilePaths != null)
                originDataSender.CsFilePaths = newDataSender.CsFilePaths;
            if (newDataSender.ConfigPaths != null)
                originDataSender.ConfigPaths = newDataSender.ConfigPaths;
            if (newDataSender.ScriptPaths != null)
                originDataSender.ScriptPaths = newDataSender.ScriptPaths;
            if (newDataSender.TestNames != null)
                originDataSender.TestNames = newDataSender.TestNames;
            if (newDataSender.DeviceNames != null)
                originDataSender.DeviceNames = newDataSender.DeviceNames;
            if (newDataSender.OutBoardNames != null)
                originDataSender.OutBoardNames = newDataSender.OutBoardNames;
            if (newDataSender.InBoardNames != null)
                originDataSender.InBoardNames = newDataSender.InBoardNames;
            if (newDataSender.ChannelNames != null)
                originDataSender.ChannelNames = newDataSender.ChannelNames;
            if (newDataSender.OutputPaths != null)
                originDataSender.OutputPaths = newDataSender.OutputPaths;
        }
        #endregion
    }
}
