using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNettyFrom.config;

namespace DotNettyFrom.common
{
    [Serializable]
    public class UIInfoModel
    {
        public int action = int.MinValue;
        public string time = DateTime.Now.ToLongTimeString();
        public string info = "";

        public string cmd = "";

        public int cmdId = int.MinValue;


        //1 发送的. 0:接收的, -1.日常信息
        public int SendType = SystemConfig.RecType;


        public UIInfoModel(string info, string cmd)
        {
            this.info = info;
            this.cmd = cmd;
            cmdId = CalcCmdId(cmd);
        }


        public UIInfoModel(string info)
        {
            this.info = info;
        }

        public UIInfoModel setAction(int action)
        {
            this.action = action;
            return this;
        }

        public UIInfoModel setSendType(int SendType)
        {
            this.SendType = SendType;
            return this;
        }

        public UIInfoModel setCmd(string cmd)
        {
            this.cmd = cmd;
            cmdId = CalcCmdId(cmd);
            return this;
        }


        public UIInfoModel setInfo(string info)
        {
            this.info = info;
            return this;
        }


        public override string ToString()
        {
            string res = time + " - ";
            if (!info.Trim().Equals(""))
            {
                res += this.info;
            }

            if (!cmd.Trim().Equals(""))
            {
                try
                {
                    string cmdIdString = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
                        SystemConfig.FunctionCodeLength * 2);
                    res += ". cmdId=" + cmdIdString;
                    cmdId = CalcCmdId(cmd);
                }
                catch (Exception)
                {
                }
                res += ". cmd=[" + cmd + "]";
            }
            return res + ".";
        }

        public int CalcCmdId(string cmd)
        {
            try
            {
                string cmdId = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
                    SystemConfig.FunctionCodeLength * 2);
                return int.Parse(cmdId);
            }
            catch (Exception)
            {
            }
            return int.MinValue;
        }
    }
}