import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyGiftsModel, ActivityDailyGiftsModelType } from '../../db/ActivityDailyGifts';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { DailyGiftsData } from '../../domain/activityField/dailyGiftsField';
import { getActivityById } from './activityService';



/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function dailyGiftsActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityDataArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.DAILY_DISCOUNT_GIFT, new Date());
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerRecords: ActivityDailyGiftsModelType[] = await ActivityDailyGiftsModel.findData(serverId, activityData.activityId, roleId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new DailyGiftsData(activityData, createTime);
    playerData.setPlayerRecords(playerRecords);
    return playerData;
}


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyGiftsData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    let playerRecords: ActivityDailyGiftsModelType[] = await ActivityDailyGiftsModel.findData(serverId, activityId, roleId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new DailyGiftsData(activityData, createTime);
    playerData.setPlayerRecords(playerRecords);

    return playerData;
}


