import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { RoleModel } from '../../db/Role';
import { ServerlistModel } from '../../db/Serverlist';
import { UserOrderModel, UserOrderModelType } from '../../db/UserOrder';
import { RewardParam } from '../../domain/activityField/rewardField';
import { YuanBaoShopData } from '../../domain/activityField/yuanBaoShopField';
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 yuanbaoActivity(serverId: number, roleId: string) {
    let activityArray = await getActivitiesByType(serverId, ACTIVITY_TYPE.YUAN_BAO_SHOP);
    if (activityArray.length == 0) {
        return null;
    }
    let activityData = activityArray[0];
    let playerRecord: UserOrderModelType[] = await UserOrderModel.findOrderByActivityID(activityData.activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new YuanBaoShopData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerYuanbaoShopData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let playerRecord: UserOrderModelType[] = await UserOrderModel.findOrderByActivityID(activityId, roleId);

    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new YuanBaoShopData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}


/**
   * 结算奖励
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   * @param {string} productID 商品ID
   *
   */
export async function makeYuanbaoShopReward(roleId: string, roleName: string, sid: string, serverId: number,
    activityId: number, productID: string) {
    let records = await UserOrderModel.findSuccessOrderByProductID(productID, roleId, activityId);
    let activityData: ActivityModelType = await getActivityById(activityId);
    if (activityData.type !== ACTIVITY_TYPE.YUAN_BAO_SHOP) {
        return STATUS.ACTIVITY_TYPE_ERROR;
    }
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new YuanBaoShopData(activityData, createTime, serverTime);
    let item = playerData.findItem(productID);

    let rewardParamArr: Array<RewardParam> = [];
    if (records.length == 0) {//首充
        rewardParamArr = rewardParamArr.concat(stringToRewardParam(item.baseReward));
        rewardParamArr = rewardParamArr.concat(stringToRewardParam(item.firstReward));
    } else {
        rewardParamArr = rewardParamArr.concat(stringToRewardParam(item.baseReward));
        rewardParamArr = rewardParamArr.concat(stringToRewardParam(item.extraReward));
    }
    let arr: Array<RewardParam> = [];
    for (let obj of rewardParamArr) {
        let index = arr.findIndex(reward => { return reward.type == obj.type && reward.id == obj.id });
        if (index != -1) {
            arr[index].count += obj.count;
        } else {
            arr.push(obj);
        }
    }

    let result = await addReward(roleId, roleName, sid, serverId, arr, ITEM_CHANGE_REASON.YUANBAO_SHOP_BUY)
    item.buyCount += 1;
    return {
        code: 0,
        data: Object.assign(result, { item, activityId: activityData.activityId })
    }
}
