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 } from '../../domain/activityField/dailyChallengesField';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';


/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function dailyChallengesActivity(serverId: number, roleId: string) {

    let activityDataArray: ActivityModelType[] = await getActivitiesByType(serverId, ACTIVITY_TYPE.TASK_DAILY_CHALLENGES);
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityData.activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyChallengesData(activityData, createTime, serverTime);
    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 getActivityById(activityId);
    let playerRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyChallengesData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecords);

    return playerData;
}


