package extend.zp.tip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.game.Global;
import com.game.player.state.PlayerWaitState;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;

import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.RuleWeight;

public class TipManager {

	private int					id		= 0;

	public Map<Integer, Tip>	tipMap	= null;
	public Tip					actionTip;
	public EXPlayer				owner	= null;
	public EXRoom 				room;
	public boolean				isWin;
	
	public TipManager(EXPlayer owner) {
		this.tipMap = new HashMap<Integer, Tip>();
		this.owner = owner;
		room = this.owner.getRoom();
	}

	public void addTip(Tip tip) {
		int id = this.getId();
		tip.id = id;
		this.tipMap.put(id, tip);
	}
	
	
	@SuppressWarnings("unchecked")
	public void choicAction(int id) {
		if (id == 0) {
			for (Entry<Integer, Tip> entry : this.tipMap.entrySet()) {
				Tip tip = entry.getValue();
				if(tip.type == RuleWeight.TYPE_PONG || tip.type == RuleWeight.TYPE_CHOW) {
					owner.chouList.add(tip.card);
				}
			}
			if(room.activeSeat == owner.seat) {
				//this.owner.stateMachine.toLastState();
				this.owner.stateMachine.curState.exit(owner);
				this.owner.stateMachine.curState = this.owner.stateMachine.lastState;
			}else {
				this.owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			}
			room.checkAction();
		} else if (this.tipMap.containsKey(id)) {
			Tip tip = this.tipMap.get(id);
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			room.addAction(new Action(tip, owner));
		} else {
			throw new Error("tip map has no id" + id);
		}
	}

	
	@SuppressWarnings("unchecked")
	public void doAction(int id) {
		if (id == 0) {
			Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<Integer, Tip> entry = it.next();
				Tip tip = entry.getValue();
				if (tip.type == RuleWeight.TYPE_CHOW || tip.type == RuleWeight.TYPE_PONG) {
					owner.chouList.add(tip.card);
				}
			}
			this.owner.stateMachine.curState.exit(owner);
			this.owner.stateMachine.curState = this.owner.stateMachine.lastState;
			room.checkAction();
		} else if (this.tipMap.containsKey(id)) {
			Tip tip = this.tipMap.get(id);
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.getRoom().addAction(new Action(tip, owner));
		} else {
			throw new Error("tip map has no id" + id);
		}
	}

	public ITObject toMP() {
		ITObject mp = new TObject();
		ITArray tipList = new TArray();
		for (Entry<Integer, Tip> entry : this.tipMap.entrySet()) {
			Tip tip = entry.getValue();
			ITObject tipMp = tip.toMP();
			tipList.addTObject(tipMp);
		}
		mp.putTArray("tip_list", tipList);
		return mp;

	}

	private int getId() {
		this.id += 1;
		return this.id;
	}

	public int getWeightest() {
		int result = 0;
		for (Entry<Integer, Tip> entry : this.tipMap.entrySet()) {
			Tip tip = entry.getValue();
			if (tip.weight > result) {
				result = tip.weight;
			}
		}
		return result;
	}
	

	public void cleanWeightest(int weight) {
		List<Tip> list = new ArrayList<Tip>(this.tipMap.values());
		for (Tip tip : list) {
			if (tip.weight < weight) {
				this.tipMap.remove(tip.id);
			}
		}
	}

	public void clean() {
		this.id = 0;
		this.isWin = false;
		this.tipMap = new HashMap<Integer, Tip>();
	}

}
