package extend.zp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;

import com.game.Util;
import com.game.data.Player;
import com.game.data.Room;
import com.game.data.Score;
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.tip.Tip;
import extend.zp.tip.TipManager;
import extend.zp.uitl.CardUtil;

/**
 * 
 * 
 */
public class EXPlayer extends Player {

	public List<Integer>	cardInhand;


	public List<Integer>	outcardList;
	public Set<Integer>		suoCardList;

	public Tip				actionTip;
	

	public List<OpCard>		opCardList;
	public List<OpCard>		currentOpCardList;
	public Stack<OpCard>	handCard;
	public int				actionCard = 0;
	public int				winCard		= 0;
	public int				winHuxi		= 0;
	public int				huXi		= 0;
	public int				AEhuXi		= 0;
	public int				allHuXi		= 0;
	public List<MingTang>	winMtList;
	public TipManager		tipMgr;
	
	public SettleLog		settleLog;
	public boolean			draw = false;
	public ITArray			pxData	=null;
	
	public Set<Integer>				chouList;
	public boolean daPai;
	public boolean fangpao;
	public boolean isZiMo;
	public boolean siShou;

	public int hutype;

	public int winType = 0;
	public List<Integer> specialList;
	public boolean kaWei = false;
	public Map<Integer, List<Integer>> tingMap;
	public List<Integer> tingList;

	public EXPlayer(int playerid, Room table, String session_id) {
		super(playerid, table, session_id);

		cardInhand = new ArrayList<>();
		outcardList = new ArrayList<>();
		
		opCardList = new ArrayList<>();
		currentOpCardList = new ArrayList<>();

		tipMgr = new TipManager(this);
		specialList = new ArrayList<>();
		settleLog = new SettleLog();
		handCard = new Stack<OpCard>();
		winMtList = new ArrayList<>();
		tingList = new ArrayList<>();
		tingMap = new HashMap<Integer, List<Integer>>();
//		settleLog.put(Config.SETTLE_DA_DIAN_PAO, 0);
//		settleLog.put(Config.SETTLE_DA_JIE_PAO, 0);
//		settleLog.put(Config.SETTLE_DA_ZIMO, 0);
		
		this.chouList = new HashSet<Integer>();
		this.suoCardList = new HashSet<Integer>();
	}
	
	protected Score newScore() {
		return new EXScore(this);
	}

	public void paiZu() {
		List<Integer> bigCharacterList = new ArrayList<>();
		List<Integer> smallCharacterList = new ArrayList<>();
		this.handCard.clear();
		for (Integer card : this.cardInhand) {
			if (card / 100 == 2) {
				bigCharacterList.add(card);
			}else {
				smallCharacterList.add(card);
			}
		}
		zuHe(bigCharacterList);
		zuHe(smallCharacterList);
	}
	

	public Integer autoDiscard() {
		Map<Integer, Integer> cardNum = CardUtil.getCardNumMap(this.cardInhand);
		for (Entry<Integer, Integer> card : cardNum.entrySet()) {
			if (card.getValue() == 1 || card.getValue() == 2) {
				return card.getKey();
			}
		}
		return 0;
	}
	
