import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityVipRechargeMoneyModel, ActivityVipRechargeMoneyModelType } from '../../db/ActivityVipRechargeMoney';
import { VipRechargeMoneyData } from '../../domain/activityField/vipRechargeMoneyField';
import { ServerlistModel } from '../../db/Serverlist';
import { RoleModel } from '../../db/Role';
import { getActivitiesByType, getActivityById } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';

/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} type 活动类型 ACTIVITY_TYPE
   * @param {string} roleId 角色Id
   *
   */

export async function vipRechargeMoneyActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.VIP_RECHARGE_MONEY);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerRecord: ActivityVipRechargeMoneyModelType = await ActivityVipRechargeMoneyModel.findData(serverId, roleId, activityData.activityId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new VipRechargeMoneyData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerVipRechargeMoneyData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    if (!activityData) {
        return null;
    }
    let playerRecord: ActivityVipRechargeMoneyModelType = await ActivityVipRechargeMoneyModel.findData(serverId, roleId, activityId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new VipRechargeMoneyData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}



/**
   * 统计充值金额
   *
   * @param {number} serverId 区Id
   * @param {string} roleId 角色Id
   * @param {string} RMB 金额
   *
   */
export async function addVipRechargeMoney(roleId: string, serverId: number, RMB: number) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.VIP_RECHARGE_MONEY);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    await ActivityVipRechargeMoneyModel.addRMB(serverId, roleId, activityData.activityId, RMB);
}

