package com.lordcard.common.util;


import com.zzyddz.shui.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.lordcard.common.exception.CrashApplication;
import com.lordcard.constant.CacheKey;
import com.lordcard.constant.Constant;
import com.lordcard.constant.Database;
import com.lordcard.entity.GameUser;
import com.lordcard.network.cmdmgr.ClientCmdMgr;
import com.lordcard.network.cmdmgr.CmdUtils;
import com.lordcard.network.http.GameCache;
import com.lordcard.ui.LoginActivity;
import com.lordcard.ui.TaskMenuActivity;
import com.lordcard.ui.dizhu.DoudizhuRoomListActivity;
import com.lordcard.ui.personal.PersonnalDoudizhuActivity;
import com.lordcard.ui.view.dialog.BaoXiangDialog;
import com.lordcard.ui.view.dialog.GameDialog;
import com.lordcard.ui.view.dialog.SingleDialog;
import com.sdk.util.SDKFactory;
import com.umeng.analytics.MobclickAgent;

public class DialogUtils {

	/** Toast鎻愮ず娑堟伅 */
	public static void toastTip(String msg) {
		toastTip(msg, Toast.LENGTH_SHORT, 0);
	}

	// /** Toast鎻愮ず娑堟伅 */
	// public static void toastTip(String msg,int showTime) {
	// toastTip(msg, showTime,0);
	// }
	/** 鎻愮ず灞呬腑鍦熷徃 */
	public static void mesToastTip(final String msg) {
		toastTip(msg, 0, Gravity.CENTER);
	}

	/**
	 * Toast鎻愮ず娑堟伅
	 * 
	 * @Title: toastTip
	 * @param @param msg 鎻愮ず鐨勬秷鎭?
	 * @param @param showTime 鏄剧ず鐨勬椂闂?灏忎簬0榛樿鏃堕棿
	 * @param @param align 鍨傜洿浣嶇疆 0 榛樿浣嶇疆
	 * @return void
	 * @throws
	 */
	public static void toastTip(final String msg, final int showTime, final int align) {
		try {
			Database.currentActivity.runOnUiThread(new Runnable() {

				public void run() {
					MultiScreenTool mst = MultiScreenTool.singleTonHolizontal();
					View toastRoot = Database.currentActivity.getLayoutInflater().inflate(R.layout.my_text_toast, null);
					toastRoot.getBackground().setAlpha(85);// 0~255閫忔槑搴﹀€?
					Toast toast = new Toast(CrashApplication.getInstance());
					if (showTime > 0) {
						toast.setDuration(showTime);
					}
					mst.adjustView(toastRoot);
					toast.setView(toastRoot);
					TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
					mst.adjustView(tv);
					if (!TextUtils.isEmpty(msg)) {
						tv.setText(Html.fromHtml(msg));
						if (align != 0) {
							toast.setGravity(align, 0, 0);
						}
						toast.show();
					}
				}
			});
		} catch (Exception e) {}
	}

	/** 鎻愮ず瀵硅瘽妗?*/
	public static void mesTip(final String msg, final boolean isFinish) {
		mesTip(msg, true, isFinish);
	}

