import { ACTIVITY_TYPE, DAILY_MEAL_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyMealModel, ActivityDailyMealModelType } from '../../db/ActivityDailyMeal';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { DailyMealData } from '../../domain/activityField/dailyMealField';
import { getActivityById } from './activityService';


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

    let playerLunchRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.LUNCH, playerData.beginTime, playerData.endTime);
    let playerDinnerRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.DINNER, playerData.beginTime, playerData.endTime);
    playerData.setPlayerRecords(playerLunchRecords.concat(playerDinnerRecords));
    return playerData;
}


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyMealData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let playerData = new DailyMealData(activityData, createTime);

    let playerLunchRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.LUNCH, playerData.beginTime, playerData.endTime);
    let playerDinnerRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.DINNER, playerData.beginTime, playerData.endTime);
    playerData.setPlayerRecords(playerLunchRecords.concat(playerDinnerRecords));
    return playerData;
}


