import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityFirstGiftModel, ActivityFirstGiftModelType } from '../../db/ActivityFirstGift';
import { RoleModel, RoleType } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { FirstGiftData } from '../../domain/activityField/firstGiftField';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';

// /**
//    * 获取活动数据
//    *
//    * @param {number} serverId 区Id
//    * @param {number} type 活动类型 ACTIVITY_TYPE
//    * @param {string} roleId 角色Id
//    *
//    */

// export async function firstGiftActivity(serverId: number, roleId: string) {
//     let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
//     let activityArray: ActivityModelType[] = await ActivityModel.findActivityByType(activityGroupId, ACTIVITY_TYPE.FIRST_GIFT, 1)

//     if (activityArray.length == 0) {
//         return null;
//     }
//     let activityData = activityArray[0];
//     let playerRecord: ActivityFirstGiftModelType = await ActivityFirstGiftModel.findData(activityData.activityId, roleId);
//     if (!playerRecord || playerRecord.isOver) {
//         return null;
//     }

//     let { createTime } = await RoleModel.findByRoleId(roleId);
//     let playerData = new FirstGiftData(activityData, createTime);
//     playerData.setPlayerRecords(playerRecord);
//     return playerData;
// }

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

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

/**
 * 获取可显示的数据
 * @param activityId 
 * @param serverId 
 * @param roleId 
 * @returns 
 */
export async function getPlayerFirstGiftDataShow(activityId: number, serverId: number, roleId: string) {
    let playerData = await getPlayerFirstGiftData(activityId, serverId, roleId);
    if(playerData && playerData.canShow && playerData.canShow()) {
        return playerData.getShowResult();
    }
    return null
}

/**
 * 记录玩家首充记录
 * @param {RoleType} role 玩家数据
 * @param {number} serverId 区id
 */
export async function recordFirstGift(role: RoleType, serverId: number) {
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.FIRST_GIFT);
    let serverTime = await getServerCreateTime(serverId);
    for (let obj of activities) {
        let playerData = new FirstGiftData(obj, role.createTime, serverTime);
        for(let { pageIndex, targetMoney } of playerData.list) {
            if(role.totalPay >= targetMoney) {
                await ActivityFirstGiftModel.begin(obj.activityId, role.roleId, pageIndex );
            }
        }
    }
}
