package com.beecavegames.bjc.handlers;

import lombok.extern.slf4j.Slf4j;

import com.beecavegames.GameSession;
import com.beecavegames.bjc.entities.Player;
import com.beecavegames.bjc.util.PlayerPaymentUtil;
import com.beecavegames.common.PurchaseFreeResponse;
import com.beecavegames.common.data.PackageSpec;
import com.beecavegames.common.data.StaticData;
import com.beecavegames.dispatch.Param;
import com.google.inject.Inject;

@Slf4j
public class GetNPurchaseFreeHandler extends BaseHandler {

	@Inject
	protected PlayerPaymentUtil playerPaymentUtil;

	@Inject
	public StaticData staticData;

	public PurchaseFreeResponse handle(@Param("productId") final long productId,
			@Param("priceTier") final int priceTier) {

		final GameSession session = GameSession.getGameSession();
		final PackageSpec packageSpec = staticData.get(PackageSpec.class, productId);
		PurchaseFreeResponse response = new PurchaseFreeResponse();
		Player player = playerUtil.getPlayer(session.beeId);
		long playerId = session.beeId;

		if (packageSpec != null && playerPaymentUtil.isFreeBuy(player, packageSpec.getCurrency(), priceTier)) {
			log.warn("GetNPurchaseFreeHandler playerId:{} currency:{} priceTier:{}", playerId, packageSpec.getCurrency(), priceTier);

			return playerUtil.creditFreePurchasePackage(playerId, productId, priceTier);
		}

		return response;
	}
}
