using Bogus.DataSets;

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 DiamondConsumeMoney(DAO dao, int userId, UserCurrencyType userCurrencyType, T_User_Currency userCurrency = null) : IUserConsumeMoney
{
    /// <summary>
    /// 用户余额扣除
    /// </summary>
    /// <param name="money"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>

    public async Task<bool> ConsumeMoneyAsync(decimal money)
    {
        T_User_Currency? currency = await AddUserCurrencyAsync(money);
        UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
        var currency_Log = new T_User_Currency_Log()
        {
            Consume = money,
            ConsumeType = (int)consumeType,
            CreateAt = DateTime.Now,
            CurrencyType = (int)userCurrencyType,
            Remarks = $"于{DateTime.Now:yyyy-MM-dd HH:mm:ss}{consumeType}[{Math.Abs(money)}]{userCurrencyType};",
            UpdateAt = DateTime.Now,
            UserId = userId,
        };
        await dao.DaoUser.Context.T_User_Currency_Log.AddAsync(currency_Log);
        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;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="money"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public async Task<T_User_Currency?> AddUserCurrencyAsync(decimal money)
    {
        var currency = await dao.DaoUser.Context.T_User_Currency.Where(it => it.CurrencyType == (int)userCurrencyType && it.UserId == userId).FirstOrDefaultAsync();
        if (currency == null)
        {
            currency = new T_User_Currency()
            {
                CreateAt = DateTime.Now,
                CurrencyMoney = 0,
                CurrencyName = userCurrencyType.ToString(),
                CurrencyType = (int)userCurrencyType,
                UserId = userId,
                UpdateAt = DateTime.Now,
            };
            await dao.DaoUser.Context.T_User_Currency.AddAsync(currency);
            await dao.DaoUser.Context.SaveChangesAsync();
        }
        currency.CurrencyMoney += money;
        if (currency.CurrencyMoney < 0)
        {
            throw new Exception("余额不足");
        }

        return currency;
    }
}
