using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;

namespace Rdh.SocketServer.Client.Models
{
    public class EleMedKitProtocolHelper
    {
        private static readonly Char[] _lineSpiltor = new char[] { '\n' };
        private static readonly char[] _blockSpiltor = new char[] { '|' };
        /// <summary>
        /// 将数据按照行拆分
        /// </summary>
        /// <param name="body"></param>
        /// <returns>拆分后的所有行（行内容中去除了标识符）</returns>
        public static String[] GetContentLines(String body)
        {
            String[] lines = body.Split(_lineSpiltor, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < lines.Length; i++) 
            {
                lines[i] = GetLineContent(lines[i]);
            }
            return lines;
        }
        public static String[] GetContentBlocks(String line) 
        {
            String[] blocks = line
                .Trim()
                .TrimStart(EleMedKitProtocolFlags.LineStart.First())
                //.Split(_blockSpiltor, StringSplitOptions.RemoveEmptyEntries);
                .Split(_blockSpiltor);
            for (int i = 0; i < blocks.Length; i++)
            {
                blocks[i] = blocks[i].Trim();
            }
            return blocks;
        }
        public static String GetLineContent(String line) 
        {
            return line.Trim().TrimStart(EleMedKitProtocolFlags.LineStart.First());
        }
        public static DateTime? GetDateTimeByLineContent(String line) 
        {
            line = GetLineContent(line);
            DateTime rlt;
            if(DateTime.TryParseExact(line,"yyyy|MM|dd|hh|mm|ss", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out rlt))
            {
                return rlt;
            }
            return null;
        }
        public static String BuildLinePadContent(String? content) 
        {
            return (content ?? String.Empty).PadLeft(EleMedKitProtocolFlags.LinePadTotalLenght, EleMedKitProtocolFlags.LinePad.First());
        }
        public static BaseEleMedKitProtocol GetRequestEleMedKitProtocol(String command) 
        {
            switch (command) 
            {
                case EleMedKitCommand.RequestLocation:
                    return new LocationRequestProtocol();
                case EleMedKitCommand.RequestClock:
                    return new ClockRequestProtocol();
                case EleMedKitCommand.RequestPatientOperation:
                    return new RequestPatientOperationProtocol();
                case EleMedKitCommand.RequestPatientOperationUsage:
                    return new RequestOperationUsageProtocol();
                case EleMedKitCommand.RequestReturnMedKit:
                    return new RequestMedKitReturnProtocol();
                case EleMedKitCommand.RequestSupplyList:
                    return new RequestSupplyDrugProtocol();
                case EleMedKitCommand.RequestDrugLend:
                    return new RequestDrugLendProtocol();
                default:
                    return new UnSupportProtocol();
            }
        }
    }
}
