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

namespace DDUtilityApp.SECS
{

    public class SECSItem : IList<SECSItem>
    {

        #region [ Propeties ] -------------------------------------------------

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

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

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

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

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

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

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

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


        public SECSItem Parent { get; set; } = null;

        public List<SECSItem> ChildItems { get; set; } = new List<SECSItem>();

        #endregion

        #region [ SECSItem ] -------------------------------------------------

        public SECSItem()
        {
        }

        public SECSItem(SECSItem parent)
        {
            this.Parent = parent;
        }

        public SECSItem(string stream, string function)
        {
            this.Stream = stream;
            this.Function = function;
        }

        #endregion

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

        public override string ToString()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat($"<{this.Format} ");
                sb.AppendFormat($"[{this.Length}] ");
                if (!string.IsNullOrEmpty(this.Name)) sb.AppendFormat($"{this.Name} ");
                if (new string[] { "L" }.Contains(this.Format) == false) sb.AppendFormat($"'{this.Value}' ");
                if (new string[] { "L" }.Contains(this.Format) == false) sb.AppendFormat($">");

                return sb.ToString();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                return string.Empty;
            }
        }

        public string ToFullString(string intendted = "")
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(intendted + this.ToString());
                foreach(SECSItem item in this.ChildItems)
                    sb.AppendFormat(item.ToFullString(intendted + "    "));

                return sb.ToString();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                return string.Empty;
            }
        }

        #endregion

        #region [ IList<SECSItem> ] -------------------------------------------

        public int Count => this.ChildItems.Count;

        public bool IsReadOnly => throw new NotImplementedException();

        public SECSItem this[int index] { get => this.ChildItems[index]; set => this.ChildItems[index] = value; }

        public int IndexOf(SECSItem item)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, SECSItem item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public void Add(SECSItem item)
        {
            item.Parent = this;
            this.ChildItems.Add(item);
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(SECSItem item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(SECSItem[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool Remove(SECSItem item)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<SECSItem> GetEnumerator()
        {
            return this.ChildItems.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

    }

}
