import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityNewHeroGiftModel, ActivityNewHeroGiftModelType } from '../../db/ActivityNewHeroGift';
import { NewHeroGiftData } from '../../domain/activityField/newHeroGiftField';
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 getNewHeroGiftsActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.DAILY_RMB_GIFTS, new Date())

    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerData = await getPlayerNewHeroGiftsData(activityData.activityId, serverId, roleId);
    return playerData
}

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

    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new NewHeroGiftData(activityData, createTime);
    let playerRecord: ActivityNewHeroGiftModelType = await ActivityNewHeroGiftModel.findData(serverId, activityId, roleId);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 任务活动完成后会获得点数
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function newHeroGiftPoint(serverId: number, activityId: number, roleId: string, addPoint: number) {
    await ActivityNewHeroGiftModel.addPoint(serverId, activityId, roleId, addPoint);
}
