package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.room.Interface.IClickListener;
import com.backfire.room.data.Player;
import com.backfire.room.data.ResCenter;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.RoomInfo.GameType;
import com.backfire.room.data.RoomInfo.Megagame;
import com.backfire.utils.Log;

/**
 * @Title: SettleAccountWindow.java
 * @Description: 结算对话框
 * @author Potter
 * @date 2013年10月31日 下午5:27:10
 * @version V2.0
 */
public class SettleAccountWindow extends BasePopupWindow {
	private View view;
	private int width;
	private int height;

	private ImageView bg;

	private ImageView selfIcon;
	private TextView selfNick;
	private TextView selfAchieve;
	private TextView selfDouble;
	private TextView selfSpring;
	private TextView selfBomb;
	private TextView selfActive;
	private ImageView selfAchieveTag;// 如果为普通场就为金币。如果为比赛场，胜利就彩卷，失败为金币。

	private ImageView other1Icon;
	private TextView other1Nick;
	private TextView other1Achieve;
	/*
	 * private TextView other1Double; private TextView other1Spring; private
	 * TextView other1Bomb; private TextView other1Active;
	 */
	private ImageView other1AcheiveTag;

	private ImageView other2Icon;
	private TextView other2Nick;
	private TextView other2Achieve;
	/*
	 * private TextView other2Double; private TextView other2Spring; private
	 * TextView other2Bomb; private TextView other2Active;
	 */
	private ImageView other2AcheiveTag;

	private ImageButton exitBtn;
	private ImageButton refightBtn;
	private MyOnClickListener listener;
	private IClickListener iClickListener;
	private int exitMsg;
	private int refightMsg;

	public SettleAccountWindow(ActivityBase activity) {
		super(activity);
		try {
			init();
		} catch (Exception e) {
			Log.e(this, e);
		}
	}

