package extend.zp;

import java.util.Map.Entry;

import com.game.Util;
import com.game.data.BasePlayBack;
import com.game.data.Player;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;

public class PlayBack extends BasePlayBack {

	public PlayBack(EXRoom room) {
		super(room);
		info.putInt("left_card", room.card.getCount());
		ITArray infoList = new TArray();
		for (Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
			ITObject obj = TObject.newInstance();
			EXPlayer player = (EXPlayer) entry.getValue();
			obj.putInt("aid", player.playerid);
			obj.putUtfString("nick", player.nick);
			obj.putInt("sex", player.sex);
			obj.putUtfString("portrait", player.portrait);
			obj.putInt("seat", player.seat);
			ITArray cardInhand = Util.toTArray(player.cardInhand);
			obj.putTArray("hand_card", cardInhand);
			obj.putInt("score", player.score.total_score);
			infoList.addTObject(obj);
		}
		info.putTArray("playerData", infoList);
	}

	public void addGetCardCommand(int seat, int card, int left_count) {
		ITObject data = TObject.newInstance();
		data.putInt("card", card);
		data.putInt("left_count", left_count);
		addCommand("GetCard", seat, data);
	}

	public void addAddCardCommand(int seat, int card) {
		ITObject data = TObject.newInstance();
		data.putInt("card", card);
		addCommand("AddCard", seat, data);
	}

	public void addThrowCardCommand(int seat, int card) {
		ITObject data = TObject.newInstance();
		data.putInt("card", card);
		addCommand("ThrowCard", seat, data);
	}

	public void addOutCardCommand(int seat, int card) {
		ITObject data = TObject.newInstance();
		data.putInt("card", card);
		addCommand("OutCard", seat, data);
	}
	
	public  void addResultData(ITObject resultData){
		ITObject mp = TObject.newInstance();
		mp.putInt("type", 0);
		mp.putTObject("data", resultData);
		ITObject cmdObj = TObject.newInstance();
		cmdObj.putUtfString("cmd", "result");
		cmdObj.putTObject("result", mp);
		cmdList.addTObject(cmdObj);
	}

	public void addActionCommand(int seat, int type, int active_card, int card, int from_seat, int huxi, ITArray opcardArray) {
		ITObject cmdData = TObject.newInstance();
		cmdData.putInt("active_card", active_card);
		cmdData.putInt("card", card);
		cmdData.putInt("type", type);
		cmdData.putInt("from_seat", from_seat);
		cmdData.putInt("hu_xi", huxi);
		if (opcardArray != null) {
			ITArray opcard = TArray.newInstance();
			for (int i = 0; i < opcardArray.size(); i++) {
				opcard.addInt(opcardArray.getInt(i));
			}
			cmdData.putTArray("opcard", opcard);
		}
		addCommand("Action", seat, cmdData);
	}

	public void addWinCardCommand(int seat, boolean zimo) {
		ITObject cmdData = TObject.newInstance();
		addCommand("Win", seat, cmdData);
	}

	public void addCommand(String cmd, int seat, ITObject data) {
		ITObject cmdObj = TObject.newInstance();
		cmdObj.putUtfString("cmd", cmd);
		cmdObj.putInt("seat", seat);
		cmdObj.putTObject("data", data);
		cmdList.addTObject(cmdObj);
	}

	public ITObject getData(EXRoom owner) {
		ITObject data = TObject.newInstance();
		data.putTObject("info", info);
		data.putTArray("cmdList", cmdList);
		data.putTObject("result", EXMainServer.gameCtr.getRoomResultData(owner));
		return data;
	}
}
