using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Xml;
using JWH;
using JWH.DATA;

namespace DDUtilityApp.TIBRENDEZVOUS.DATA
{

    public class Mode : DataTableBase
    {

        public string Name { get; set; } = string.Empty;

        public List<Service> Services { get; set; } = new List<Service>();

        public List<Network> Networks { get; set; } = new List<Network>();

        public List<Daemon> Daemons { get; set; } = new List<Daemon>();

        public List<Subject> Subjects { get; set; } = new List<Subject>();

        public List<SendSubject> SendSubjects { get; set; } = new List<SendSubject>();

        [ReadOnly(true)]
        public string Description { get { return $"{this.Name}"; } }

        public Mode()
        {
        }

        public Mode(string name)
        {
            this.Name = name;
        }

        public Mode(XmlNode node)
        {
            try
            {
                node.PropertiesCopyAttribute(this);
                XmlNode[] nodes = null;

                // Services
                nodes = node.GetNodesByName("Service");
                foreach (XmlNode item in nodes)
                    this.Services.Add(new Service(item));

                // Networks
                nodes = node.GetNodesByName("Network");
                foreach (XmlNode item in nodes)
                    this.Networks.Add(new Network(item));

                // Daemons
                nodes = node.GetNodesByName("Daemon");
                foreach (XmlNode item in nodes)
                    this.Daemons.Add(new Daemon(item));

                // Subjects
                nodes = node.GetNodesByName("Subject");
                foreach (XmlNode item in nodes)
                    this.Subjects.Add(new Subject(item));

                // SendSubjects
                nodes = node.GetNodesByName("SendSubject");
                foreach (XmlNode item in nodes)
                    this.SendSubjects.Add(new SendSubject(item));
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        public string ToXmlString(string indent = "")
        {
            try
            {
                string indent02 = $"{indent}  ";
                string indent04 = $"{indent02}  ";
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"{indent}<{this.GetType().Name} Name=\"{this.Name}\">");

                // Services
                foreach (Service value in this.Services)
                    sb.Append(value.ToXmlString(indent04));

                // Networks
                foreach (Network value in this.Networks)
                    sb.Append(value.ToXmlString(indent04));

                // Daemons
                foreach (Daemon value in this.Daemons)
                    sb.Append(value.ToXmlString(indent04));

                // Subjects
                foreach (Subject value in this.Subjects)
                    sb.Append(value.ToXmlString(indent04));

                // SendSubjects
                foreach (SendSubject value in this.SendSubjects)
                    sb.Append(value.ToXmlString(indent04));

                sb.AppendLine($"{indent}</{this.GetType().Name}>");
                return sb.ToString();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                throw ex;
            }
        }

        public override string ToString()
        {
            return $"{this.Name}";
        }

    }

}
