package cn.net.caoyao.cpzshandset.ui.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;


public class DialogUtils {
	private static iCallBack callBack;

	/**
	 * 
	 * @author zwj<br/>
	 *         2015-4-20 下午10:52:47<br/>
	 * @param mContext
	 * @param title
	 * @param msg
	 * @param back
	 */
	public static void showCallBackDialog(Context mContext, String title,
			String msg, iCallBack back) {
		BaseCallBackDialog(mContext, title, msg, back);
	}

	/**
	 * 数据上传完成 <br/>
	 * 2015-5-21上午10:43:11
	 * 
	 * @param mContext
	 * @param back
	 */
	public static void showUploadOverDialog(Context mContext, iCallBack back) {
		showUploadOverDialog(mContext, "", back);
	}

	/**
	 * 数据上传完成 <br/>
	 * 2015-5-21上午10:43:11
	 * 
	 * @param mContext
	 * @param msg
	 *            数据追加(数据已经传输完成)
	 * @param back
	 */
	public static void showUploadOverDialog(Context mContext, String msg,
			iCallBack back) {
		if (null == msg || msg.trim().isEmpty()) {
			msg = "";
		} else {
			msg = "\n" + msg;
		}
		BaseCallBackDialog(mContext, "提示", "数据已经传输完成" + msg, back);
	}

//	/**
//	 * 升级数据对话框提示 <br/>
//	 * 2015-5-21上午9:46:33
//	 * 
//	 * @param mContext
//	 * @param back
//	 */
//	public static void showUploadDialog(Context mContext, iCallBack back) {
//		BaseCallBackDialog(mContext, "提示", "您要上传数据吗？", back);
//	}

