import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityGrowthModel, ActivityGrowthModelType } from '../../db/ActivityGrowth';
import { ActivityGrowthPointModel, ActivityGrowthPointModelType } from '../../db/ActivityGrowthPoint';
import { HeroModel } from '../../db/Hero';
import { GrowthData } from '../../domain/activityField/growthField';
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 growthActivity(serverId: number, roleId: string) {
    let activityDataArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.TASK_GROWTH);
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerRecords: ActivityGrowthModelType[] = await ActivityGrowthModel.findData(serverId, activityData.activityId, roleId);
    let playerPointRecord: ActivityGrowthPointModelType = await ActivityGrowthPointModel.findData(serverId, activityData.activityId, roleId);

    let userHeroes = await HeroModel.findByRole(roleId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new GrowthData(activityData, createTime, serverTime);
    await playerData.setPlayerRecords(playerRecords, roleId, userHeroes);
    playerData.setPlayerPointRecord(playerPointRecord);
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerGrowthData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    let playerRecords: ActivityGrowthModelType[] = await ActivityGrowthModel.findData(serverId, activityId, roleId);
    let playerPointRecord: ActivityGrowthPointModelType = await ActivityGrowthPointModel.findData(serverId, activityId, roleId);

    let userHeroes = await HeroModel.findByRole(roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new GrowthData(activityData, createTime, serverTime);
    await playerData.setPlayerRecords(playerRecords, roleId, userHeroes);
    playerData.setPlayerPointRecord(playerPointRecord);
    return playerData;
}