	public void zuHe(List<Integer> cardList) {
		Collections.sort(cardList);
		for (Entry<Integer, Integer> entry : Util.getCardNumMap(cardList).entrySet()) {
			int card = entry.getKey();
			int num = entry.getValue();
			if (num >= 3) {
				Util.removeCard(cardList, card, 3);
				OpCard opCard = new OpCard(RuleWeight.TYPE_KAN, card, card, card, 0);
				this.handCard.push(opCard);
			}
		}
		List<Integer> tempList = new ArrayList<>(cardList);
		while (true) {
			if (tempList.size() < 3) break;
			int card = tempList.get(0);
			if (Util.checkCard(card + 1, tempList) && Util.checkCard(card + 2, tempList)) {
				OpCard opCard = new OpCard(RuleWeight.TYPE_CHOW, card, card+1, card+2, 0);
				this.handCard.push(opCard);
				Util.removeCard(tempList, card, 1);
				Util.removeCard(tempList, card + 1, 1);
				Util.removeCard(tempList, card + 2, 1);
				Util.removeCard(cardList, card, 1);
				Util.removeCard(cardList, card + 1, 1);
				Util.removeCard(cardList, card + 2, 1);
			}else {
				tempList.remove(0);
			}
		}
		
		for (Entry<Integer, Integer> entry : Util.getCardNumMap(cardList).entrySet()) {
			int card = entry.getKey();
			int num = entry.getValue();
			if (num == 2) {
				Util.removeCard(cardList, card, 2);
				OpCard opCard = new OpCard(RuleWeight.TYPE_JIANG, card, card, 0, 0);
				this.handCard.push(opCard);
			}
		}
		while (true) {
			if (cardList.size() >= 3) {
				int card1 = cardList.get(0), card2 = cardList.get(1), card3 = cardList.get(2);
				OpCard opCard = new OpCard(RuleWeight.TYPE_SAN, card1, card2, card3, 0);
				this.handCard.push(opCard);
				Util.removeCard(cardList, card1, 1);
				Util.removeCard(cardList, card2, 1);
				Util.removeCard(cardList, card3, 1);
			}else if (cardList.size() >= 0 && cardList.size() < 3) {
				int card1 = 0, card2 = 0;
				if (cardList.size() == 1) {
					card1 = cardList.get(0);
				}else if (cardList.size() == 2){
					card1 = cardList.get(0);
					card2 = cardList.get(1);
				}
				if (card1 > 0) {
					OpCard opCard = new OpCard(RuleWeight.TYPE_SAN, card1, card2, 0, 0);
					this.handCard.push(opCard);
				}
				break;
			}
		}
	}
	
	public ITObject getReloadInfo(boolean self) {
		ITObject playerData = super.getReloadInfo();
		ITArray disCard = Util.toTArray(this.outcardList);
		playerData.putTArray("outcard_list", disCard);
		playerData.putInt("card_count", cardInhand.size());
		playerData.putInt("score", score.total_score);
		playerData.putBoolean("sishou", this.siShou);
		ITArray opcards = TArray.newInstance();
		for (OpCard opcard : opCardList) {
			ITObject opcardParam = new TObject();
			opcardParam.putInt("type", opcard.type);
			opcardParam.putInt("huxi", opcard.type == RuleWeight.TYPE_WEI && !self ? 0 : opcard.huXi);
			opcardParam.putInt("card", opcard.card1);
			ITArray op = TArray.newInstance();
			op.addInt(opcard.card2);
			if(opcard.card3 > 0) op.addInt(opcard.card3);
			opcardParam.putTArray("opcard", op);
			opcards.addTObject(opcardParam);
		}
		playerData.putTArray("opcard", opcards);

		return playerData;
	}
	
	public void resetHuXi() {
		this.AEhuXi = 0;
		for (OpCard opCard : this.opCardList) {
			this.AEhuXi += opCard.huXi;
		}
	}

	public void clear() {
		super.clear();
		this.cardInhand.clear();
		this.draw = false;
		this.outcardList.clear();
		this.opCardList.clear();
		this.currentOpCardList.clear();
		this.pxData = null;
		this.winCard = 0;
		this.huXi = 0;
		this.winHuxi = 0;
		this.actionCard = 0;
		this.score.resetRound();
		this.chouList.clear();
		this.suoCardList.clear();
		this.daPai = false;
		this.fangpao = false;
		this.hutype = 0;
		this.AEhuXi = 0;
		this.isZiMo = false;
		this.kaWei = false;
		this.winType = 0;
		this.winMtList.clear();
		this.specialList.clear();
		this.tingMap.clear();
		this.tingList.clear();
		this.siShou = false;
	}
	

	public EXRoom getRoom() {
		return (EXRoom) room;
	}
	
}
