import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS, TREASURE_HUNT_DATA_TYPE } from '../../consts';
import { ActivityTreasureHuntShopModel } from '../../db/ActivityTreasureHuntShop';
import { ActivityTreasureHuntTaskModel } from '../../db/ActivityTreasureHuntTask';
import { ActivityTreasureHuntTreasureShopModel } from '../../db/ActivityTreasureHuntTreasureShop';
import { ActivityTreasureHuntFirstPageModel } from '../../db/ActivityTreasureHuntFirstPage';
import { TreasureHuntData } from '../../domain/activityField/treasureHuntField';
import { addReward, stringToConsumeParam, stringToRewardParam } from './giftPackageService';
import { RewardParam } from '../../domain/activityField/rewardField';
import { getActivitiesByType, getActivityById, getActivityByServerId } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { handleCost } from '../role/rewardService';
import { ActivityTreasureHuntChallengeModel } from '../../db/ActivityTreasureHuntChallenge';

export async function getPlayerTreasureHuntDataShow(activityId: number, serverId: number, roleId: string) {
    let playerData = await getPlayerTreasureHuntData(activityId, serverId, roleId);
    if(playerData && playerData.canShow && playerData.canShow()) {
        return playerData.getShowResult();
    }
    return null
}
/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerTreasureHuntData(activityId: number, serverId: number, roleId: string, dataType: TREASURE_HUNT_DATA_TYPE = TREASURE_HUNT_DATA_TYPE.ALL) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new TreasureHuntData(activityData, createTime, serverTime);
    if(dataType == TREASURE_HUNT_DATA_TYPE.FIRST_PAGE || dataType == TREASURE_HUNT_DATA_TYPE.ALL) {
        let playerTreasureFirstPageRecord = await ActivityTreasureHuntFirstPageModel.findData(serverId, activityId, roleId, playerData.roundIndex);
        playerData.firstPage.setPlayerFirstPageRecord(playerTreasureFirstPageRecord);
    }
    if (dataType == TREASURE_HUNT_DATA_TYPE.SHOP || dataType == TREASURE_HUNT_DATA_TYPE.ALL) {
        let playerShopRecord = await ActivityTreasureHuntShopModel.findTreasureData(activityId, roleId, playerData.roundIndex, playerData.todayIndex);
        playerData.shop.setPlayerShopRecords(playerShopRecord);
    }
    if (dataType == TREASURE_HUNT_DATA_TYPE.TASK || dataType == TREASURE_HUNT_DATA_TYPE.ALL) {
        let playerTaskRecord = await ActivityTreasureHuntTaskModel.findDataByRoundIndex(serverId, activityId, roleId, playerData.roundIndex);
        playerData.tasks.setPlayerTaskRecords(playerTaskRecord);
    }
    if(dataType == TREASURE_HUNT_DATA_TYPE.TREASURE_SHOP || dataType == TREASURE_HUNT_DATA_TYPE.ALL) {
        let playerTreasureShopRecord = await ActivityTreasureHuntTreasureShopModel.findData(activityId, roleId, playerData.roundIndex);
        playerData.treasureShop.setPlayerTreasureShopRecords(playerTreasureShopRecord);
    }
    if(dataType == TREASURE_HUNT_DATA_TYPE.CHALLENGE || dataType == TREASURE_HUNT_DATA_TYPE.ALL) {
        let playerRecord = await ActivityTreasureHuntChallengeModel.findData(serverId, activityId, roleId, playerData.roundIndex);
        playerData.challenge.setChallengeRecord(playerRecord)
    }

    return playerData;
}

export async function getPlayerTreasureHuntFirstPageData(activityId: number, serverId: number, roleId: string) {
    return await getPlayerTreasureHuntData(activityId, serverId, roleId, TREASURE_HUNT_DATA_TYPE.FIRST_PAGE);
}

export async function getPlayerTreasureHuntShopData(activityId: number, serverId: number, roleId: string) {
    return await getPlayerTreasureHuntData(activityId, serverId, roleId, TREASURE_HUNT_DATA_TYPE.SHOP);
}

export async function getPlayerTreasureHuntTaskData(activityId: number, serverId: number, roleId: string) {
    return await getPlayerTreasureHuntData(activityId, serverId, roleId, TREASURE_HUNT_DATA_TYPE.TASK);
}

export async function getPlayerTreasureHuntChallengeData(activityId: number, serverId: number, roleId: string) {
    return await getPlayerTreasureHuntData(activityId, serverId, roleId, TREASURE_HUNT_DATA_TYPE.CHALLENGE);
}

export async function getPlayerTreasureHuntTreasureShopData(activityId: number, serverId: number, roleId: string) {
    return await getPlayerTreasureHuntData(activityId, serverId, roleId, TREASURE_HUNT_DATA_TYPE.TREASURE_SHOP);
}

/**
   * 购买每日物资
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeShop(roleId: string, roleName: string, sid: string, serverId: number,
    activityId: number, productID: string) {
    // let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let playerData = await getPlayerTreasureHuntShopData(activityId, serverId, roleId,);
    if (!playerData) {
        return {
            code: STATUS.ACTIVITY_MISSING,
        }
    }

    let item = playerData.shop.findProductID(productID)

    if (!item) {
        return {
            code: STATUS.ACTIVITY_MISSING,
        }
    }
    if (item.buyCount >= item.countMax) {
        return {
            code: STATUS.ACTIVITY_MAX_COUNT,
        }
    }
    let cellIndex = item.cellIndex;

    //添加购买记录
    await ActivityTreasureHuntShopModel.buyShopRecord(activityId, roleId, playerData.roundIndex, playerData.todayIndex, cellIndex, 1);

    let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
    let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.TREASURE_HUNT_SHOP_BUY)

    item.buyCount += 1;
    return {
        code: 0,
        data: Object.assign(result, { item })
    }
}

export async function treasureHuntChallengeConsume(serverId: number, roleId: string, sid: string) {
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.TREASURE_HUNT);
    for(let { activityId } of activities) {
        let playerData = await getPlayerTreasureHuntChallengeData(activityId, serverId, roleId,);
        if (!playerData) return false;
    
        if(playerData.canShow()) {
            let challengeData = playerData.challenge;
            //消耗资源
            let consumeStr = challengeData.consume;
            let consume = stringToConsumeParam(consumeStr)
            let resourceResult = await handleCost(roleId, sid, consume, ITEM_CHANGE_REASON.TREASURE_HUNT_CHALLENGE);
            if (!resourceResult) return false;
        
            let warId = playerData.challenge.randomGK();
            await ActivityTreasureHuntChallengeModel.setWarId(serverId, activityId, roleId, playerData.roundIndex, warId);    
        }
    }
    return true
}