import { ACTIVITY_TYPE, STATUS, TASK_TYPE } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { PopUpShopData, } from '../../domain/activityField/popUpShopField';
import { RewardParam } from '../../domain/activityField/rewardField';
import { addReward, stringToRewardParam } from './giftPackageService';
import { ActivityPopUpShopModel, ActivityPopUpShopModelType } from '../../db/ActivityPopUpShop';
import { ServerlistModel } from '../../db/Serverlist';
import moment = require('moment');

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

export async function popUpShopActivity(serverId: number, roleId: string) {
    let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
    let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.POP_UP_SHOP, new Date())

    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerData = await getPlayerPopUpShopData(activityData.activityId, serverId, roleId);
    return playerData
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerPopUpShopData(activityId: number, serverId: number, roleId: string) {
    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    let playerRecords: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.findAllOpenData(serverId, activityId, roleId);

    let allPlayerShop = [];
    let allTaskData = JSON.parse(activityData.data);
    for (let record of playerRecords) {
        if (record.isPush) {//没有购买
            let index = allTaskData.findIndex(obj => { return obj && obj.id === record.id && obj.taskType === record.type })
            if (index != -1) {
                let playerData = new PopUpShopData(allTaskData[index], activityData.activityId);
                playerData.setPlayerRecords(record)
                if (playerData.buyCount < allTaskData[index].count) {
                    allPlayerShop.push(playerData)
                }
            }
        }
    }
    return allPlayerShop;
}

/**
   * 结算购买礼包的奖励
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makePopUpShopReward(roleId: string, roleName: string, sid: string, serverId: number, funcs: number[],
    activityId: number, productID: string, paramStr: string) {
    if (!paramStr) {
        return STATUS.ORDER_PARAM_ERROR;
    }
    let paramObj = JSON.parse(paramStr);
    let beginTime = moment(paramObj.beginTimeStamp).toDate();

    let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
    if (!activityData) {
        return STATUS.ACTIVITY_MISSING;
    }
    if (activityData.type !== ACTIVITY_TYPE.POP_UP_SHOP) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let allTaskData: any[] = JSON.parse(activityData.data);
    let taskIndex = allTaskData.findIndex(obj => { return obj && obj.productID == productID });
    if (taskIndex == -1) {
        return STATUS.ACTIVITY_NO_PRODUCT;
    }
    let playerData = new PopUpShopData(allTaskData[taskIndex], activityId);
    let playerRecord: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findDataByBeginTime(serverId, activityId, roleId, playerData.id, playerData.taskType, beginTime);
    if (!playerRecord) {
        return STATUS.APPLY_ORDER_ERROR;
    }
    let curDate = new Date();
    if (curDate < playerRecord.beginTime || curDate > playerRecord.endTime) {
        return STATUS.ACTIVITY_POP_UP_SHOP_CLOSED;
    }
    if (playerRecord.totalCount < playerData.condition) {
        return STATUS.ACTIVITY_TASK_UNCOMPLETED;
    }
    if (playerRecord.buyCount >= playerData.count) {
        return STATUS.ACTIVITY_REWARDED;
    }
    playerRecord = await ActivityPopUpShopModel.addRecord(serverId, activityId, roleId, playerData.id, playerData.taskType, 1, beginTime);
    playerData.setPlayerRecords(playerRecord);

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


    return {
        code: 0,
        data: Object.assign(result, { item: playerData, activityId: activityData.activityId })
    }
}