import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityRefreshTaskModel, ActivityRefreshTaskModelType } from '../../db/ActivityRefreshTask';
import { ActivityRefreshTaskPointModel, ActivityRefreshTaskPointModelType } from '../../db/ActivityRefreshTaskPoint';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { RefreshTaskData, RefreshTaskItem } from '../../domain/activityField/refreshTaskField';


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


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerRefreshTaskData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new RefreshTaskData(activityData, createTime);
    let playerRecords: ActivityRefreshTaskModelType[] = await ActivityRefreshTaskModel.findData(serverId, activityId, roleId, playerData.roundIndex);
    let pointRecordData: ActivityRefreshTaskPointModelType = await ActivityRefreshTaskPointModel.findData(serverId, activityId, roleId);
    playerData.setPlayerRecords(playerRecords, pointRecordData);
    return playerData;
}


