using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Rdh.SocketServer.Client.Models
{
    public abstract class BaseEleMedKitProtocol
    {
        public virtual string? Command { get; set; }
        /// <summary>
        /// 获取或设置数据的最小行数
        /// </summary>
        public virtual int MinLines { get; set; } = 1;
        public virtual string AddLineContent(string? line)
        {
            return line == null
                ? string.Empty
                : EleMedKitProtocolFlags.LineStart + line + EleMedKitProtocolFlags.LineEnd;
        }
        public virtual string AddBlockContent(string? content)
        {
            return content == null
                ? string.Empty
                : EleMedKitProtocolFlags.BlockSpiltor + content;
        }
        public virtual string AddList<T>(IEnumerable<T>? list)
        {
            if (list == null || list.Count() == 0)
            {
                return string.Empty;
            }
            StringBuilder builder = new StringBuilder();
            if (typeof(T) == typeof(string))
            {
                foreach (T t in list)
                {
                    builder.Append(t as string);
                }
            }
            else
            {
                foreach (T t in list)
                {
                    builder.Append(t.ToString());
                }
            }
            return builder.ToString();
        }
        public abstract string GetContent();
        /// <summary>
        /// 检验数据行并进行解析
        /// </summary>
        /// <param name="lines"></param>
        /// <returns>是否检验成功</returns>
        public virtual bool ParseContent(string[] lines)
        {
            if (lines == null || lines.Length < 1)
            {
                return false;
            }
            Command = lines[0];
            return true;
        }
        public override string ToString()
        {
            return EleMedKitProtocolFlags.Start + GetContent() + EleMedKitProtocolFlags.End;
        }
    }
}
