package taurus.util;

import java.util.*;
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;




public class Paixing {

	private final static boolean qs_yijihua_check(Map<Integer, Integer> cardMap,int se,int index) {
		Integer num = cardMap.get(se+index);
		if(num!=null&&num==1) {
			for(int i=1;i<=9;++i) {
				if(i !=index) {
					if(cardMap.containsKey(se+i)){
						return false;
					}
				}
			}
			return true;
		}
		return false;
	}





	public static void main(String[] args) {
		List<Integer> cardInhand = new java.util.ArrayList<>();
		List<Integer> opCards = new java.util.ArrayList<>();
		//opCards.add(new OpCard(RuleWeight.TYPE_PONG, 109));
		//opCards.add(new OpCard(RuleWeight.TYPE_PONG, 209));
		cardInhand.add(206);
		cardInhand.add(206);
		cardInhand.add(206);
		cardInhand.add(205);
		cardInhand.add(101);
		//cardInhand.add(204);

		cardInhand.add(107);
		cardInhand.add(107);
		cardInhand.add(104);
		cardInhand.add(104);
		cardInhand.add(101);
		cardInhand.add(101);

		cardInhand.add(101);

		List<Integer> chi = new ArrayList<>();
		List<Integer> peng = new ArrayList<>();
		List<Integer> gang = new ArrayList<>();


		System.out.println(tingCheck( cardInhand,chi,peng,gang));
	}

	private final static boolean quanqiuren(List<Integer> opCards,List<Integer> cardInhand, int drawCard) {
		if (cardInhand.size() > 1) {
			return false;
		}
		if(drawCard == 0)return true;
		if (cardInhand.get(0) != drawCard)
			return false;
		return true;
	}

	private final static boolean qingyise(List<Integer> opCards, List<Integer> cardInhand, int drawCard) {
		int se = cardInhand.get(0) / 100;
		if (drawCard!=0&&drawCard / 100 != se)
			return false;
		for (Integer card : opCards) {
			if (se != card / 100) {
				return false;
			}
		}
		for (int card : cardInhand) {
			if (se != card / 100) {
				return false;
			}
		}
		return true;
	}

	private final static boolean pongpong(List<Integer> opCards, List<Integer> cardInhand, int drawCard,boolean quanqiuren) {
		int card1count=0,card2count=0;
		Map<Integer, Integer> cardMap =Util.getCardNumMap(cardInhand);
		for (Entry<Integer, Integer> entry : cardMap.entrySet()) {
			int card = entry.getKey();
			int num = entry.getValue();
			if(num == 4) return false;
			switch(num) {
				case 1:
					if(card1count>=1)return false;
					if(drawCard!=0&&card != drawCard)return false;
					card1count++;
					break;
				case 2:
					if(card2count >=2)return false;
					card2count ++;
					break;
			}
		}
		if(card1count>0&&card2count>0)return false;
		if(card1count>0&&drawCard!=0&&!cardMap.containsKey(drawCard))return false;
		if(card2count == 2&&drawCard!=0) {
			if(!cardMap.containsKey(drawCard))return false;
			if(cardMap.get(drawCard)!=2)return false;
		}
		return true;
	}

	private final static boolean jiangjiang(List<Integer> opCards, List<Integer> cardInhand, int drawCard) {
		if(drawCard!=0){
			if (drawCard % 100 != 2 && drawCard % 100 != 5 && drawCard % 100 != 8) {
				return false;
			}
		}
		for (Integer card : opCards) {
			if (card % 100 != 2 && card % 100 != 5 && card % 100 != 8) {
				return false;
			}
		}
		for (int card : cardInhand) {
			if (card % 100 != 2 && card % 100 != 5 && card % 100 != 8) {
				return false;
			}

		}
		return true;
	}

	private final static int qixiaodui(List<Integer> opCards, List<Integer> cardInhand, int drawCard) {
		if (cardInhand.size() != 13) {
			return -1;
		}
		List<Integer> cardlist = cardInhand;
		int pair = 0;
		int card4count=0;
		Map<Integer, Integer> cardMap =Util.getCardNumMap(cardlist);
		for (Entry<Integer, Integer> entry : cardMap.entrySet()) {
			int num = entry.getValue();
			int card = entry.getKey();
			if (num == 4) {
				pair +=2;
				card4count ++;
			}else if(num >=2) {
				if(num==3&&drawCard!=0&&drawCard != card) {
					return -1;
				}
				pair +=1;
			}else if(num ==1) {
				if(drawCard!=0&&drawCard != card) {
					return -1;
				}
			}
		}
		if(pair<6) return -1;
		if(drawCard ==0)return card4count+1;
		if(!cardMap.containsKey(drawCard)) return -1;
		if(cardMap.get(drawCard) == 3) {
			card4count++;
		}
		return card4count+1;
	}

	public final static WinCardType putWinCardType(Map<Integer, WinCardType> map, int type, int value,int score) {
		if (map == null)
			return null;
		if (map.containsKey(type)) {
			WinCardType wct = map.get(type);
			wct.value += value;
			return wct;
		} else {
			WinCardType wct = new WinCardType(type, value,score);
			map.put(type, wct);
			return wct;
		}
	}

