using log4net;
using System;
using System.Collections.ObjectModel;
using System.Xml;
using TSManagementApp.Utils;

namespace TSManagementApp.Models
{
    public class TimeStampsManager
    {
        #region Logger
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        public TimeStampsManager()
        {
            TimeStamps = new ObservableCollection<TimeStampReqRes>();
        }

        #region Properties

        public ObservableCollection<TimeStampReqRes> TimeStamps { get; set; }

        #endregion

        #region Methods 

        public void AddTimeStamp(TimeStampReqRes timeStamp)
        {
            try
            {
                TimeStamps.Add(timeStamp);
                log.Info($"time stamp {timeStamp.TimeStampFilePath} added to history list");
            }
            catch (Exception ex)
            {
                log.Error($"Cant add time stamp {timeStamp.TimeStampFilePath} to history list - {ex.ToString()}");
            }
            
        }

        public void DeleteOneTimeStamp(TimeStampReqRes timeStamp)
        {
            TimeStamps.Remove(timeStamp);
            log.Info($"Time stamp {timeStamp.TimeStampFilePath} was deleted from history list");
            SerializeTimeStamps();
        }

        public void SerializeTimeStamps()
        {
            try
            {
                if (TimeStamps != null)
                {
                    log.Info($"Start serializing time stamps history");
                    XmlDocument xml = new XmlDocument();
                    xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));

                    XmlNode timeStamps = xml.CreateElement("TimeStamps");
                    XmlNode timeStamp, node;
                    foreach (TimeStampReqRes a in TimeStamps)
                    {
                        timeStamp = xml.CreateElement("TimeStamp");

                        node = xml.CreateElement("SourceFilePath");
                        node.InnerText = a.SourceFilePath;
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("GenTime");
                        node.InnerText = a.GenTime;
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("TimeStampFilePath");
                        node.InnerText = a.TimeStampFilePath;
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("TimeStampFileName");
                        node.InnerText = a.TimeStampFileName;
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("TimeStampFileType");
                        node.InnerText = a.TimeStampFileType;
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("TimeStampRequest");
                        if (a.TimeStampRequest != null)
                            node.InnerText = Convert.ToBase64String(a.TimeStampRequest);
                        timeStamp.AppendChild(node);
                        node = xml.CreateElement("TimeStampResponse");
                        if (a.TimeStampResponse != null)
                            node.InnerText = Convert.ToBase64String(a.TimeStampResponse);
                        timeStamp.AppendChild(node);

                        timeStamps.AppendChild(timeStamp);
                    }

                    xml.AppendChild(timeStamps);
                    xml.Save($"{SystemUtils.UserLocalAppDirectory}\\TimeStampsHistory.xml");
                    log.Info("Time stamp history successfully serialized");
                }
            }
            catch(Exception ex)
            {
                log.Error($"Error while serializing time stamps list - {ex.ToString()}");
            }
        }

        #endregion
    }
}
