using CloudGaming.Code.Account;
using CloudGaming.DtoModel.Account.User;
using CloudGaming.DtoModel.Account.User.Cache;
using CloudGaming.DtoModel.SevenSign;
using CloudGaming.Model.DbSqlServer.Db_Phone;

using Org.BouncyCastle.Bcpg.OpenPgp;

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

namespace CloudGaming.Code.Other;

/// <summary>
/// 七天签到
/// </summary>
public class SevenSignBLL : CloudGamingBase
{
    public SevenSignBLL(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    /// <summary>
    /// 七天签到
    /// </summary>
    /// <returns></returns>
    public async Task<SevenDayDto> GetSevenSignList()
    {
        int consecutiveSignDays = 0;
        bool isSignSevenDay = false;
        if (_UserId > 0)
        {
            //获取用户签到状态
            var userSevenInfo = await UserInfo.GetUserSevenDay(Dao, RedisCache);
            consecutiveSignDays = userSevenInfo.ConsecutiveSignDays;
            isSignSevenDay = userSevenInfo.IsSignSevenDay;

        }
        SevenDayDto sevenDays = new SevenDayDto();
        var list = Cache.SevenDayList;
        for (int i = 0; i < list.Count; i++)
        {
            var item = list[i];
            SevenDay sevenDay = new SevenDay()
            {
                Image = item.ImageId,
                SignId = item.Id,
                SignName = item.Name ?? "",
                Status = i < consecutiveSignDays ? 1 : (i == consecutiveSignDays ? 0 : 2)
            };
            if (i == consecutiveSignDays)
            {
                if (isSignSevenDay)
                {

                    sevenDay.Status = 2;
                }
            }
            sevenDays.List.Add(sevenDay);
        }
        sevenDays.IsSign = isSignSevenDay;
        return sevenDays;
    }

    /// <summary>
    /// 七天签到
    /// </summary>
    /// <returns></returns>
    public async Task<BaseResponse<bool>> SevenSignToday()
    {
        if (_UserId == 0)
        {
            throw MessageBox.Show(ResponseCode.UserNotLogin, "用户未登录");
        }
        //获取用户签到状态
        var userSevenInfo = await UserInfo.GetUserSevenDay(Dao, RedisCache);

        if (userSevenInfo.IsSignSevenDay)
        {
            throw MessageBox.Show(ResponseCode.Error, "用户已经签到过了");
        }

        // 获取用户最后一条签到记录
        var log = await Dao.DaoPhone.Context.T_User_SignDays
            .Where(it => it.UserId == _UserId)
            .OrderByDescending(it => it.Id)
            .FirstOrDefaultAsync();

        if (userSevenInfo.ConsecutiveSignDays == 7)
        {
            userSevenInfo.ConsecutiveSignDays = 0;
        }

        if (Cache.SevenDayList.Count < userSevenInfo.ConsecutiveSignDays)
        {
            throw MessageBox.Show(ResponseCode.Error, "签到配置错误");
        }
        var sevenRaw = Cache.SevenDayList[userSevenInfo.ConsecutiveSignDays]?.Rewards;
        string msg = "签到成功！";
        if (sevenRaw != null && sevenRaw.Count > 0)
        {
            foreach (var raw in sevenRaw)
            {
                var isSuccess = await this.UserConsumeDiamondMoneyAsync((UserCurrencyType)raw.CurrencyType, raw.AwardNum, $"七天签到-第{userSevenInfo.ConsecutiveSignDays + 1}天礼包");
                if (!isSuccess)
                {

                    throw MessageBox.Show(ResponseCode.Error, "奖励发放失败");
                }
                msg += $"获得{(UserCurrencyType)raw.CurrencyType}*{raw.AwardNum}，";
            }
            msg = msg.TrimEnd('，');
            msg += "。";
            await this.SendUserMessageAsync("七天签到奖励", msg);
        }
        T_User_SignDays t_User_SignDays = new T_User_SignDays()
        {
            CreatTime = DateTime.Now,
            SignDayNum = userSevenInfo.ConsecutiveSignDays + 1,
            SignDayTime = DateTime.Now,
            UpdateTime = DateTime.Now,
            UserId = _UserId,
            Desc = $"七天签到-第{userSevenInfo.ConsecutiveSignDays + 1}天",
        };


        // 保存到数据库
        await Dao.DaoPhone.Context.T_User_SignDays.AddAsync(t_User_SignDays);
        await Dao.DaoPhone.Context.SaveChangesAsync();
        userSevenInfo.IsSignSevenDay = true;
        userSevenInfo.ConsecutiveSignDays++;
        userSevenInfo.LastSevenDayTime = DateTime.Now;
        await this.SaveUserInfoCacheChangesAsync();
        return new BaseResponse<bool>(ResponseCode.Success, msg) { };
    }

}
