import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyChallengesModel, ActivityDailyChallengesModelType } from '../../db/ActivityDailyChallenges';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { DailyChallengesData, DailyItem } from '../../domain/activityField/dailyChallengesField';


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


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyChallengesData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let playerRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityId, roleId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new DailyChallengesData(activityData, createTime);
    playerData.setPlayerRecords(playerRecords);

    return playerData;
}


