import moment = require('moment');
import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityRefreshShopModel, ActivityRefreshShopModelType } from '../../db/ActivityRefreshShop';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { RefreshShopData } from '../../domain/activityField/refreshShopField';
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 getRefreshShopActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.REFRESH_SHOP);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new RefreshShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
    if (!playerRecord) {//生成商店可购买商品的数据
        let { lv } = await RoleModel.findByRoleId(roleId);
        let randomGoods = playerData.randomGoodsItems(lv);
        playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
    }
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 玩家商店数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerRefreshShopData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let role = await RoleModel.findByRoleId(roleId, 'lv createTime');
    let createTime = await getRoleCreateTime(roleId, role);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new RefreshShopData(activityData, createTime, serverTime);

    let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex);
    if (!playerRecord) {//生成商店可购买商品的数据
        let randomGoods = playerData.randomGoodsItems(role.lv);
        playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
    }
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 玩家商店数据(指定回合数，大富翁商店每回合刷新)
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerRefreshShopDataByRoundIndex(activityId: number, serverId: number, roleId: string, roundIndex: number) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    let role = await RoleModel.findByRoleId(roleId, 'lv createTime');
    let createTime = await getRoleCreateTime(roleId, role);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new RefreshShopData(activityData, createTime, serverTime);
    playerData.roundIndex = roundIndex;
    let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex);
    if (!playerRecord) {//生成商店可购买商品的数据
        let randomGoods = playerData.randomGoodsItems(role.lv);
        playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
    }
    playerData.setPlayerRecords(playerRecord);
    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
   * @param {number} roundIndex 大富翁重置回合数
   *
   */
export async function makeRefreshShopReward(roleId: string, roleName: string, sid: string, serverId: number,
    activityId: number, productID: string, roundIndex: number) {
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (!activityData) {
        return STATUS.ACTIVITY_MISSING;
    }
    if (activityData.type !== ACTIVITY_TYPE.REFRESH_SHOP) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new RefreshShopData(activityData, createTime, serverTime);
    if (roundIndex) {
        playerData.roundIndex = roundIndex;
    }

    let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.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_REFRESH_SHOP);

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

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