/**
 * 
 */
package com.xiugou.x1.game.server.module.player.service;

import java.util.List;

import org.gaming.db.repository.BaseRepository;
import org.gaming.db.usecase.SlimDao;
import org.gaming.prefab.IGameCause;
import org.gaming.prefab.ITipCause;
import org.gaming.prefab.thing.CostReceipt;
import org.gaming.prefab.thing.ICostThing;
import org.gaming.prefab.thing.IRewardThing;
import org.gaming.prefab.thing.IThingType;
import org.gaming.prefab.thing.NumberThingStorer;
import org.gaming.prefab.thing.RewardReceipt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiugou.x1.design.constant.ItemType;
import com.xiugou.x1.design.constant.TipsCode;
import com.xiugou.x1.game.server.module.player.model.Player;

import pojo.xiugou.x1.pojo.log.player.DiamondLog;

/**
 * @author YY
 *
 */
@Component
public class DiamondStorer extends NumberThingStorer<DiamondLog> {

	@Autowired
	private PlayerService playerService;
	
	@Override
	protected DiamondLog newLog() {
		return new DiamondLog();
	}

	@Override
	protected BaseRepository<DiamondLog> initRepository() {
		return SlimDao.getRepository(DiamondLog.class);
	}

	@Override
	protected IThingType thingType() {
		return ItemType.DIAMOND;
	}

	@Override
	protected ITipCause lackCode() {
		return TipsCode.DIAMOND_LACK;
	}

	@Override
	public long getCount(long entityId, int thingId) {
		return playerService.getEntity(entityId).getDiamond();
	}

	@Override
	protected RewardReceipt doAdd(long entityId, List<? extends IRewardThing> rewardThings, IGameCause cause, String remark) {
		Player player = playerService.getEntity(entityId);
		for(IRewardThing rewardThing : rewardThings) {
			player.setDiamond(player.getDiamond() + rewardThing.getNum());
		}
		playerService.update(player);
		return new RewardReceipt(rewardThings);
	}

	@Override
	protected CostReceipt doCost(long entityId, List<? extends ICostThing> costThings, IGameCause cause, String remark) {
		Player player = playerService.getEntity(entityId);
		for(ICostThing costThing : costThings) {
			player.setDiamond(player.getDiamond() - costThing.getNum());
		}
		playerService.update(player);
		return new CostReceipt(costThings);
	}

	@Override
	protected String getOwnerName(long entityId) {
		return playerService.getEntity(entityId).getNick();
	}
}
