import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityNewHeroGachaModel, ActivityNewHeroGachaModelType } from '../../db/ActivityNewHeroGacha';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { NewHeroGachaData } from '../../domain/activityField/newHeroGachaField';


/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function newHeroGachaActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityDataArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.NEW_HERO_GACHA, new Date());
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerRecord: ActivityNewHeroGachaModelType[] = await ActivityNewHeroGachaModel.findData(serverId, activityData.activityId, roleId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new NewHeroGachaData(activityData, createTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}


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

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

    return playerData;
}




