import moment = require('moment');
import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityShopModel, ActivityShopModelType } from '../../db/ActivityShop';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { LimitShopData } from '../../domain/activityField/limitShopField';
import { deltaDays } from '../../pubUtils/util';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';
import { addReward, stringToRewardParam } from './giftPackageService';

/**
   * 获取新手玩家限购礼包活动数据  新手限定作为开服七天乐的延伸礼包。只存在于开服前七天，七天时间过去，玩家无法购买
   *
   * @param {number} serverId 区Id
   * @param {number} type 活动类型 ACTIVITY_TYPE
   * @param {string} roleId 角色Id
   *
   */

export async function newPlayerLimitPackageActivity(serverId: number, roleId: string) {
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE);
    if (activities.length == 0) {
        return null;
    }
    let activityData = activities[0];
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new LimitShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityShopModelType = await ActivityShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 每日、每周限购礼包活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerLimitPackageData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new LimitShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityShopModelType = await ActivityShopModel.findData(activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}


/**
   * 获取每日/每周限购礼包数据
   *
   * @param {number} serverId 区Id
   * @param {number} type 活动类型 ACTIVITY_TYPE
   * @param {string} roleId 角色Id
   *
   */

export async function limitPackageActivity(serverId: number, roleId: string, type: number) {
    let activityArray = await getActivitiesByType(serverId, type);
    activityArray = activityArray.sort((a, b) => {
        return b.activityId - a.activityId
    });
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerData = await getPlayerLimitPackageData(activityData.activityId, serverId, roleId);
    return playerData
}

/**
   * 玩家活动第几天
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function newPlayerActivityDays(roleId: string) {
    let { createTime } = await RoleModel.findByRoleId(roleId);
    let createDate = moment(createTime * 1000).toDate();
    let todayIndex = deltaDays(createDate, new Date, true) + 1;
    return todayIndex;
}




/**
   * 结算购买的奖励
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeLimitPackageReward(roleId: string, roleName: string, sid: string, serverId: number, 
    activityId: number, productID: string) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (!activityData) {
        return STATUS.ACTIVITY_MISSING;
    }
    if (activityData.type !== ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE &&
        activityData.type !== ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_DAILY &&
        activityData.type !== ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_WEEKLY) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new LimitShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityShopModelType = await ActivityShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecord);

    let item = playerData.findItemByProductID(productID);
    if (!item) {
        return STATUS.ACTIVITY_NO_PRODUCT;
    }
    // if (item.countMax > 0 && item.buyCount >= item.countMax) {
    //     return STATUS.ACTIVITY_MAX_COUNT;
    // }

    let rewardArray = stringToRewardParam(item.reward)
    let result = await addReward(roleId, roleName, sid, serverId, rewardArray, ITEM_CHANGE_REASON.BUY_LIMIT_PACKAGE);

    await ActivityShopModel.addRecord(activityId, roleId, playerData.roundIndex, item.id, 1);

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

export async function checkLimitPackageCanBuy(roleId: string, serverId: number, activityData: ActivityModelType, productID: string) {
    if(!activityData) return false
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new LimitShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityShopModelType = await ActivityShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecord);

    let item = playerData.findItemByProductID(productID);
    if (!item) return false
    if (item.countMax > 0 && item.buyCount >= item.countMax) return false;
    return true;
}