	/** 鎻愮ず瀵硅瘽妗?*/
	public static void mesTip(final String msg, final boolean showCancel, final boolean isFinish) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, showCancel) {

						public void okClick() {
							if (isFinish) {
								ActivityUtils.finishAcitivity();
							}
						}
					};
					if (null != Database.currentActivity) {
						gameDialog.show();
						gameDialog.setText(msg);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/** 寮€瀹濈 */
	public static void baoxiangTip(final String msg, final boolean isFinish) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					BaoXiangDialog baoXiangDialog = new BaoXiangDialog(Database.currentActivity) {

						public void okClick() {
							if (isFinish) {
								ActivityUtils.finishAcitivity();
							}
						}
					};
					baoXiangDialog.show();
					baoXiangDialog.setText(msg);
				} catch (Exception e) {}
			}
		});
	}

	/** 鍗曚釜纭畾鎻愮ず */
	public static void mesTipSingle(final String msg, final boolean isFinish) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					SingleDialog gameDialog = new SingleDialog(Database.currentActivity, isFinish) {

						public void okClick() {
							dismiss();
						}
					};
					gameDialog.show();
					gameDialog.setText(msg);
				} catch (Exception e) {}
			}
		});
	}

	/** 鐧诲綍澶辫触 */
	public static void loginFail() {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, false) {

						public void okClick() {
							ActivityUtils.finishAcitivity();
						}
					};
					gameDialog.show();
					gameDialog.setText("鐧诲綍澶辫触锛岃绋嶅€欏啀璇曪紒");
				} catch (Exception e) {}
			}
		});
	}

	/** 閲嶆柊鐧诲綍娓告垙 */
	public static void reLogin(final Activity act) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, false) {

						public void okClick() {
							GameCache.remove(CacheKey.GAME_USER);
							Intent in = new Intent();
							in.setClass(act, SDKFactory.getLoginView());
							act.startActivity(in);
							ActivityUtils.finishAcitivity();
						}
					};
					gameDialog.show();
					gameDialog.setText("鐢ㄦ埛淇℃伅杩囨湡锛岃閲嶆柊鐧诲綍锛?);
				} catch (Exception e) {}
			}
		});
	}

	public static void sentBeanTip() {
		toastTip("鑻遍泟涔熸湁钀介毦鏃讹紝鎴戜滑涓轰綘鍑嗗浜? + Database.SEND_BEAN + "閲戣眴锛屽姪浣犱笢灞卞啀璧枫€?);
	}

	public static void receiveAward() {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity) {

						public void okClick() {
							Intent in = new Intent();
							in.setClass(Database.currentActivity, TaskMenuActivity.class);
							in.putExtra("award", true);
							Database.currentActivity.startActivity(in);
						}
					};
					gameDialog.show();
					gameDialog.setText(Database.currentActivity.getText(R.string.award).toString());
				} catch (Exception e) {}
			}
		});
	}

	/**
	 * 涓庢湇鍔″櫒鏂紑杩炴帴
	 * 
	 * @param tipMsgId
	 *            string.xml璧勬簮鏂囦欢鎻愮ず淇℃伅id
	 */
	public static void netFailTip(int tipMsgId) {
		final String msg = Database.currentActivity.getResources().getString(tipMsgId);
		final Activity act = Database.currentActivity;
		act.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, false) {

						public void okClick() {
							Intent intent = new Intent();
							if (Database.GAME_TYPE == Constant.GAME_TYPE_DIZHU) { // 鏂楀湴涓?
								intent.setClass(act, DoudizhuRoomListActivity.class);
							}
							act.startActivity(intent);
							ActivityUtils.finishAcitivity();
						}
					};
					gameDialog.show();
					gameDialog.setText(msg);
				} catch (Exception e) {}
			}
		});
	}

	/**
	 * 閫€鍑烘父鎴?
	 * 
	 * @param context
	 * @param act
	 */
	public static void exitGame(final Context context) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity) {

						public void okClick() {
							CmdUtils.exitGame();
							MobclickAgent.onEvent(context, "纭閫€鍑烘父鎴忎腑");
							GameUser gu = (GameUser) GameCache.getObj(CacheKey.GAME_USER);
							// 璁板綍閫冭窇鏃ュ織
							if (gu != null) {
								MobclickAgent.onEvent(context, "閫冭窇:" + gu.getAccount());
								gu.setRound(0);
								GameCache.putObj(CacheKey.GAME_USER, gu);
							}
							ClientCmdMgr.closeClient();
							ActivityUtils.finishAcitivity();
						}

						public void cancelClick() {
							dismiss();
						};
					};
					gameDialog.show();
					// 瓒呭揩璧?
					if (1 == Database.JOIN_ROOM.getRoomType()) {
						gameDialog.setText("姣旇禌杩樺湪杩涜锛岄€€鍑烘瘮璧涘皢涓嶉€€杩樻姤鍚嶈垂锛岀‘瀹氶€€鍑猴紵");
					} else {
						gameDialog.setText("鏄惁杩斿洖娓告垙澶у巺锛?);
					}
				} catch (Exception e) {}
			}
		});
	}

	/**
	 * 鍔犺浇鎻愮ず
	 */
	public static ProgressDialog getWaitProgressDialog(Context context, String msg) {
		ProgressDialog dialog = new ProgressDialog(context, R.style.tsdialog);
		dialog.setMessage(msg);
		dialog.setProgress(ProgressDialog.STYLE_SPINNER);
		dialog.setCancelable(true);
		dialog.setIndeterminate(true);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.8f;// 閫忔槑搴?
		lp.dimAmount = 0.0f;// 榛戞殫搴?
		window.setAttributes(lp);
		return dialog;
	}
	/**
	 * 鍏呭€兼彁绀?
	 * @param context
	 * @param msg
	 * @return
	 */
	public static ProgressDialog getChargingProgressDialog(Context context, String msg) {
		ProgressDialog dialog = new ProgressDialog(context, R.style.tsdialog);
		dialog.setMessage(msg);
		dialog.setProgress(ProgressDialog.STYLE_SPINNER);
		dialog.setCancelable(true);
		dialog.setIndeterminate(true);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.8f;// 閫忔槑搴?
		lp.dimAmount = 0.0f;// 榛戞殫搴?
		window.setAttributes(lp);
		return dialog;
	}
	public static void quitGameTip() {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, false) {

						public void okClick() {
							ClientCmdMgr.closeClient();
							Intent intent = new Intent();
							intent.setClass(Database.currentActivity, DoudizhuRoomListActivity.class);
							Database.currentActivity.startActivity(intent);
							ActivityUtils.finishAcitivity();
						}
					};
					gameDialog.show();
					gameDialog.setText("鍏朵粬娓告垙鐜╁宸查€€鍑?璇烽€€鍑哄悗閲嶆柊杩涘叆");
				} catch (Exception e) {}
			}
		});
	}

	/**
	 * 鍏呭€煎け璐?
	 */
	public static void alipayFail(final String msg) {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity, false) {

						public void okClick() {
							Intent intent = new Intent();
							intent.setClass(Database.currentActivity, TaskMenuActivity.class);
							Database.currentActivity.startActivity(intent);
							ActivityUtils.finishAcitivity();
						}
					};
					gameDialog.show();
					gameDialog.setText(msg);
				} catch (Exception e) {}
			}
		});
	}

	/**
	 * 鍒ゆ柇缃戠粶鏄惁鍙敤
	 * 
	 * @param context
	 * @return
	 */
	public static GameDialog getNetWorkDialog() {
		GameDialog gameDialog = new GameDialog(Database.currentActivity) {

			public void okClick() {
				// Settings.ACTION_WIRELESS_SETTINGS 绉诲姩缃戠粶(2G/3G)
				Database.currentActivity.startActivity(new Intent(Settings.ACTION_SETTINGS)); // 杩涘叆鎵嬫満涓殑wifi缃戠粶璁剧疆鐣岄潰
			}

			public void cancelClick() {
				if (!Database.currentActivity.getClass().equals(LoginActivity.class)) {
					ActivityPool.exitApp();
				}
			}
		};
		gameDialog.show();
		gameDialog.setText("缃戠粶杩炴帴涓嶄笂,璇锋鏌ユ偍鐨勭綉缁滆缃?");
		gameDialog.setOkText("缃戠粶璁剧疆");
		gameDialog.setOkButtonBg(R.drawable.select1_btn_bg);
		gameDialog.setCancelText("鍙?娑?);
		gameDialog.setCancelButtonBg(R.drawable.select_btn_bg);
		return gameDialog;
	}
	
	
	/** 缃戠粶缂撴參鎻愮ず */
	public static void netSlowTip() {
		Database.currentActivity.runOnUiThread(new Runnable() {

			public void run() {
				try {
					GameDialog gameDialog = new GameDialog(Database.currentActivity) {

						public void okClick() {
							dismiss();
						}

						public void cancelClick() {
							dismiss();
							MobclickAgent.onEvent(Database.currentActivity, "缃戞參鎱㈣繘鍏ュ崟鏈烘父鎴?);
							Intent intent = new Intent();
							intent.setClass(Database.currentActivity, PersonnalDoudizhuActivity.class);
							Database.currentActivity.startActivity(intent);
						}
					};
					
					String msg = Database.currentActivity.getString(R.string.link_server_fail);
					gameDialog.show();
					gameDialog.setText(msg);
					gameDialog.setOkText("纭?  瀹?);
					gameDialog.setOkButtonBg(R.drawable.select_btn_bg);
					gameDialog.setCancelText("鍗曟満娓告垙");
					gameDialog.setCancelButtonBg(R.drawable.select1_btn_bg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}