import { ActivityThirtyDaysModel, ActivityThirtyDaysModelType } from '../../db/ActivityThirtyDays';
import { ThirtyDaysData } from '../../domain/activityField/thirtyDaysField';
import { ActivityThirtyDaysPointRewardModel, ActivityThirtyDaysPointRewardModelType } from '../../db/ActivityThirtyDaysPointReward';
import { getActivitiesByType, getActivityById } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';

/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} type 活动类型 ACTIVITY_TYPE
   * @param {string} roleId 角色Id
   *
   */

export async function thirtyDaysActivity(type: number, serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, type);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerData = await getPlayerThirtyDaysData(activityData.activityId, serverId, roleId);

    return playerData
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerThirtyDaysData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let playerRecords: ActivityThirtyDaysModelType[] = await ActivityThirtyDaysModel.findData(serverId, activityId, roleId);
    let playerPointRecords: ActivityThirtyDaysPointRewardModelType[] = await ActivityThirtyDaysPointRewardModel.findData(activityId, roleId);

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

    // let playerHeroes = await HeroModel.findByRole(roleId);
    // let colorStarHeroes = playerHeroes.filter(obj => { return obj && obj.colorStar > 0 })
    // playerData.setColorStarCount(colorStarHeroes.length);

    return playerData;
}

/**
   * 活动任务添加统计次数
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function addPlayerThirtyDaysTask(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let playerRecords: ActivityThirtyDaysModelType[] = await ActivityThirtyDaysModel.findData(serverId, activityId, roleId);
    let playerPointRecords: ActivityThirtyDaysPointRewardModelType[] = await ActivityThirtyDaysPointRewardModel.findData(activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new ThirtyDaysData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecords);
    playerData.setPlayerPointRecords(playerPointRecords);
    return playerData;
}