/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2019-01-05
 * 时间: 14:08
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using MrWu.Debug;
using MrWu.RabbitMQ;
using RabbitMQ.Client;
using Server.Pack;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GameData;

namespace Server.MQ {
    /// <summary>
    /// 等待处理的消息
    /// </summary>
    public class WaitBeMsg {

        ///// <summary>
        ///// 查慢的原因用的,日志
        ///// </summary>
        //public DateTime logTime;

           // private ServerHead mhead;
        /// <summary>
        /// 消息内容
        /// </summary>
        public PackHead head;

        /// <summary>
        /// 包体数据
        /// </summary>
        public string jsondata;

        /// <summary>
        /// 包头+包体
        /// </summary>
        public string jsonmessage {
            get {
                return message.bodystr;
            }
        }

        /// <summary>
        /// 其他参数
        /// </summary>
        public List<object> pms;

        /// <summary>
        /// 步骤
        /// </summary>
        public int step = 0;

        /// <summary>
        /// 消息
        /// </summary>
        public Message message;

        /// <summary>
        /// 是否处理完自动确认 如果不是自动确认的话，消息需要手动确认，请清理消息内存
        /// </summary>
        public bool AutoAck;

        /// <summary>
        /// 交换机
        /// </summary>
        public string exchange {
            get {
                return message.exchange;
            }
        }

        /// <summary>
        /// 路由键
        /// </summary>
        public string routingkey {
            get {
                return message.routingkey;
            }
        }

        /// <summary>
        /// 消息属性
        /// </summary>
        public IBasicProperties propertis {
            get {
                return message.propertis;
            }
        }

        /// <summary>
        /// 用户数据
        /// </summary>
        public IDictionary<string, object> userData {
            get {
                return message.userData;
            }
        }

        /// <summary>
        /// 是否不需要确认
        /// </summary>
        public bool noAck {
            get {
                return message.noAck;
            }
        }

        /// <summary>
        /// 消息唯一id
        /// </summary>
        public string id {
            get {
                return message.id;
            }
        }

        /// <summary>
        /// 消息投递标签
        /// </summary>
        public ulong deliveryTag {
            get {
                return message.deliveryTag;
            }
        }

        /// <summary>
        /// 是否被重复接收过
        /// </summary>
        public bool redelivered {
            get {
                return message.redelivered;
            }
        }

        /// <summary>
        /// 是否确认过
        /// </summary>
        public bool isAck {
            get;
            private set;
        }

        /// <summary>
        /// 是否要继续添加到消息列表中处理
        /// </summary>
        public bool isAdd;

        /// <summary>
        /// 数据
        /// </summary>
        public object[] datas { get; private set; } = new object[10];

        /// <summary>
        /// 上次的任务
        /// </summary>
        public Task task;

        /// <summary>
        /// 确认消息
        /// </summary>
        /// <param name="mulitple"></param>
        /// <returns></returns>
        public bool Ack(bool mulitple = false) {

            if (isAck) {
                Debug.Warning("消息已被确认过,请勿重复确认!");
                return false;
            }

            isAck = true;

            return message.Ack(mulitple);
        }

        /// <summary>
        /// 是否被拒绝确认
        /// </summary>
        /// <param name="mulitple">是否多条消息</param>
        /// <param name="requeue">是否重新入列</param>
        /// <returns></returns>
        public bool NAck(bool mulitple = false, bool requeue = false) {

            if (isAck) {
                Debug.Warning("消息已被确认过,请勿重复确认!");
                return false;
            }

            isAck = true;

            return message.NAck(mulitple, requeue);
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        internal void Clear() {
            MessagePool.PutMessage(ref message);
            isAck = false;
            step = 0;
            isAdd = false;
            this.head = null;
            this.jsondata = null;
            this.pms = null;
            this.task = null;
            int len = datas.Length;
            for (int i = 0; i < len; i++) {
                datas[i] = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public WaitBeMsg() {
            //this.message = msg;
            //jsondata = JsonPack.GetPackHead(msg.bodystr, out head);
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="msg"></param>
        ///// <param name="ack"></param>
        //public WaitBeMsg(BasicGetResult msg, bool ack) {
        //    this.deliverytag = msg.DeliveryTag;
        //    jsonmessage = Encoding.UTF8.GetString(msg.Body);
        //    jsondata = JsonPack.GetPackHead(jsonmessage, out head);
        //    this.exchange = msg.Exchange;
        //    this.routingKey = msg.Exchange;
        //    this.ack = ack;
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="deliverytag"></param>
        ///// <param name="body"></param>
        ///// <param name="exchange"></param>
        ///// <param name="routingkey"></param>
        ///// <param name="ack"></param>
        //public WaitBeMsg(ulong deliverytag, byte[] body, string exchange, string routingkey, bool ack) {
        //    this.deliverytag = deliverytag;
        //    jsonmessage = Encoding.UTF8.GetString(body);
        //    jsondata = JsonPack.GetPackHead(jsonmessage, out head);
        //    this.exchange = exchange;
        //    this.routingKey = routingkey;
        //    this.ack = ack;
        //}
    }
}