	private void init() throws Exception {
		LayoutInflater layoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRes = R.layout.settle_account_window;

		view = layoutInflater.inflate(layoutRes, null);
		view.setFocusableInTouchMode(true);

		bg = (ImageView) view.findViewById(R.id.settle_account_bg);
		// 自己玩家
		selfIcon = (ImageView) view.findViewById(R.id.selfIcon);
		selfNick = (TextView) view.findViewById(R.id.selfNick);
		selfAchieve = (TextView) view.findViewById(R.id.selfAchieve);
		selfDouble = (TextView) view.findViewById(R.id.selfDouble);
		selfSpring = (TextView) view.findViewById(R.id.selfSpring);
		selfBomb = (TextView) view.findViewById(R.id.selfBomb);
		selfActive = (TextView) view.findViewById(R.id.selfActive);
		selfAchieveTag = (ImageView) view.findViewById(R.id.selfAchieveTag);
		// 左下角玩家
		other1Icon = (ImageView) view.findViewById(R.id.other1Icon);
		other1Nick = (TextView) view.findViewById(R.id.other1Nick);
		other1Achieve = (TextView) view.findViewById(R.id.other1Score);
		/*
		 * other1Double=(TextView)view.findViewById(R.id.otherDouble1);
		 * other1Spring=(TextView)view.findViewById(R.id.otherSpring1);
		 * other1Bomb=(TextView)view.findViewById(R.id.otherBomb1);
		 * other1Active=(TextView)view.findViewById(R.id.otherActive1);
		 */
		other1AcheiveTag = (ImageView) view.findViewById(R.id.other1AchieveTag);
		// 右下角玩家
		other2Icon = (ImageView) view.findViewById(R.id.other2Icon);
		other2Nick = (TextView) view.findViewById(R.id.other2Nick);
		other2Achieve = (TextView) view.findViewById(R.id.other2Achieve);
		/*
		 * other2Double=(TextView)view.findViewById(R.id.otherDouble2);
		 * other2Spring=(TextView)view.findViewById(R.id.otherSpring2);
		 * other2Bomb=(TextView)view.findViewById(R.id.otherBomb2);
		 * other2Active=(TextView)view.findViewById(R.id.otherActive2);
		 */
		other2AcheiveTag = (ImageView) view.findViewById(R.id.other2AchieveTag);

		// ----处理数据
		RoomInfo roomInfo = RoomInfo.getInstance();
		boolean isMegagame = roomInfo.getMegagame() == Megagame.BIG_PRIZE ? true
				: false;
		int selfSeatid = roomInfo.getSeatId();
		Player selfPlayer = roomInfo.getPlayer(selfSeatid);
		if (null != selfPlayer) {
			synchronized (selfPlayer) {
				boolean isWin = selfPlayer.isWin();
				if (isWin) {
					bg.setBackgroundResource(R.drawable.win_dialog);
				} else {
					bg.setBackgroundResource(R.drawable.fail_dialog);
				}
				// ----self结算信息----
				selfIcon.setBackgroundResource(ResCenter.getIcon(
						selfPlayer.getIcon(), selfPlayer.getSide(),
						selfPlayer.getSex(), isWin));
				selfNick.setText(selfPlayer.getNick());
				selfDouble.append(Integer.toString(selfPlayer.getWinRatio()));
				selfSpring.append(Integer.toString(selfPlayer.getSpring()));
				selfBomb.append(Integer.toString(selfPlayer.getBombs()));
				selfActive.append(Integer.toString(selfPlayer.getReward()));

				if (isMegagame && isWin) {
					selfAchieveTag
							.setBackgroundResource(R.drawable.js_lottery_icon);
					setTextColor(isWin, selfPlayer.getWinLottery(), selfAchieve);
				} else {
					selfAchieveTag.setBackgroundResource(R.drawable.js_vcoin_icon);
					setTextColor(isWin, selfPlayer.getWinMoney(), selfAchieve);
				}
			}
		}

		// ----other1结算信息----
		int other1Seatid = roomInfo.getPreSeatId(selfSeatid);
		Player other1Player = roomInfo.getPlayer(other1Seatid);
		if (null != other1Player) {
			synchronized (other1Player) {
				other1Icon.setBackgroundResource(ResCenter.getIcon(
						other1Player.getIcon(), other1Player.getSide(),
						other1Player.getSex(), other1Player.isWin()));
				other1Nick.setText(other1Player.getNick());
				/*
				 * other1Double.append(Integer.toString(other1Player.getWinRatio()));
				 * other1Spring.append(Integer.toString(other1Player.getSpring()));
				 * other1Bomb.append(Integer.toString(other1Player.getBombs()));
				 * other1Active.append(Integer.toString(other1Player.getReward()));
				 */

				boolean isWin = other1Player.isWin();
				if (isMegagame && isWin) {
					other1AcheiveTag
							.setBackgroundResource(R.drawable.js_lottery_icon);
					setTextColor(isWin, other1Player.getWinLottery(), other1Achieve);
				} else {
					other1AcheiveTag
							.setBackgroundResource(R.drawable.js_vcoin_icon);
					setTextColor(isWin, other1Player.getWinMoney(), other1Achieve);
				}
			}
		}

		// ----other2结算信息----
		int other2Seatid = roomInfo.getNextSeatId(selfSeatid);
		Player other2Player = roomInfo.getPlayer(other2Seatid);
		if (null != other2Player) {
			synchronized (other2Player) {
				other2Icon.setBackgroundResource(ResCenter.getIcon(
						other2Player.getIcon(), other2Player.getSide(),
						other2Player.getSex(), other2Player.isWin()));
				other2Nick.setText(other2Player.getNick());
				/*
				 * other2Double.append(Integer.toString(other2Player.getWinRatio()));
				 * other2Spring.append(Integer.toString(other2Player.getSpring()));
				 * other2Bomb.append(Integer.toString(other2Player.getBombs()));
				 * other2Active.append(Integer.toString(other2Player.getReward()));
				 */

				boolean isWin = other2Player.isWin();
				if (isMegagame && isWin) {
					other2AcheiveTag
							.setBackgroundResource(R.drawable.js_lottery_icon);
					setTextColor(isWin, other2Player.getWinLottery(), other2Achieve);
				} else {
					other2AcheiveTag
							.setBackgroundResource(R.drawable.js_vcoin_icon);
					setTextColor(isWin, other2Player.getWinMoney(), other2Achieve);
				}
			}
		}

		// ----listener-----
		listener = new MyOnClickListener();
		// ----按钮-----
		exitBtn = (ImageButton) view.findViewById(R.id.settle_account_exitBtn);
		exitBtn.setOnClickListener(listener);
		refightBtn = (ImageButton) view
				.findViewById(R.id.settle_account_refightBtn);
		refightBtn.setOnClickListener(listener);

		this.setFocusable(true);
		this.setConsumeKeyEvent(false);

		width = DdzApplication.getApplication().screenWidth;
		height = DdzApplication.getApplication().screenHeight;
		this.createPopupWindow(view, width, height);

		Log.i(this,
				"view 's info:L=" + view.getLeft() + ",R=" + view.getRight()
						+ ",T=" + view.getTop() + ",B=" + view.getBottom()
						+ "W=" + view.getWidth() + ",H=" + view.getHeight());
	}

	public void setTextColor(boolean isWin, long score, TextView textView) {
		if (isWin) {
			textView.setTextColor(Color.YELLOW);
			textView.setText("+" + score);
		} else {
			textView.setTextColor(Color.RED);
			textView.setText(Long.toString(score));
		}
	}

	public void registerListenerInfo(int exitMsg, int refightMsg,
			IClickListener iClickListener) {
		this.exitMsg = exitMsg;
		this.refightMsg = refightMsg;
		this.iClickListener = iClickListener;
	}

	@Override
	protected boolean popupWindowWillShow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void popupWindowDidShow() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void popupWindowWillDissmiss() {
		// TODO Auto-generated method stub

	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View item) {
			if (item.equals(exitBtn)) {
				CloseWindow();
				iClickListener.sendMsg(exitMsg);
			} else if (item.equals(refightBtn)) {
				CloseWindow();
				iClickListener.sendMsg(refightMsg);
			}
		}
	}

}
