package com.backfire.arithmetic;

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

import com.backfire.arithmetic.CardJudge.CardType;

public abstract class CardCompare {

	public class CompareValue {
		// 第一个小于第二个
		public static final int Lessthan = -1;
		// 第一个等于第二个
		public static final int Equal = 0;
		// 第一个大于第二个
		public static final int MoreThan = 1;
		// 参数为空
		public static final int ErrorEmpty = -2;
		// 类型获取失败
		public static final int ErrorCardAll = -3;
		// 数量不相同
		public static final int ErrorNum = -4;
		// 其他错误
		public static final int Error = -5;
	}

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

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

	public CardCompare() {
		m_CardVecFirst=new ArrayList<Card>();
		m_CardVecSecond=new ArrayList<Card>();
		
		m_pFirstCard=new Card();
		m_pSecondCard=new Card();
		
		m_iFitstNum=0;
		m_iSecondNum=0;
	}

	public void clear() {

	}

	// 判断牌的大小 参数一表示手牌 参数二表示需要比较的值 返回值比较结果
	public abstract int compareCard(final ArrayList<Card> myCardFirst,
			final ArrayList<Card> myCardSecond);

	/** 同种牌型判断 **/
	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 {
			//说明：加了此段，777 66  < 777 88  这个是错的
			/*for (int i = 0; i < m_CardVecFirst.size(); ++i) {
				if (m_CardVecFirst.get(i).getPoint() == m_CardVecSecond.get(i)
						.getPoint())
					continue;
				else if (m_CardVecFirst.get(i).getPoint() > m_CardVecSecond
						.get(i).getPoint())
					return CompareValue.MoreThan;
				else
					return CompareValue.Lessthan;
			}*/
			return CompareValue.Equal;
		}
	}

	/** 比较炸弹 **/
	public int compareBomb() {
		// 类型相同
		if (m_firstCardType.ordinal() == m_secondCardType.ordinal()) {
			return compareSame();// 同种类型比较
		} else if (m_firstCardType.ordinal() > m_secondCardType.ordinal()) {
			return CompareValue.MoreThan;
		}
		return CompareValue.Lessthan;
	}

	public void getReturnTypeDate(final CardReturn typeFirst,
			final CardReturn typeSecond) {
		m_firstCardType = typeFirst.mCardType;
		m_iFitstNum = typeFirst.mCardNum;
		if (m_pFirstCard != null) {
			m_pFirstCard = null;
		}
		m_pFirstCard = new Card(typeFirst.mCardHead);
		//--------------------------------------
		m_secondCardType = typeSecond.mCardType;
		m_iSecondNum = typeSecond.mCardNum;
		if (m_pSecondCard != null) {
			m_pSecondCard = null;
		}
		m_pSecondCard = new Card(typeSecond.mCardHead);
	}
	
	public static boolean isEqualCards(ArrayList<Card> cards1, ArrayList<Card> cards2) {
		boolean result = false;
		if (cards1 == cards2) {
			return true;
		}
		if (cards1 != null && cards2 != null && cards1.size() == cards2.size()) {
			Collections.sort(cards1, new CardAssist.Desc());
			Collections.sort(cards2, new CardAssist.Desc());
			boolean isEqual = false;
			do {
				for (int i = 0; i < cards1.size(); i++) {
					Card card1 = cards1.get(i);
					Card card2 = cards2.get(i);
					if (null == card1 || null == card2
							|| !card1.isEqualTo(card2)) {
						return false;
					}
				}
				isEqual = true;
			} while (false);
			if (isEqual) {
				result = isEqual;
			}
		}
		return result;
	}
}
