import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityGrowthModel, ActivityGrowthModelType } from '../../db/ActivityGrowth';
import { ActivityGrowthPointModel, ActivityGrowthPointModelType } from '../../db/ActivityGrowthPoint';
import { SevenDaysData } from '../../domain/activityField/sevenDaysField';
import { ActivityDailyChallengesModel, ActivityDailyChallengesModelType } from '../../db/ActivityDailyChallenges';
import { ActivityDailyGiftsModel, ActivityDailyGiftsModelType } from '../../db/ActivityDailyGifts';
import { getActivitiesByType, getActivityById } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';

/**
   * 获取活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function sevenDaysActivity(serverId: number, roleId: string, type: number) {
    let activityDataArray = await getActivitiesByType(serverId, type);
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SevenDaysData(activityData, createTime, serverTime);
    //成长数据
    let playerGrowthRecords: ActivityGrowthModelType[] = await ActivityGrowthModel.findData(serverId, activityData.activityId, roleId);
    let playerPointRecord: ActivityGrowthPointModelType = await ActivityGrowthPointModel.findData(serverId, activityData.activityId, roleId);
    playerData.growth.setPlayerRecords(playerGrowthRecords);
    playerData.growth.setPlayerPointRecord(playerPointRecord);
    //今日挑战
    let playerDailyChallengeRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityData.activityId, roleId);
    playerData.dailyChallenge.setPlayerRecords(playerDailyChallengeRecords);
    //每日特惠礼包
    let playerRecords: ActivityDailyGiftsModelType[] = await ActivityDailyGiftsModel.findData(serverId, activityData.activityId, roleId);
    playerData.dailyGift.setPlayerRecords(playerRecords);

    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerSevenDaysData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SevenDaysData(activityData, createTime, serverTime);
    //成长数据
    let playerGrowthRecords: ActivityGrowthModelType[] = await ActivityGrowthModel.findData(serverId, activityId, roleId);
    let playerPointRecord: ActivityGrowthPointModelType = await ActivityGrowthPointModel.findData(serverId, activityId, roleId);
    playerData.growth.setPlayerRecords(playerGrowthRecords);
    playerData.growth.setPlayerPointRecord(playerPointRecord);
    //今日挑战
    let playerDailyChallengeRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityId, roleId);
    playerData.dailyChallenge.setPlayerRecords(playerDailyChallengeRecords);
    //每日特惠礼包
    let playerRecords: ActivityDailyGiftsModelType[] = await ActivityDailyGiftsModel.findData(serverId, activityId, roleId);
    playerData.dailyGift.setPlayerRecords(playerRecords);
    return playerData;
}


/**
   * 玩家成长数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerSevenDaysGrowthData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SevenDaysData(activityData, createTime, serverTime);
    //成长数据
    let playerGrowthRecords: ActivityGrowthModelType[] = await ActivityGrowthModel.findData(serverId, activityId, roleId);
    let playerPointRecord: ActivityGrowthPointModelType = await ActivityGrowthPointModel.findData(serverId, activityId, roleId);
    playerData.growth.setPlayerRecords(playerGrowthRecords);
    playerData.growth.setPlayerPointRecord(playerPointRecord);
    return playerData;
}


/**
   * 玩家今日挑战数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerSevenDaysDailyChallengesData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SevenDaysData(activityData, createTime, serverTime);
    //今日挑战
    let playerDailyChallengeRecords: ActivityDailyChallengesModelType[] = await ActivityDailyChallengesModel.findData(serverId, activityId, roleId);
    playerData.dailyChallenge.setPlayerRecords(playerDailyChallengeRecords);
    return playerData;
}

/**
   * 玩家每日特惠礼包数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerSevenDaysDailyGiftsData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new SevenDaysData(activityData, createTime, serverTime);
    //每日特惠礼包
    let playerRecords: ActivityDailyGiftsModelType[] = await ActivityDailyGiftsModel.findData(serverId, activityId, roleId);
    playerData.dailyGift.setPlayerRecords(playerRecords);
    return playerData;
}