import { ACTIVITY_TYPE, TASK_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityRechargeMoneyModel, ActivityRechargeMoneyModelType } from '../../db/ActivityRechargeMoney';
import { RechargeMoneyData, RechargeMoneyItem } from '../../domain/activityField/rechargeMoneyField';
import moment = require('moment');
import { ServerlistModel } from '../../db/Serverlist';
import { RoleModel } from '../../db/Role';

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

export async function rechargeMoneyActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.RECHARGE_MONEY, new Date())

    let activityData = activityArray[0];
    let playerRecord: ActivityRechargeMoneyModelType[] = await ActivityRechargeMoneyModel.findData(serverId, roleId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new RechargeMoneyData(activityData, createTime);
    playerData.setPlayerRecords(playerRecord);
    delete playerData.recordArray;
    return playerData;

    // let playerDataArray = [];
    // for (let activityData of activityArray) {
    //     let playerData = await getPlayerRechargeMoneyData(activityData.activityId, serverId, roleId);
    //     playerDataArray.push(playerData)
    // }
    // return playerDataArray
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerRechargeMoneyData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let playerRecord: ActivityRechargeMoneyModelType[] = await ActivityRechargeMoneyModel.findData(serverId, roleId);

    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new RechargeMoneyData(activityData, createTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}



/**
   * 统计充值金额
   *
   * @param {number} serverId 区Id
   * @param {string} roleId 角色Id
   * @param {string} RMB 金额
   *
   */
export async function addRechargeMoney(roleId: string, serverId: number, RMB: number) {
    let beginTime = moment(new Date()).startOf('d').toDate();
    let endTime = moment(new Date()).endOf('d').toDate();
    await ActivityRechargeMoneyModel.addRMB(serverId, roleId, RMB, beginTime, endTime);
}

