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 { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { GrowthData } from '../../domain/activityField/growthField';
import { getActivityById } from './activityService';

/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function growthActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityDataArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.TASK_GROWTH, new Date());
    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 RoleModel.findByRoleId(roleId);
    let playerData = new GrowthData(activityData, createTime);
    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 RoleModel.findByRoleId(roleId);
    let playerData = new GrowthData(activityData, createTime);
    await playerData.setPlayerRecords(playerRecords, roleId, userHeroes);
    playerData.setPlayerPointRecord(playerPointRecord);
    return playerData;
}