	// /**
	// *
	// * @author zwj<br/>
	// * 2015-4-20 下午10:54:14<br/>
	// * @param mContext
	// * @param title
	// * @param msg
	// * @param back
	// */
/**
 * 升级数据对话框提示 
 * <br/>2015-5-21下午12:38:58
 * @param mContext
 * @param iUploadCount 需要上传的数据
 * @param back
 */
	public static void showUploadDialog(Context mContext, int iUploadCount,
			iCallBack back) {
		callBack = back;
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext,
				AlertDialog.THEME_HOLO_LIGHT);
		builder.setCancelable(false);
		if (iUploadCount > 0) {
			// 可以上传
			builder.setTitle("提示").setMessage(R.string.upload_tips);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							callBack.isRight(true);
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							callBack.isRight(false);
						}
					});
		} else {
			builder.setTitle("提示").setMessage(R.string.no_up_data);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							callBack.isRight(false);
						}
					});
		}
		builder.create().show();
	}

	public interface iCallBack {
		public void isRight(boolean b);
	}

	/**
	 * 
	 * @author zwj<br/>
	 *         2015-4-20 下午10:54:14<br/>
	 * @param mContext
	 * @param title
	 * @param msg
	 * @param back
	 */
	private static void BaseCallBackDialog(Context mContext, String title,
			String msg, iCallBack back) {
		if (null == title) {
			title = "提示";
		}
		callBack = back;
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext,
				AlertDialog.THEME_HOLO_LIGHT);
		builder.setTitle(title).setMessage(msg);
		builder.setCancelable(false);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callBack.isRight(true);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callBack.isRight(false);
			}
		});
		builder.create().show();
	}

	/**
	 * 就是展示信息使用 <br/>
	 * 2015-4-22下午12:34:21
	 * 
	 * @param mContext
	 * @param msg展示信息
	 */
	public static void ShowMsgDialog(Context mContext, String msg) {
		BaseShowMsgDialog(mContext, null, null, msg);
	}

	/**
	 * 防止一个对话框多次弹出的效果 <br/>
	 * 2015-4-22下午12:34:21
	 * 
	 * @param mContext
	 * @param msg展示信息
	 */
	public static AlertDialog ShowMsgRetAlertDialog(Context mContext, String msg) {
		return BaseShowMsgDialog(mContext, null, null, msg);
	}

	/**
	 * @author zwj<br/>
	 *         2015-4-20 下午10:54:14<br/>
	 * @param mContext
	 * @param title
	 * @param msg
	 * @param back
	 */
	private static AlertDialog BaseShowMsgDialog(Context mContext,
			String title, String positive, String msg) {
		if (null == title) {
			title = "提示";
		}
		if (null == positive) {
			positive = "确认";
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext,
				AlertDialog.THEME_HOLO_LIGHT);
		builder.setTitle(title).setMessage(msg);
		builder.setPositiveButton(positive,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
		return alertDialog;
	}

	/**
	 * listView上展示显示popupWindow，显示删除，点击删除当前选项 <br/>
	 * 2015-4-23下午1:25:19
	 * 
	 * @param v
	 * @param mActivity
	 * @param back
	 * @return
	 */
	public static void showDeletePopupWindow(View v, final Activity mActivity,
			final iCallBack back) {
		showDeletePopupWindow(v, mActivity, 0, back);
	}

	/**
	 * listView上展示显示popupWindow，显示删除，点击删除当前选项 <br/>
	 * <br/>
	 * 2015-5-14下午3:40:39
	 * 
	 * @param v
	 * @param mActivity
	 * @param padTop
	 *            距离上方多少距离
	 * @param back
	 */
	@SuppressWarnings("deprecation")
	public static void showDeletePopupWindow(View v, final Activity mActivity,
			int padTop, final iCallBack back) {
		LinearLayout layout = new LinearLayout(mActivity);
		layout.setGravity(Gravity.CENTER);
		layout.setBackgroundColor(Color.GRAY);
		layout.setBackgroundResource(R.drawable.bg_popupwindow_delete);
		TextView tv = new TextView(mActivity);
		tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		tv.setGravity(Gravity.CENTER_HORIZONTAL);
		tv.setPadding(0, 7 + padTop, 0, 0);
		tv.setTextSize(16);
		tv.setText("删除");
		tv.setTextColor(Color.WHITE);
		tv.setSingleLine(true);
		layout.addView(tv);
		int vWidth = 100;
		final PopupWindow popupWindow = new PopupWindow(layout, vWidth,
				v.getHeight());

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		int[] location = new int[2];
		v.getLocationOnScreen(location);

		DisplayMetrics dm = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;
		popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
				(screenWidth - vWidth) / 2,
				location[1] - popupWindow.getHeight());
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				back.isRight(true);
				Toast.makeText(mActivity, "删除成功", Toast.LENGTH_SHORT).show();
			}
		});

	}

	/**
	 * 设置网络 <br/>
	 * 2015-4-24下午3:13:49
	 * 
	 * @param mContext
	 */
	public static void showSetNetDialog(final Context mContext) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext,
				AlertDialog.THEME_HOLO_LIGHT);
		builder.setTitle("网络状态").setMessage("当前网络不可用，是否设置网络！");
		builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent("/");
				if (android.os.Build.VERSION.SDK_INT > 10) {
					intent = new Intent(
							android.provider.Settings.ACTION_SETTINGS);
				} else {
					intent = new Intent();
					ComponentName component = new ComponentName(
							"com.android.settings",
							"com.android.settings.Settings");
					intent.setComponent(component);
					intent.setAction("android.intent.action.VIEW");
				}
				mContext.startActivity(intent);
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	/**
	 * 
	 * @author zwj<br/>
	 *         2015-4-26 下午4:46:39<br/>
	 * @param mProgressDialog
	 * @param title
	 * @param msg
	 * @param context
	 * @return
	 */
	public static ProgressDialog showProgressDialog(
			ProgressDialog mProgressDialog, String title, String msg,
			Context context) {
		if (mProgressDialog != null) {
			if (mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
			}
			mProgressDialog = null;
		}
		mProgressDialog = new ProgressDialog(context,
				ProgressDialog.THEME_HOLO_LIGHT);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setTitle(title);
		mProgressDialog.setCanceledOnTouchOutside(false);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
		return mProgressDialog;
	}

	/**
	 * 带进度的 <br/>
	 * 2015-4-3上午10:14:46
	 * 
	 * @param dialog
	 * @param title
	 * @param msg
	 * @param context
	 * @param max
	 *            总共要上传的数量
	 * @return
	 */
	public static ProgressDialog showProgressHorizontalDialog(
			ProgressDialog dialog, String title, String msg, Context context,
			int max) {
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
			dialog = null;
		}
		dialog = new ProgressDialog(context,
				AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setCancelable(false);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setMax(max);
		dialog.setTitle(title);
		dialog.setMessage(msg);
		dialog.show();
		return dialog;
	}

	/**
	 * 取消等待条 2014-6-19上午10:18:24
	 * 
	 * @param mProgressDialog
	 * @param context
	 */
	public static void dismissProgressDialog(ProgressDialog mProgressDialog) {
		if (null != mProgressDialog && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}
}
