using JWH;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DDUtilityApp.SECS
{

    public class CEID
    {

        public static CEID CreateCEID(XmlNode node)
        {
            CEID ceid = new CEID();
            try
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (string.Compare(attribute.Name, "ID", true) == 0) ceid.ID = attribute.Value;
                    if (string.Compare(attribute.Name, "Name", true) == 0) ceid.Name = attribute.Value;
                    if (string.Compare(attribute.Name, "Description", true) == 0) ceid.Description = attribute.Value;
                }

                if (string.IsNullOrEmpty(ceid.ID))
                {
                    XLogger.Instance.Warn($"CEID.ID 값이 존재하지 않습니다.");
                    return null;
                }

                int seq = 1;
                foreach (XmlNode child in node.ChildNodes)
                {
                    RPT rpt = RPT.CreateRPT(child);
                    if (rpt.Seq == -1) rpt.Seq = seq++;
                    else seq = rpt.Seq + 1;
                    if (rpt != null) ceid.RptDictionary.Add(rpt.ID, rpt);
                }

                return ceid;
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                return null;
            }
        }


        #region [ Properties ] ------------------------------------------------

        public string ID { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public bool Enable { get; set; } = true;

        public Dictionary<string, RPT> RptDictionary { get; set; } = new Dictionary<string, RPT>();

        public IEnumerable<RPT> RptCollection { get { return this.RptDictionary.Values; } }

        #endregion

        #region [ CEID ] ------------------------------------------------------

        public CEID()
        {
        }

        public CEID(string id, string name = "", string description = "", bool enable = true)
        {
            this.ID = id;
            this.Name = name;
            this.Description = description;
            this.Enable = enable;
        }

        #endregion

        #region [ Method ] ----------------------------------------------------

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"ID={this.ID}");
            sb.Append($", Name={this.Name}");
            if (!string.IsNullOrEmpty(this.Description)) sb.Append($", Description={this.Description}");
            if (this.Enable == false) sb.Append($", Enable={this.Enable}");

            StringBuilder sbRpt = new StringBuilder();
            sbRpt.Append($"RPT[{this.RptCollection.Count()}] {{ ");
            foreach (RPT rpt in this.RptCollection)
                sbRpt.Append($"{rpt.ID} ");

            sbRpt.Append($"}}");

            return $"{sb.ToString()}, {sbRpt.ToString()}";
        }

        public string ToXml(string indent = "", bool inReport = true, bool inSvid = true)
        {
            StringBuilder sb = new StringBuilder();

            if (inReport)
            {
                sb.Append($"{indent}<CEID ID='{this.ID}'");
                sb.Append($" NAME='{this.Name}'");
                sb.Append($" ENABLE='{this.Enable}' ");
                if (!string.IsNullOrEmpty(this.Description)) sb.Append($" DESCRIPTION='{this.Description}'");
                sb.AppendLine($">");

                foreach (RPT rpt in this.RptCollection.OrderBy(x => x.Seq))
                    sb.AppendLine($"{indent}{rpt.ToXml($"  ", inSvid)}");
                sb.Append($"{indent}</CEID>");
            }
            else
            {
                sb.Append($"{indent}<CEID ID='{this.ID}'");
                sb.Append($" NAME='{this.Name}'");
                sb.Append($" ENABLE='{this.Enable}' ");
                if (!string.IsNullOrEmpty(this.Description)) sb.Append($" DESCRIPTION='{this.Description}'");
                sb.Append($" />");
            }

            return sb.ToString();
        }

        #endregion

    }

}
