import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyGKModel, ActivityDailyGKModelType } from '../../db/ActivityDailyGK';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { DailyGKData } from '../../domain/activityField/dailyGKField';
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 dailyGKActivity(serverId: number, roleId: string) {
    let activityDataArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.DAILY_GK);
    if (activityDataArray.length === 0) {
        return null;
    }
    let activityData: ActivityModelType = activityDataArray[0];
    let playerRecord: ActivityDailyGKModelType = await ActivityDailyGKModel.findData(serverId, activityData.activityId, roleId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyGKData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyGKData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let playerRecord: ActivityDailyGKModelType = await ActivityDailyGKModel.findData(serverId, activityId, roleId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyGKData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);

    return playerData;
}

/**
   * 挑战每日关卡成功
   *
   * @param {number} activityId 活动id
   * @param {number} gk 关卡id
   *
   */
export async function challengeDailyGK(serverId: number, roleId: string, activityId: number, gk: number) {
    let activityData: ActivityModelType = await getActivityById(activityId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyGKData(activityData, createTime, serverTime);
    let item = playerData.findItemByGK(gk);
    if (item) {
        let dayIndex = item.dayIndex;
        await ActivityDailyGKModel.addRecord(serverId, activityId, roleId, dayIndex);
    } else {
        console.log('没有找到对应关卡活动数据', activityId, gk)
    }

    return playerData;
}




