import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyRMBGiftsModel, ActivityDailyRMBGiftsModelType } from '../../db/ActivityDailyRMBGifts';
import { DailyRMBGiftsData } from '../../domain/activityField/dailyRMBGiftsField';
import moment = require('moment');
import { RewardParam } from '../../domain/activityField/rewardField';
import { addReward, stringToRewardParam } from './giftPackageService';
import { getActivitiesByType, getActivityById } from './activityService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';

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

export async function dailyRMBGiftsActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.DAILY_RMB_GIFTS);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerData = await getPlayerDailyRMBGiftsData(activityData.activityId, serverId, roleId);
    return playerData
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyRMBGiftsData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyRMBGiftsData(activityData, createTime, serverTime);
    let playerRecords: ActivityDailyRMBGiftsModelType[] = await ActivityDailyRMBGiftsModel.findBuyRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), playerData.todayIndex);

    playerData.setPlayerRecords(playerRecords);
    return playerData;
}

/**
   * 结算购买礼包的奖励
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeDailyRMBGiftsReward(roleId: string, roleName: string, sid: string, serverId: number, 
    activityId: number, productID: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (activityData.type !== ACTIVITY_TYPE.DAILY_RMB_GIFTS) {
        return {
            code: STATUS.ACTIVITY_TYPE_ERROR,
        }
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyRMBGiftsData(activityData, createTime, serverTime);
    let bigGiftProductID = playerData.productID;
    if (bigGiftProductID == productID) {//一次性购买7天礼包
        let playerRecord: ActivityDailyRMBGiftsModelType = await ActivityDailyRMBGiftsModel.findRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), playerData.todayIndex);
        playerData.setPlayerRecords(playerRecord?[playerRecord]: []);
        if (playerData.shouldbuyNextDay()) {//今天已经购买过其他奖励
            playerData.todayIndex ++;
        }

        for (let i = 0; i < playerData.day; i++) {
            let todayIndex = playerData.todayIndex + i;
            await ActivityDailyRMBGiftsModel.buyRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), todayIndex, true);
        }
        let endTime = moment(playerData.endTime).add(playerData.day, 'd').toDate()
        return {
            code: 0,
            data: Object.assign({}, { item: { endTime, receiveCount: 0 } })
        }
    } else {
        let item = playerData.findProduct(productID);
        if (!item) {
            return {
                code: STATUS.ACTIVITY_NO_PRODUCT,
            }
        }
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.DAILY_RMB_GIFT)
        await ActivityDailyRMBGiftsModel.receiveRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), playerData.todayIndex, [item.id]);
        item.isReceive = true;
        return {
            code: 0,
            data: Object.assign(result, { item })
        }
    }
}
