import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityNewHeroGKModel, ActivityNewHeroGKModelType } from '../../db/ActivityNewHeroGK';
import { NewHeroGKData } from '../../domain/activityField/newHeroGKField';
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 newHeroGKActivity(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: ActivityNewHeroGKModelType = await ActivityNewHeroGKModel.findData(serverId, activityData.activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new NewHeroGKData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}


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

    return playerData;
}

/**
   * 挑战每日关卡成功
   *
   * @param {number} activityId 活动id
   * @param {number} gk 关卡id
   *
   */
// export async function challengeNewHeroGK(serverId: number, roleId: string, activityId: number, pageIndex: number, gk: number) {
//     let isFirst = false;
//     let activityData: ActivityModelType = await getActivityById(activityId);
//     let playerRecords = await ActivityNewHeroGKModel.findData(serverId, activityId, roleId);
//     let records = playerRecords && playerRecords.records ? playerRecords.records : [];

//     let createTime = await getRoleCreateTime(roleId);
//     let serverTime = await getServerCreateTime(serverId);
//     let playerData = new NewHeroGKData(activityData, createTime, serverTime);
//     let item = playerData.findItemByGK(pageIndex, gk);
//     if (item) {
//         let index = item.index;
//         let recordIndex = records.findIndex(obj => { return obj.pageIndex == pageIndex && obj.index == index });
//         if (recordIndex == -1) {
//             isFirst = true;
//             await ActivityNewHeroGKModel.addRecord(serverId, activityId, roleId, pageIndex, index);
//         }
//     } else {
//         console.log('challengeNewHeroGK没有找到对应关卡活动数据', activityId, gk)
//     }
//     return isFirst;
// }




