using System.Runtime.Serialization;

namespace Core.Protocol
{
    /// <summary>
    /// 基础响应分页
    /// </summary>
    public class BaseReplyPagination
    {
        /// <summary>
        /// 当前页
        /// </summary>
        [DataMember]
        public int Current_Page { get; set; }
            = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        [DataMember]
        public int Page_Size { get; set; }
            = 1000;

        /// <summary>
        /// 总记录数
        /// </summary>
        [DataMember]
        public int Total { get; set; }
            = 0;
    }

    #region BaseReply

    /// <summary>
    /// 基础响应
    /// </summary>
    [DataContract]
    public class BaseReply
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        [DataMember]
        public Guid ExecGuid { get; set; }

        /// <summary>
        /// 执行码
        /// </summary>
        [DataMember]
        public int Code { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        [DataMember]
        public bool Success { get; set; }

        /// <summary>
        /// 执行消息
        /// </summary>
        [DataMember]
        public string Msg { get; set; }
            = string.Empty;

        /// <summary>
        /// 数据API
        /// </summary>
        [DataMember]
        public string? DataApi { get; set; }

        /// <summary>
        /// 数据主体
        /// </summary>
        [DataMember]
        public object? DataTable { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        [DataMember]
        public object? DataObject { get; set; }

        /// <summary>
        /// 列信息
        /// </summary>
        [DataMember]
        public List<ReplyColumn>? Columns { get; set; }
            = new List<ReplyColumn>();

        /// <summary>
        /// 分页信息
        /// </summary>
        [DataMember]
        public BaseReplyPagination Pagination { get; set; }
            = new BaseReplyPagination();

        #region Create

        /// <summary>
        /// 创建基础响应对象
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static BaseReply Create(string msg)
        {
            var reply = new BaseReply();
            reply.Msg = msg;

            reply.ReplySuccess();
            return reply;
        }

        #endregion

        #region Create

        /// <summary>
        /// 创建基础响应对象
        /// </summary>
        /// <returns></returns>
        public static BaseReply Create(List<object> data, List<ReplyColumn>? columns = null)
        {
            var reply = new BaseReply();
            reply.DataTable = data;
            reply.Columns = columns;

            if (data != null)
                reply.Pagination.Total = data.Count;

            reply.ReplySuccess();
            return reply;
        }

        #endregion

        #region Create

        /// <summary>
        /// 创建基础响应对象
        /// </summary>
        /// <returns></returns>
        public static BaseReply Create<T>(List<T> data, List<ReplyColumn>? columns = null)
        {
            var reply = new BaseReply();
            reply.DataTable = data;
            reply.Columns = columns;

            if (data != null)
                reply.Pagination.Total = data.Count;

            reply.ReplySuccess();
            return reply;
        }

        #endregion

        #region CreateFalt

        /// <summary>
        /// 创建基础响应对象
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static BaseReply CreateFalt(string msg = "失败")
        {
            var reply = new BaseReply();
            reply.Success = false;
            reply.Code = -1;
            reply.Msg = msg;
            return reply;
        }

        #endregion

        #region ReplySuccess

        /// <summary>
        /// 成功
        /// </summary>
        public void ReplySuccess()
        {
            Success = true;
            Code = 200;

            if (string.IsNullOrEmpty(Msg))
                Msg = "成功";
        }

        #endregion

        #region ReplyFalt

        /// <summary>
        /// 失败
        /// </summary>
        public void ReplyFalt(string msg = "失败", int code = -1)
        {
            Success = false;
            Code = code;
            Msg = msg;
        }

        #endregion
    }

    #endregion

    /// <summary>
    /// 基础响应
    /// </summary>
    [DataContract]
    public class Base2Reply
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        [DataMember]
        public bool Success { get; set; }

        /// <summary>
        /// 执行码
        /// </summary>
        [DataMember]
        public int Code { get; set; }

        /// <summary>
        /// 执行消息
        /// </summary>
        [DataMember]
        public string Msg { get; set; }
            = string.Empty;

        /// <summary>
        /// 数据类型
        /// </summary>
        [DataMember]
        public ReplyDataType DataType { get; set; }
            = ReplyDataType.Object;

        /// <summary>
        /// 数据主体
        /// </summary>
        [DataMember]
        public object? Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        public void ReplySuccess(string data)
        {
            Success = true;
            Code = 0;
            Msg = "成功";
            Data = data;
        }

        /// <summary>
        /// 成功
        /// </summary>
        public void ReplySuccess()
        {
            Success = true;
            Code = 0;
            Msg = "成功";
        }

        /// <summary>
        /// 失败
        /// </summary>
        public void ReplyFalt()
        {
            Success = false;
            Code = -1;
            Msg = "失败";
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        public void ReplyFalt(string msg, string? data = null)
        {
            Success = false;
            Code = -1;
            Msg = msg;
            Data = data;
        }
    }

    /// <summary>
    /// 响应数据类型
    /// </summary>
    public enum ReplyDataType
    {
        /// <summary>
        /// 空类型
        /// </summary>
        Empty,

        /// <summary>
        /// 字符串类型
        /// </summary>
        String,

        /// <summary>
        /// 对象类型
        /// </summary>
        Object,

        /// <summary>
        /// 列表类型
        /// </summary>
        ObjectList,

        /// <summary>
        /// 动态对象类型
        /// </summary>
        ExpandoObject,

        /// <summary>
        /// 动态对象类型
        /// </summary>
        ExpandoObjectList,
    }

    /// <summary>
    /// 响应列
    /// </summary>
    public class ReplyColumn
    {
        public ReplyColumn()
        {
        }


        public ReplyColumn(string name, string caption)
        {
            Name = name;
            Caption = caption;
        }

        /// <summary>
        /// 列名
        /// </summary>
        [DataMember]
        public string Name { get; set; }
            = string.Empty;

        /// <summary>
        /// 列标题
        /// </summary>
        [DataMember]
        public string Caption { get; set; }
            = string.Empty;

        /// <summary>
        /// 列宽度
        /// </summary>
        [DataMember]
        public double Width { get; set; }

        /// <summary>
        /// 是否可见
        /// </summary>
        [DataMember]
        public bool IsVisible { get; set; }
            = true;

        /// <summary>
        /// 格式化字符串
        /// </summary>
        [DataMember]
        public string Format { get; set; }
            = string.Empty;
    }
}