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

namespace DDUtilityApp.SECS
{

    public class RPT
    {

        public static RPT CreateRPT(XmlNode node)
        {
            RPT rpt = new RPT();
            try
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (string.Compare(attribute.Name, "ID", true) == 0) rpt.ID = attribute.Value;
                    if (string.Compare(attribute.Name, "Seq", true) == 0) { int nSeq; int.TryParse(attribute.Value, out nSeq); rpt.Seq = nSeq; };
                    if (string.Compare(attribute.Name, "Name", true) == 0) rpt.Name = attribute.Value;
                    if (string.Compare(attribute.Name, "Description", true) == 0) rpt.Description = attribute.Value;
                }

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

                int seq = 1;
                foreach (XmlNode child in node.ChildNodes)
                {
                    VID vid = VID.CreateVID(child);
                    if (vid.Seq == -1) vid.Seq = seq++;
                    else seq = rpt.Seq + 1;
                    if (vid != null) rpt.VidDictionary.Add(vid.ID, vid);
                }

                return rpt;
            }
            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 int Seq { get; set; } = -1;

        public Dictionary<string, VID> VidDictionary { get; set; } = new Dictionary<string, VID>();

        public IEnumerable<VID> VidCollection { get { return this.VidDictionary.Values; } }

        #endregion

        #region [ RPT ] -------------------------------------------------------

        public RPT()
        {
        }

        public RPT(string id, string name = "", string description = "")
        {
            this.ID = id;
            this.Name = name;
            this.Description = description;
        }

        #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}");

            StringBuilder sbVid = new StringBuilder();
            sbVid.Append($"VID[{this.VidCollection.Count()}] {{ ");
            foreach(VID vid in this.VidCollection)
                sbVid.Append($"{vid.ID} ");

            sbVid.Append($"}}");

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

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

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

                foreach (VID vid in this.VidCollection.OrderBy(x => x.Seq))
                    sb.AppendLine($"{indent}{vid.ToXml($"  ", true)}");

                sb.Append($"{indent}</RPT>");
            }
            else
            {
                sb.Append($"{indent}<RPT");
                sb.Append($" SEQ ='{this.Seq}'");
                sb.Append($" ID ='{this.ID}'");
                if (!string.IsNullOrEmpty(this.Name)) sb.Append($" NAME='{this.Name}'");
                sb.Append($" />");
            }

            return sb.ToString();
        }

        #endregion

    }

}
