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 { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';


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

    let playerLunchRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.LUNCH, playerData.roundIndex);
    let playerDinnerRecords: ActivityDailyMealModelType[] = await ActivityDailyMealModel.findData(serverId, activityData.activityId, roleId, DAILY_MEAL_TYPE.DINNER, playerData.roundIndex);
    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 getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyMealData(activityData, createTime, serverTime);

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


