package extend.zp.tip;

import java.util.HashMap;
import java.util.Iterator;
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.RuleWeight;


public class TipManager {

	private int						id		= 0;
	private int						uid		= 100;

	public HashMap<Integer, Tip>	tipMap	= null;
	public Tip						actionTip;
	public int						weight	= 0;
	public EXPlayer					owner	= null;

	public TipManager(EXPlayer owner) {
		this.tipMap = new HashMap<Integer, Tip>();
		this.owner = owner;

	}

	public void addTip(Tip tip) {
		int id = this.getId();
		this.weight = this.weight | tip.weight;
		tip.id = id;
		this.tipMap.put(id, tip);
	}
	public int getUid(){
		return this.uid;
	}
	
	//
	public void choicAction(int id,ITArray biid) {
		if (id == this.uid) {
			///
			Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
			int card = 0;
			while (it.hasNext()) {
				Map.Entry<Integer, Tip> entry = it.next();
				Tip tip = entry.getValue();
				card = tip.card;
				if (tip.type == RuleWeight.TYPE_CHOW) {
					this.owner.chouChiSet.add(tip.card);
				}
				if (tip.type == RuleWeight.TYPE_PONG) {
					this.owner.chouPongSet.add(tip.card);
				}
			}
			this.owner.isMingTang = true;
			this.owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			this.owner.getRoom().checkAction();

			Global.logger.info(this.owner + " pass discardtip, id: " + id + " passcard: " + card);
		} else if (this.tipMap.containsKey(id)) {
			Tip tip = this.tipMap.get(id);
			this.tipMap.remove(id);
			if (tip.type == RuleWeight.TYPE_CHOW) {
				tip.biid = biid;
				Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<Integer, Tip> entry = it.next();
					if (entry.getValue().type == RuleWeight.TYPE_PONG) {
						this.owner.chouPongSet.add(tip.card);
					}
					if (entry.getValue().type != RuleWeight.TYPE_CHOW && entry.getValue().type != RuleWeight.TYPE_BI) {
						it.remove();
					}
				}
			} else if (tip.type == RuleWeight.TYPE_PONG) {
				Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<Integer, Tip> entry = it.next();
					if (entry.getValue().type != RuleWeight.TYPE_PONG) {
						it.remove();
					}
				}
			}
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.getRoom().addAction(new Action(tip, owner));
		} else {
			throw new Error("tip map has no id" + id);
		}
	}

	@SuppressWarnings("unchecked")
	public void doAction(int id,ITArray biid) {
		if (id == this.uid) {
			// ���Գԣ�����ȡ���ˣ���¼����
			Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
			int card = 0;
			while (it.hasNext()) {
				Map.Entry<Integer, Tip> entry = it.next();
				Tip tip = entry.getValue();
				card = tip.card;
				if (tip.type == RuleWeight.TYPE_CHOW) {
					this.owner.chouChiSet.add(tip.card);
				}
			}
			this.owner.isMingTang = true;
			this.owner.stateMachine.curState.exit(owner);
			this.owner.stateMachine.curState = this.owner.stateMachine.lastState;
			this.owner.getRoom().checkAction();

			Global.logger.info(this.owner + " pass drawtip, id: " + id + " passcard: " + card);
		} else if (this.tipMap.containsKey(id)) {
			Tip tip = this.tipMap.get(id);
			this.tipMap.remove(id);
			if (tip.type == RuleWeight.TYPE_CHOW) {
				tip.biid = biid;
				Iterator<Entry<Integer, Tip>> it = this.tipMap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<Integer, Tip> entry = it.next();
					if (entry.getValue().type != RuleWeight.TYPE_CHOW && entry.getValue().type != RuleWeight.TYPE_BI) {
						it.remove();
					}
				}
			}
			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();
		mp.putInt("weight", this.weight);
		mp.putInt("uid", this.uid);
		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;
		this.id += uid;
		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 cleanTip() {
		this.weight = 0;
		this.id = 0;
		this.uid += 100;
		this.tipMap = new HashMap<Integer, Tip>();
	}

	public void clean() {
		this.cleanTip();
	}

}
