import { ActivityTurntableModel } from "../../db/ActivityTurntableRec";
import { RoleModel } from "../../db/Role";
import { LuckyTurntableData } from "../../domain/activityField/luckyTurntableField";
import { shouldRefresh } from "../../pubUtils/util";
import { getRoleCreateTime, getServerCreateTime } from "../redisService";
import { getActivityById } from "./activityService";

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
 export async function getPlayerLuckyTurntableData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new LuckyTurntableData(activityData, createTime, serverTime);

    let playerRecord = await ActivityTurntableModel.findByActivityId(serverId, activityId, roleId, playerData.roundIndex);
    if(playerRecord && shouldRefresh(playerRecord.refTodayCount, new Date())) {
        playerRecord = await ActivityTurntableModel.refreshTodayCount(serverId, activityId, roleId, playerData.roundIndex);
    }
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

export async function getPlayerLuckyTurntableDataShow(activityId: number, serverId: number, roleId: string) {
    let playerData = await getPlayerLuckyTurntableData(activityId, serverId, roleId);
    if(playerData && playerData.canShow && playerData.canShow()) {
        return playerData.getShowResult();
    }
    return null
}