import { ACTIVITY_TYPE, TASK_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityFirstGiftModel, ActivityFirstGiftModelType } from '../../db/ActivityFirstGift';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { FirstGiftData } from '../../domain/activityField/firstGiftField';

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

export async function firstGiftActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityArray: ActivityModelType[] = await ActivityModel.findActivityByType(activityGroupId, ACTIVITY_TYPE.FIRST_GIFT, 1)

    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerRecord: ActivityFirstGiftModelType = await ActivityFirstGiftModel.findData(activityData.activityId, roleId);
    if (!playerRecord || playerRecord.isOver) {
        return null;
    }

    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new FirstGiftData(activityData, createTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerFirstGiftData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let playerRecord: ActivityFirstGiftModelType = await ActivityFirstGiftModel.findData(activityId, roleId);

    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new FirstGiftData(activityData, createTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}
