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 { ServerlistModel } from '../../db/Serverlist';
import { RoleModel } from '../../db/Role';
import { getActivityById } from './activityService';

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

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

    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 RoleModel.findByRoleId(roleId);
    let playerData = new DailyRMBGiftsData(activityData, createTime);
    // let productID = playerData.productID;
    // let playerOrders: UserOrderModelType[] = await UserOrderModel.findPlayerOrder(productID, roleId, activityId, 1);
    // let buyTime = moment(new Date()).startOf('d').toDate()
    // let endTime = null;
    // if (playerOrders.length > 0) {
    //     buyTime = playerOrders[0].createdAt;//购买时候的日期
    //     endTime = moment(buyTime).add(7, 'd').endOf('d').toDate()
    // }
    let playerRecord: ActivityDailyRMBGiftsModelType = await ActivityDailyRMBGiftsModel.findRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate());
    let unReceiveRecord: ActivityDailyRMBGiftsModelType[] = await ActivityDailyRMBGiftsModel.findBuyRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate());

    playerData.setPlayerRecords(playerRecord, unReceiveRecord.length);
    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 RoleModel.findByRoleId(roleId);
    let playerData = new DailyRMBGiftsData(activityData, createTime);
    let bigGiftProductID = playerData.productID;
    if (bigGiftProductID == productID) {//一次性购买7天礼包
        let playerRecord: ActivityDailyRMBGiftsModelType = await ActivityDailyRMBGiftsModel.findRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate());
        if (playerRecord && playerRecord.records && playerRecord.records.length > 0) {//今天已经购买过其他奖励
            playerData.beginTime += 24 * 60 * 60 * 1000;
        }

        for (let i = 0; i < playerData.day; i++) {
            let beginTime = playerData.beginTime + 24 * 60 * 60 * 1000 * i;
            await ActivityDailyRMBGiftsModel.buyRecord(serverId, activityId, roleId, moment(beginTime).toDate(), 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(), [item.id]);
        item.isReceive = true;
        return {
            code: 0,
            data: Object.assign(result, { item })
        }
    }
}
