using CloudGaming.Code.Account.Contract;
using CloudGaming.Code.DataAccess;
using CloudGaming.DtoModel.Account.User;

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

namespace CloudGaming.Code.Account.UserCurrency;

/// <summary>
/// 游戏扣除钻石
/// </summary>
/// <param name="dao"></param>
/// <param name="userId"></param>
/// <param name="userCurrencyType"></param>
/// <param name="userCurrency"></param>
public class GameDiamondConsumeMoney(DAO dao, int userId, UserCurrencyType userCurrencyType, T_User_Currency userCurrency = null) : IUserConsumeMoney
{
    /// <summary>
    /// 玩游戏消耗钻石
    /// </summary>
    /// <param name="money"></param>
    /// <param name="currencyLogId"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public async Task<(bool, int)> ConsumeMoneyAsync(decimal money, int currencyLogId)
    {
        var currency = await new DiamondConsumeMoney(dao, userId, userCurrencyType, userCurrency).AddUserCurrencyAsync(money);
        if (currency == null)
        {
            throw new Exception("扣费失败");
        }
        UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
        T_User_Currency_Log currency_Log = null;
        if (currencyLogId > 0)
        {
            currency_Log = await dao.DaoUser.Context.T_User_Currency_Log.FirstOrDefaultAsync(it => it.Id == currencyLogId);
        }
        if (currency_Log == null)
        {
            currency_Log = new T_User_Currency_Log()
            {
                Consume = 0,
                ConsumeType = (int)consumeType,
                CreateAt = DateTime.Now,
                CurrencyType = (int)userCurrencyType,
                Remarks = $"",
                UpdateAt = DateTime.Now,
                UserId = userId,
            };
            await dao.DaoUser.Context.T_User_Currency_Log.AddAsync(currency_Log);
        }
        currency_Log.Consume += money;
        currency_Log.Remarks = $"用户玩游戏{consumeType}[{Math.Abs(money)}]{userCurrencyType}";
        await dao.DaoUser.Context.SaveChangesAsync();
        if (userCurrency != null)
        {
            userCurrency.CurrencyMoney = currency.CurrencyMoney;
            userCurrency.UpdateAt = currency.UpdateAt;
            userCurrency.Id = currency.Id;
            userCurrency.CurrencyName = currency.CurrencyName;
            userCurrency.CreateAt = currency.CreateAt;
            userCurrency.CurrencyType = currency.CurrencyType;
        }
        else
        {
            userCurrency = currency;
        }
        return (true, currency_Log?.Id ?? 0);
    }


    /// <summary>
    /// 用户余额扣除
    /// </summary>
    /// <param name="money"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>

    public async Task<bool> ConsumeMoneyAsync(decimal money)
    {
        return await new DiamondConsumeMoney(dao, userId, userCurrencyType, userCurrency).ConsumeMoneyAsync(money);
    }


}
