package cn.net.caoyao.cpzshandset.common.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;

public class DialogUtil {

	/**
	 * 无标题的进度条对话框
	 * 
	 * @param context
	 *            上下文对象
	 * @param msg
	 *            提示内容
	 * @return ProgressDialog 对象
	 */
	public static ProgressDialog showProgressDialog(Context context, String msg) {
		ProgressDialog mDialog = new ProgressDialog(context);
		mDialog.setMessage(msg);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();
		return mDialog;
	}

	/**
	 * 带标题的进度条对话框
	 * 
	 * @param context
	 *            上下文对象
	 * @param title
	 *            标题，没有时可置为null
	 * @param msg
	 *            提示内容
	 * @return ProgressDialog 对象
	 */
	public static ProgressDialog showProgressDialog(Context context, String title, String msg) {
		ProgressDialog mDialog = new ProgressDialog(context);
		if (title != null) {
			mDialog.setTitle(title);
		}
		mDialog.setMessage(msg);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();
		return mDialog;
	}

	/**
	 * 对话框（确认，取消）
	 * 
	 * @param context
	 *            上下文对象
	 * @param title
	 *            标题,没有时置为null
	 * @param msg
	 *            提示内容
	 * @param onClickListener
	 *            该方法需要实现的OnClickListener接口，注意此处用的是PositiveButton和NegativeButton
	 * @return AlertDialog 对象
	 */
	public static AlertDialog showDialog(Context context, String title, String msg, OnClickListener onClickListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(msg);
		builder.setPositiveButton("确认", onClickListener);
		builder.setNegativeButton("取消", onClickListener);
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}

	/**
	 * 对话框
	 * 
	 * @param context
	 *            上下文对象
	 * @param title
	 *            标题,没有时置为null
	 * @param msg
	 *            提示内容
	 * @param btnOkName
	 *            确定按钮名
	 * @param btnCancleName
	 *            取消按钮名
	 * @param onClickListener
	 *            该方法需要实现的OnClickListener接口，注意此处用的是PositiveButton和NegativeButton
	 * @return
	 */
	public static AlertDialog showDialog(Context context, String title, String msg, String btnOkName,
			String btnCancleName, OnClickListener onClickListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(msg);
		builder.setPositiveButton(btnOkName, onClickListener);
		builder.setNegativeButton(btnCancleName, onClickListener);
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}

	/**
	 * 带图标的对话框（确认，取消）
	 * 
	 * @param context
	 *            上下文对象
	 * @param iconResID
	 *            图标的资源ID,没有时置为-1
	 * @param title
	 *            标题,没有时置为null
	 * @param msg
	 *            提示内容
	 * @param onClickListener
	 *            该方法需要实现的OnClickListener接口，注意此处用的是PositiveButton和NegativeButton
	 * @return AlertDialog 对象
	 */
	public static AlertDialog showDialog(Context context, int iconResID, String title, String msg,
			OnClickListener onClickListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		if (iconResID != -1) {
			builder.setIcon(iconResID);
		}
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(msg);
		builder.setPositiveButton("确认", onClickListener);
		builder.setNegativeButton("取消", onClickListener);
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}

	public interface DialogClickListener {
		void onClickListener(View v, Dialog dialog);
	};

	/**
	 * 自定义进度条对话框
	 * 
	 * @param context
	 * @param msg
	 * @return
	 */
	public static Dialog getLoadingDialog(Context context, String msg) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.clear_cache_loading_dialog, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		// main.xml中的ImageView
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
		// 加载动画
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.clear_cache_loading_animation);
		// 使用ImageView显示动画
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		if (!TextUtils.isEmpty(msg)) {
			tipTextView.setText(msg);// 设置加载信息
		}
		Dialog loadingDialog = new Dialog(context, R.style.clear_cache_loading_dialog_style);// 创建自定义样式dialog

		loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));// 设置布局

		ScreenManager sm = new ScreenManager(context);
		// 使之padding
		// sm.FrameLayoutParams(layout, wight, hight, topMargin, leftMargin,
		// rightMargin, bottomMargin);
		sm.LinearLayoutParams(tipTextView, 0, 0, 10, 10, 0, 0);
		tipTextView.setTextSize(sm.setTextSize(16));
		return loadingDialog;

	}
}
