package com.backfire.arithmetic;

import java.util.ArrayList;
import java.util.Collections;

import com.backfire.arithmetic.CardCompare.CompareValue;
import com.backfire.arithmetic.CardJudge3_Laizi.CardType_Laizi;

public class CardCompare3_Laizi {
	public ArrayList<Card> m_CardVecFirst; // 避免频繁传参
	public ArrayList<Card> m_CardVecSecond; // 避免频繁传参

	public CardType_Laizi m_firstCardType, m_secondCardType; // 牌的类型
	public Card m_pFirstCard;// 第一副牌牌头
	public Card m_pSecondCard; // 第二副牌牌头
	public int m_iFirstNum, m_iSecondNum; // 牌的数量
	

	@Override
	public String toString() {
		return "CardCompare3_Laizi [\n m_firstCardType=" + m_firstCardType
				+", m_pFirstCard="+ m_pFirstCard
				+ ", m_iFirstNum=" + m_iFirstNum +",\n"
				+ " m_secondCardType=" + m_secondCardType + ", m_pSecondCard=" + m_pSecondCard
				+ ", m_iSecondNum="
				+ m_iSecondNum + "]";
	}

	public CardCompare3_Laizi() {
		m_CardVecFirst=new ArrayList<Card>();
		m_CardVecSecond=new ArrayList<Card>();
		
		m_firstCardType=CardType_Laizi.CardEnd;
		m_secondCardType=CardType_Laizi.CardEnd;
		
		m_pFirstCard=new Card();
		m_pSecondCard=new Card();
		
		m_iFirstNum=0;
		m_iSecondNum=0;
	}

	public void clear() {

	}

	public int compareCard(final ArrayList<Card> myCardFirst,
			final ArrayList<Card> myCardSecond) {
		int value = CompareValue.Error;
		do {
			// 判空
			if (myCardFirst.isEmpty() || myCardSecond.isEmpty()) {
				value = CompareValue.ErrorEmpty;
				break;
			}
	
			getReturnTypeDate(myCardFirst, myCardSecond);
			m_CardVecFirst = myCardFirst;
			m_CardVecSecond = myCardSecond;
	
			// 判空
			if (m_firstCardType == CardType_Laizi.CardEnd
					|| m_secondCardType == CardType_Laizi.CardEnd) {
				value = CompareValue.ErrorCardAll;
			} else if (m_firstCardType == CardType_Laizi.ROCKET
					|| m_secondCardType == CardType_Laizi.ROCKET) // 如果其中一个是王炸
			{
				value = compareOneIsROCKETOrLAIZHI_BOMBOrBOMBOrMIX_BOMB(CardType_Laizi.ROCKET);// 王炸一方大
			} else if ((m_firstCardType == CardType_Laizi.LAIZHI_BOMB && m_secondCardType != CardType_Laizi.LAIZHI_BOMB)
					|| (m_firstCardType != CardType_Laizi.LAIZHI_BOMB && m_secondCardType == CardType_Laizi.LAIZHI_BOMB)) {
				value = compareOneIsROCKETOrLAIZHI_BOMBOrBOMBOrMIX_BOMB(CardType_Laizi.LAIZHI_BOMB);// 癞子炸一边大
			} else if (((m_firstCardType == CardType_Laizi.BOMB && m_secondCardType != CardType_Laizi.BOMB) || (m_firstCardType != CardType_Laizi.BOMB && m_secondCardType == CardType_Laizi.BOMB))) {
				value = compareOneIsROCKETOrLAIZHI_BOMBOrBOMBOrMIX_BOMB(CardType_Laizi.BOMB);// 原生炸弹一方大
			} else if (((m_firstCardType == CardType_Laizi.MIX_BOMB && m_secondCardType != CardType_Laizi.MIX_BOMB) || (m_firstCardType != CardType_Laizi.MIX_BOMB && m_secondCardType == CardType_Laizi.MIX_BOMB))) {
				value = compareOneIsROCKETOrLAIZHI_BOMBOrBOMBOrMIX_BOMB(CardType_Laizi.MIX_BOMB);// 原生炸弹一方大
			} else if (m_iFirstNum != m_iSecondNum)// 数量不相同
			{
				value = CompareValue.ErrorNum;
			} else {
				value = compareSame(); // 同种牌型比较
			}
		} while (false);
		
		return value;
	}

	// 判断一方为王炸的情况
	// 判断一方为纯癞子炸的情况
	// 判断一方为原生炸弹的情况
	// 判断一方为混搭炸弹的情况
	public int compareOneIsROCKETOrLAIZHI_BOMBOrBOMBOrMIX_BOMB(
			CardType_Laizi type) {
		if (m_firstCardType == type) {
			return CompareValue.MoreThan;
		} else {
			return CompareValue.Lessthan;
		}
	}

	public int compareSame() {
		if (m_pFirstCard.getPoint() > m_pSecondCard.getPoint()) {
			return CompareValue.MoreThan;
		} else if (m_pFirstCard.getPoint() < m_pSecondCard.getPoint()) {
			return CompareValue.Lessthan;
		} else {
			return CompareValue.Equal;
		}
	}

