package com.gh.common.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.gh.gamecenter.R;

public class DialogUtils {

	public static Dialog showWaitDialog(Context context, String msg) {
		Dialog dialog = new Dialog(context);
		View view = View.inflate(context, R.layout.set_wait_dialog, null);
		TextView message = (TextView) view.findViewById(R.id.set_wait_message);
		message.setText(msg);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}

	private static boolean isShow = false;

	public static void showWarningDialog(Context context, String title, CharSequence msg, String cancel, String confirm,
	                                     final ConfiremListener cmListener, final CancelListener clListener) {

		if (isShow) {
			return;
		}
		isShow = true;

		final Dialog dialog = new Dialog(context);

		View view = View.inflate(context, R.layout.common_alertdialog, null);

		// 标题
		TextView alertdialog_title = (TextView) view.findViewById(R.id.alertdialog_title);
		alertdialog_title.setText(title);

		// 内容
		TextView alertdialog_content = (TextView) view.findViewById(R.id.alertdialog_content);
		alertdialog_content.setText(msg);

		// 取消按钮
		TextView alertdialog_cannel = (TextView) view.findViewById(R.id.alertdialog_cannel);
		alertdialog_cannel.setText(cancel);
		alertdialog_cannel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (clListener != null) {
					clListener.onCancel();
				}
			}
		});

		// 确定按钮
		TextView alertdialog_confirm = (TextView) view.findViewById(R.id.alertdialog_confirm);
		alertdialog_confirm.setText(confirm);
		alertdialog_confirm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (cmListener != null) {
					cmListener.onConfirem();
				}
			}
		});

		dialog.setOnDismissListener(new Dialog.OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				isShow = false;
			}
		});

		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(view);
		dialog.show();
	}

	public static void showHijackDialog(final Context context) {
		showWarningDialog(context, "警告", "您当前网络环境异常，下载地址已被替换（网络劫持），请更换网络环境进行下载。",
				new ConfiremListener() {
					@Override
					public void onConfirem() {
						// 跳转wifi管理界面
						Intent intent = new Intent("android.settings.WIFI_SETTINGS");
						context.startActivity(intent);
					}
				});
	}

	public static void showWarningDialog(Context context, String title, CharSequence msg, final ConfiremListener listener) {
		showWarningDialog(context, title, msg, "取消", "确定", listener, null);
	}

	public static void showDownloadDialog(Context context, ConfiremListener listener) {
		showWarningDialog(context, "下载提示", "您当前使用的网络为2G/3G/4G，开始下载将会消耗移动流量，确定下载？", listener);
	}

	public static void showCancelDialog(Context context, final ConfiremListener listener) {
		Spanned content = Html.fromHtml("取消关注游戏后，您将无法及时收到游戏" +
				"<font color='#ff0000'>攻略</font>、" +
				"<font color='#ff0000'>资讯</font>等最新动态提醒。");
		showWarningDialog(context, "取消关注", content, "暂不取消", "确定取消", listener, null);
	}

	public static void showPluginDialog(Context context, final ConfiremListener listener) {
		Spanned spanned = Html.fromHtml("您将进行插件化安装以实现插件功能,此过程将"
				+ "<font color=\"#ff0000\">卸载</font>" + "当前使用的版本并"
				+ "<font color=\"#ff0000\">安装插件版本</font>" + "。");
		showWarningDialog(context, "插件化安装", spanned, listener);
	}

	public static void showDisclaimerDialog(Context context, String content) {
		final Dialog disclaimerDialog = new Dialog(context);
		View view = View.inflate(context, R.layout.dialog_disclaimer, null);

//		TextView title = (TextView) view.findViewById(R.id.disclaimer_title);
//		title.setText("免责声明");

		TextView message = (TextView) view.findViewById(R.id.disclaimer_message);
		Spanned spanned = Html.fromHtml(content);
		message.setText(spanned);

		view.findViewById(R.id.disclaimer_confirm).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						disclaimerDialog.dismiss();
					}
				});

		disclaimerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		disclaimerDialog.setContentView(view);
		disclaimerDialog.show();
	}

	public interface ConfiremListener{
		void onConfirem();
	}

	public interface CancelListener{
		void onCancel();
	}

}
