import { ActivityNewHeroGiftModel, ActivityNewHeroGiftModelType } from '../../db/ActivityNewHeroGift';
import { ActivityNewHeroGKModel, ActivityNewHeroGKModelType } from '../../db/ActivityNewHeroGK';
import { UserGachaModel, UserGachaType } from '../../db/UserGacha';
import { NewHeroGachaData } from '../../domain/activityField/newHeroGachaField';
import { NewHeroGiftData } from '../../domain/activityField/newHeroGiftField';
import { NewHeroGKData } from '../../domain/activityField/newHeroGKField';
import { gameData } from '../../pubUtils/data';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivityById } from './activityService';
import { refreshGacha } from './gachaService';



/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
 export async function getPlayerNewHeroGachaData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new NewHeroGachaData(activityData, createTime, serverTime);
    let userGacha: UserGachaType = await UserGachaModel.findByRole(roleId, playerData.gachaId, activityId);
    let dicGacha = gameData.gacha.get(playerData.gachaId);
    userGacha = await refreshGacha(dicGacha, userGacha);
    if(!userGacha.pickHero) {
      userGacha = await UserGachaModel.updateInfo(roleId, playerData.gachaId, activityId, { pickHero: playerData.getDefaultHero() })
    }
    playerData.setPlayerRecords(userGacha, dicGacha);

    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} warId 关卡id
   *
   */
export async function challengeNewHeroGK(serverId: number, roleId: string, activityId: number, warId: number) {
    let data = await ActivityNewHeroGKModel.addRecord(serverId, activityId, roleId, warId);
    let isFirst = data.records.filter(cur => cur.warId == warId).length <= 1;
    return isFirst;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
 export async function getPlayerNewHeroGiftsData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new NewHeroGiftData(activityData, createTime, serverTime);
    let playerRecord: ActivityNewHeroGiftModelType = await ActivityNewHeroGiftModel.findData(serverId, activityId, roleId);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 任务活动完成后会获得点数
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function newHeroGiftPoint(serverId: number, activityId: number, roleId: string, addPoint: number) {
   let activityData = await getActivityById(activityId);
   if(!activityData) return 0

   let createTime = await getRoleCreateTime(roleId);
   let serverTime = await getServerCreateTime(serverId);
   let playerData = new NewHeroGiftData(activityData, createTime, serverTime);

   if(playerData.canRecordTaskPoint()) {
      let result = await ActivityNewHeroGiftModel.addPoint(serverId, activityId, roleId, addPoint);
       return result.totalPoint;
   }
   return 0
}