	public void getReturnTypeDate(final ArrayList<Card> typeFirst,
			final ArrayList<Card> typeSecond) {
		CardJudge3_Laizi cardJudgeLazi = new CardJudge3_Laizi();
		Collections.sort(typeFirst,new CardAssist.Desc());
		// 比较牌
		CardReturn_Laizi myReturnSecond = cardJudgeLazi.judgeCardType(typeSecond);

		CardReturn_Laizi myReturnFirst = new CardReturn_Laizi();
		ArrayList<Card> pCardWithNoLaizi = new ArrayList<Card>(); // 没有癞子的牌，用于拆分
		ArrayList<Card> pCardIsLaizi = new ArrayList<Card>(); // 有癞子的牌，用于拆分
		
		ArrayList<Card> pCoyp = CardAssist.copyList(typeFirst);
		
		for (int i = 0; i < pCoyp.size(); ++i) {
			if (pCoyp.get(i).isLaiZi()) {
				pCardIsLaizi.add(pCoyp.get(i));
				continue;
			}
			pCardWithNoLaizi.add(pCoyp.get(i));
		}

		if (pCardIsLaizi.isEmpty()) {
			CardJudge3 myJundge = new CardJudge3();
			CardReturn myReturn = myJundge.judgeCardType(pCoyp);
			myReturnFirst.copy(myReturn.mCardType, myReturn.mCardHead,
					myReturn.mCardNum);
		} else {
			switch (myReturnSecond.mCardType) {
			case SOLO:
				if (pCoyp.size() == 1)
					cardJudgeLazi.judgeSOLO(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case PAIR:
				if (pCoyp.size() == 2)
					cardJudgeLazi.judgePAIR(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case TRIO:
				if (pCoyp.size() == 3)
					cardJudgeLazi.judgeTRIO(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case CHAIN:
				if (pCoyp.size() >= 5)
					cardJudgeLazi.judgeCHAIN(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case TRIO_X:
				if (pCoyp.size() == 4)
					cardJudgeLazi.judgeTRIO_WITH_1(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst);
				else if (pCoyp.size() == 5)
					cardJudgeLazi.judgeTRIO_WITH_2(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst);
				break;

			case PAIR_CHAIN:
				if (pCoyp.size() >= 6)
					cardJudgeLazi.judgePAIR_CHAIN(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst);
				break;

			case PLANE:
				if (pCoyp.size() >= 6)
					cardJudgeLazi.judgeTRIO_CHAIN(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst);
				break;

			case PLANE_1:
				if (pCoyp.size() >= 8)
					cardJudgeLazi.judgePLANE_1(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case PLANE_2:
				if (pCoyp.size() >= 10)
					cardJudgeLazi.JudgePLANE_2(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case FOUR_WITH_X:
				if (pCoyp.size() == 6)
					cardJudgeLazi.judgeFOUR_WITH_2(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst); // 考虑带王的问题
				else if (pCoyp.size() == 8)
					cardJudgeLazi.judgeFOUR_WITH_4(pCardWithNoLaizi,
							pCardIsLaizi, myReturnFirst);
				break;

			case MIX_BOMB:
				if (pCoyp.size() == 4)
					cardJudgeLazi.judgeBOMB(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case BOMB:
				if (pCoyp.size() == 4)
					cardJudgeLazi.judgeBOMB(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case LAIZHI_BOMB:
				if (pCoyp.size() == 4)
					cardJudgeLazi.judgeBOMB(pCardWithNoLaizi, pCardIsLaizi,
							myReturnFirst);
				break;

			case CardEnd:
				break;
			}

			if (pCoyp.size() == 4) {
				cardJudgeLazi.judgeBOMB(pCardWithNoLaizi, pCardIsLaizi,
						myReturnFirst);
			} else if (pCoyp.size() == 2) {
				if ((pCoyp.get(0).getPoint() == Card.PointBigJoker)
						&& (pCoyp.get(1).getPoint() == Card.PointSmallJoker)) {
					myReturnFirst.copy(CardType_Laizi.ROCKET, pCoyp.get(0),
							2);
				}
			}
		}

		// ----------------------------------------------------------------------

		if (myReturnFirst.mCardType == CardType_Laizi.CardEnd) {
			return;
		}

		m_firstCardType = myReturnFirst.mCardType;
		m_iFirstNum = myReturnFirst.mCardNum;
		if (m_pFirstCard != null) {
			m_pFirstCard = null;
		}
		m_pFirstCard = new Card(myReturnFirst.mCardHead);
		//---------------------------------------
		m_secondCardType = myReturnSecond.mCardType;
		m_iSecondNum = myReturnSecond.mCardNum;
		if (m_pSecondCard != null) {
			m_pSecondCard = null;
		}
		m_pSecondCard = new Card(myReturnSecond.mCardHead);
	}
}