	public static boolean checkWin(Map<Integer, WinCardType> map, List<Integer> opCards, List<Integer> cardInhand,int drawCard, int difen) {
		return checkWin(map,opCards,cardInhand,drawCard,true, difen);
	}

	public static boolean checkWin(Map<Integer, WinCardType> map, List<Integer> opCards, List<Integer> cardInhand,int drawCard,boolean jiang, int difen) {
		int qixiaodui = qixiaodui(opCards,cardInhand, drawCard);
		if (qixiaodui != -1) {
			putWinCardType(map, WinCardType.QIXIAODUI, qixiaodui,WinCardType.WIN_TYPE_SCORE);
			if (qingyise(opCards,cardInhand, drawCard)) {
				putWinCardType(map, WinCardType.QINGYISE, 1,WinCardType.WIN_TYPE_SCORE);
			}
			if (jiangjiang(opCards,cardInhand, drawCard)) {
				putWinCardType(map, WinCardType.JIANGJIANGHU, 1,WinCardType.WIN_TYPE_SCORE);
			}
			return true;
		}
		if (jiangjiang(opCards,cardInhand, drawCard)) {
			putWinCardType(map, WinCardType.JIANGJIANGHU, 1,WinCardType.WIN_TYPE_SCORE);
			boolean _qqr =quanqiuren(opCards,cardInhand, drawCard);
			if (_qqr) {
				putWinCardType(map, WinCardType.QUANQIUREN, 1,WinCardType.WIN_TYPE_SCORE);
			}
			if (pongpong(opCards,cardInhand, drawCard,_qqr)) {
				putWinCardType(map, WinCardType.PENGPENGHU, 1,WinCardType.WIN_TYPE_SCORE);
			}
			return true;
		}
		if (quanqiuren(opCards,cardInhand, drawCard)) {
			putWinCardType(map, WinCardType.QUANQIUREN, 1,WinCardType.WIN_TYPE_SCORE);
			if (qingyise(opCards,cardInhand, drawCard)) {
				putWinCardType(map, WinCardType.QINGYISE, 1,WinCardType.WIN_TYPE_SCORE);
			}
			if (jiangjiang(opCards,cardInhand, drawCard)) {
				putWinCardType(map, WinCardType.JIANGJIANGHU, 1,WinCardType.WIN_TYPE_SCORE);
			}
			if (pongpong(opCards,cardInhand, drawCard,true)) {
				putWinCardType(map, WinCardType.PENGPENGHU, 1,WinCardType.WIN_TYPE_SCORE);
			}
			return true;
		}
		if (pongpong(opCards,cardInhand, drawCard,false)) {
			putWinCardType(map, WinCardType.PENGPENGHU, 1,WinCardType.WIN_TYPE_SCORE);
			if (qingyise(opCards,cardInhand, drawCard)) {
				putWinCardType(map, WinCardType.QINGYISE, 1,WinCardType.WIN_TYPE_SCORE);
			}
			return true;
		}
		if (qingyise(opCards,cardInhand, drawCard)) {
			WinCard win = new WinCard(cardInhand, drawCard);
			win.jiang = false;
			if (win.tryWin()) {
				putWinCardType(map, WinCardType.QINGYISE, 1,WinCardType.WIN_TYPE_SCORE);
				return true;
			}
		} else {
			WinCard win = new WinCard(cardInhand, drawCard);
			win.jiang = jiang;
			if (win.tryWin()) {
				putWinCardType(map, WinCardType.XIAOHU, 1, difen);
				return true;
			}
		}
		return false;
	}

	static public boolean tingCheck(List<Integer> cardInhand,List<Integer> chowGrop,List<Integer> pengGroup,List<Integer> gangdepai) {
		List<Integer> listadd = new ArrayList<>();
		listadd.addAll(chowGrop);
		listadd.addAll(pengGroup);
		listadd.addAll(gangdepai);

		if(qixiaodui(listadd,cardInhand, 0)!=-1)return true;

		if (quanqiuren(chowGrop,cardInhand, 0))return true;

		if (chowGrop.size()==0){
			if (jiangjiang(pengGroup,cardInhand, 0))return true;
		}
		if (chowGrop.size()==0){
			if (pongpong(pengGroup,cardInhand, 0,false))return true;
		}

		if (qingyise(listadd,cardInhand, 0)) {
			WinCard win = new WinCard(cardInhand, 0);
			win.jiang = false;
			return win.tryWin();
		}
		return false;
		//WinCard win = new WinCard(cardInhand, 0);
		//return win.tryWin();
	}


	static public boolean tingKongCheck(List<Integer> opCards,List<Integer> cardInhand,int kongCard,boolean jiang) {
		if (quanqiuren(opCards,cardInhand, 0))return true;
		if (jiangjiang(opCards,cardInhand, kongCard))return true;
		if (pongpong(opCards,cardInhand, 0,false))return true;
		if (qingyise(opCards,cardInhand, kongCard)) {
			WinCard win = new WinCard(cardInhand, 0);
			win.jiang = false;
			return win.tryWin();
		}
		WinCard win = new WinCard(cardInhand, 0);
		win.jiang = jiang;
		return win.tryWin();
	}

}
