using BossServer.db;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using CSharpUtil.Extensions.Time;

namespace BossServer.server
{
    /// <summary>
    /// 邮件类型枚举
    /// </summary>
    public enum EMailType
    {
        /// <summary>
        /// 系统邮件
        /// </summary>
        SystemMail = 1,
        /// <summary>
        /// 好友雇佣 
        /// </summary>
        [Obsolete]
        HireCoin,
        /// <summary>
        /// PVP阶段奖励
        /// </summary>
        [Obsolete]
        PvpLeagueReward,
        /// <summary>
        /// pvp上榜奖励
        /// </summary>
        PvpRankReward,
        /// <summary>
        /// 公会申请被拒
        /// </summary>
        [Obsolete]
        GuildApplyRefuse,
        /// <summary>
        /// 公会捐献结算
        /// </summary>
        [Obsolete]
        GuildDonateSettle,
        /// <summary>
        /// 公会钻石礼包
        /// </summary>
        [Obsolete]
        GuildCashGiftReward,
        /// <summary>
        /// 公会公告/通知邮件
        /// </summary>
        [Obsolete]
        GuildNotice

    }

    /// <summary>
    /// 邮件发送功能
    /// </summary>
    class EmailProc
    {

        /// <summary>
        /// 邮件最大容量100封
        /// </summary>
        internal const int MaxMailContent = 100;

        /// <summary>
        /// 邮件最大过期时间7天
        /// </summary>
        internal const int MaxMailExpireTs = 604800;

        /// <summary>
        /// Mysql邮件日志表
        /// </summary>
        const string MailLog_TableName = "tab_mailrecord";
        /// <summary>
        /// 系统邮件 - 发送世界boss战奖励邮件
        /// </summary>
        /// <param name="zoneid"></param>
        /// <param name="uid"></param>
        /// <param name="index"></param>
        /// <param name="rewardName"></param>
        /// <param name="reward"></param>
        public static void SendWorldBossReward(int zoneid, string uid, int index,string rewardName, string reward)
        {
            var mail = new Email(EMailType.SystemMail, "世界boss奖励", $"恭喜您在世界boss战斗中输出伤害排{index}名,获得{rewardName}奖励!", reward);
            InsertMail(zoneid, uid, mail);
        }
        /// <summary>
        /// 插入邮件
        /// </summary>
        /// <param name="zoneid"></param>
        /// <param name="uid"></param>
        /// <param name="mail"></param>
        private static int InsertMail(int zoneid, string uid, Email mail)
        {
            var mem = Redis.Ins.GetDatabase(0);
            var key_id = MemKey_User.Mail_CurId_int(zoneid, uid);
            var key_queue = MemKey_User.Mail_Queue_hash(zoneid, uid);
            mail.insertts = (int)DateTime.Now.ToUnixTimeStamp();
            mail.mailId = (int)mem.StringIncrement(key_id);
            if (!mem.HashSet(key_queue, mail.mailId.ToString(), mail.ToString()))
            {                 // 重试下
                mail.mailId = (int)mem.StringIncrement(key_id);
                if (!mem.HashSet(key_queue, mail.mailId.ToString(), mail.ToString()))
                {

                }
            }
            logMail(zoneid, uid, mail);                                   // 将邮件写入Mysql中
            return mail.mailId;
        }
        /// <summary>
        /// 写邮件日志
        /// </summary>
        /// <param name="zoneid"></param>
        /// <param name="uid"></param>
        /// <param name="mail"></param>
        private static void logMail(int zoneid, string uid, Email mail)
        {
            var sql = $" Insert Into {MailLog_TableName} (`mailId`, `zoneid`, `appendix`, `type`, `sender_name`, `sender_uid`, `to_uid`, `title`, `content`, `tag`) " +
                $"values({mail.mailId},{zoneid},{mail.appendix}, {mail.type}, {mail.sender_name}, {mail.sender_uid}, {uid}, {mail.title}, {mail.content}, {mail.tag})";

            var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
            Debug.Assert(n > 0, $"{sql} 执行失败!");
        }

    }

    public class Email
    {
        public int mailId = 0;
        public string sender_uid;
        public string sender_name;
        public string appendix;
        public int type;
        public string title;
        public string content;
        public object tag;
        public int readts = 0;
        public int drawedts = 0;
        public int insertts = 0;
        public bool isDrawed() => drawedts > 0;
        public bool isExistsReward() => !string.IsNullOrEmpty(appendix);
        public int ExpireTs() => insertts + EmailProc.MaxMailExpireTs;


        public Email(EMailType emailType,string title, string content,string reward,string uid_sender="系统",string name_sender="系统") {
            this.type =(int) emailType;
            this.title = title;
            this.content = content;
            this.appendix = reward;
            this.sender_uid = uid_sender;
            this.sender_name = name_sender;


        }

        public override string ToString()
        {
            return new JObject(this).ToString();
        }
    }
}
