package extend.zp.uitl;

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

import com.game.Util;
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 ChowCard {
	private Map<Integer, Integer> cardMap;
	private int eventCard;
	private int biCard;
	private int eventCardType;
	private int modCard;
	
	public ChowCard(List<Integer> cardInhand,int eventCard) {
		this.eventCard = eventCard;
		this.eventCardType = eventCard / 100;
		int bg_num = this.eventCardType == 1 ? 100 : -100;
		this.biCard = eventCard+bg_num;
		this.modCard = eventCard % 100;
		this.cardMap = Util.getCardNumMap(cardInhand);
		List<Integer> removeList = new ArrayList<Integer>();
		for (Entry<Integer, Integer> entry1 : cardMap.entrySet()) {
			int card = entry1.getKey();
			int num = entry1.getValue();
			if(num>=3)removeList.add(card);
		}
		
		for(Integer card : removeList) {
			cardMap.remove(card);
		}
	}
	
	private boolean handle(ITArray p_list,int opcard1,int opcard2) {
		ITArray opcard = TArray.newInstance();
		opcard.addInt(opcard1);
		opcard.addInt(opcard2);
		boolean result = true;
		ITObject obj = TObject.newInstance();
		ITArray list = TArray.newInstance();
		obj.putTArray("opcard", opcard);
		obj.putTArray("bi_list", list);
		if(CardUtil.checkCard(eventCard, cardMap)) {
			CardUtil.addCard(eventCard, cardMap, -1);
			CardUtil.addCard(opcard1, cardMap, -1);
			CardUtil.addCard(opcard2, cardMap, -1);
			result = tryChow(list);
			CardUtil.addCard(eventCard, cardMap, 1);
			CardUtil.addCard(opcard1, cardMap, 1);
			CardUtil.addCard(opcard2, cardMap, 1);
		}
		if(list.size()==0)obj.remove("bi_list");
		if(result)p_list.addTObject(obj);
		return result;
	}
	
	
	public boolean tryChow(ITArray list){
		boolean result = false;
		if (CardUtil.checkCard(eventCard - 1, cardMap)  && CardUtil.checkCard(eventCard - 2, cardMap)) {
			result = handle(list,eventCard-1,eventCard-2) || result;
		}
		
		if (CardUtil.checkCard(eventCard + 1, cardMap) && CardUtil.checkCard(eventCard - 1, cardMap)) {
			result = handle(list,eventCard+1,eventCard-1) || result;
		}
		
		if (CardUtil.checkCard(eventCard + 1, cardMap) && CardUtil.checkCard(eventCard + 2, cardMap)) {
			result = handle(list,eventCard+1,eventCard+2) || result;
		}
		int type = this.eventCardType;
		int bi_card = this.biCard;
		int mod_card = this.modCard;
		
		if(mod_card == 2&&CardUtil.checkCard(type*100+7, cardMap)&&CardUtil.checkCard(type*100+10, cardMap)) {
			result = handle(list,type*100+7,type*100+10) || result;
		}else if(mod_card == 7&&CardUtil.checkCard(type*100+2, cardMap)&&CardUtil.checkCard(type*100+10, cardMap)) {
			result = handle(list,type*100+2,type*100+10) || result;
		}else if(mod_card == 10&&CardUtil.checkCard(type*100+2, cardMap)&&CardUtil.checkCard(type*100+7, cardMap)) {
			result = handle(list,type*100+2,type*100+7) || result;
		}
		
		if (cardMap.containsKey(bi_card) && cardMap.get(bi_card) == 2) {
			result = handle(list,bi_card,bi_card) || result;
		}
		if (CardUtil.checkCard(eventCard, cardMap) &&CardUtil.checkCard(bi_card, cardMap)) {
			CardUtil.addCard(eventCard, cardMap, -1);
			result = handle(list,eventCard,bi_card) || result;
			CardUtil.addCard(eventCard, cardMap, 1);
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		ArrayList<Integer> cardInhand = new ArrayList<Integer>();
		String cardStr = "101, 102, 102, 103, 104, 106, 106, 110, 110, 201, 201, 202, 203, 204, 205, 206, 209, 209, 209, 210";
		String[] cards = cardStr.split(", ");
		for(String c : cards) {
			Integer card = Integer.valueOf(c);
			cardInhand.add(card);
		}
		
		
		ChowCard chow = new ChowCard(cardInhand, 101);
		ITArray list = TArray.newInstance();
		chow.tryChow(list);
		//System.out.println(list);
	}
}
