using JWH;
using System;
using System.Text;
using System.Xml;

namespace DDUtilityApp.SECS
{

    public class VID
    {

        public static VID CreateVID(XmlNode node)
        {
            VID vid = new VID();
            try
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (string.Compare(attribute.Name, "ID", true) == 0) vid.ID = attribute.Value;
                    if (string.Compare(attribute.Name, "Seq", true) == 0) { int seq; int.TryParse(attribute.Value, out seq); vid.Seq = seq; };
                    if (string.Compare(attribute.Name, "Name", true) == 0) vid.Name = attribute.Value;
                    if (string.Compare(attribute.Name, "Format", true) == 0) vid.Format = attribute.Value;
                    if (string.Compare(attribute.Name, "Length", true) == 0) { int length; int.TryParse(attribute.Value, out length); vid.Length = length; };
                    if (string.Compare(attribute.Name, "Description", true) == 0) vid.Description = attribute.Value;
                }

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

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


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

        public string ID { get; set; }

        public string Name { get; set; }

        public string Class { get; set; } = "SVID";

        public string Format { get; set; }

        public int Length { get; set; } = 1;

        public string Description { get; set; }

        public int Seq { get; set; } = -1;

        #endregion

        #region [ VID ] -------------------------------------------------------

        public VID()
        {
        }

        public VID(string id, string name="", string vidClass="", string format="", string description="")
        {
            this.ID = id;
            this.Name = name;
            this.Class = vidClass;
            this.Format = format;
            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.Format) == false) sb.Append($", Format={this.Format}");
            if (string.IsNullOrEmpty(this.Description) == false) sb.Append($", Description={this.Description}");

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

        public string ToXml(string indent = "", bool inSeq = false)
        {
            StringBuilder sb = new StringBuilder();

            if (inSeq) sb.Append($"{indent}<SVID SEQ='{this.Seq}'");
            else sb.Append($"{indent}<VID");

            sb.Append($" ID='{this.ID}'");
            sb.Append($" NAME='{this.Name}'");
            if (!string.IsNullOrEmpty(this.Format))
            {
                sb.Append($" FORMAT='{this.Format}'");
                sb.Append($" LENGTH='{this.Length}'");
            }
            if (!string.IsNullOrEmpty(this.Description)) sb.Append($" DESCRIPTION='{this.Description}'");
            sb.Append($" />");

            return sb.ToString();
        }

        #endregion

    }

}
