package com.ygtx.emcs.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

import java.util.concurrent.ExecutionException;

/**
 * 等待对话框
 * @author ZW
 *
 */
public class MyProgress {
	private static MyProgress instance;

	private MyProgress() {
		// 构造函数私有化，外部无法直接实例化
	}
	public static MyProgress getInstance() {
		if (instance == null) {
			instance = new MyProgress();
		}
		return instance;
	}
	/**
	 * 定义一个全局等待对话框的对象，控制一个实例只能操作一个等待对话框
	 */
	private ProgressDialog mProgressDialog = null;
	private Boolean is_show;
	
	/**
	 * 等待对话框显示
	 * @param context
	 * @param title 标题
	 * @param msg 内容
	 */
	public void show(Context context, String title, String msg){
		try {
			if (mProgressDialog == null) {
				mProgressDialog = new ProgressDialog(context);
			}
			System.out.println("mProgressDialog-show===");
			System.out.println(mProgressDialog);
			is_show = true;
			mProgressDialog.setTitle(title);
			mProgressDialog.setMessage(msg);
			mProgressDialog.setCanceledOnTouchOutside(false);    // 设置点击对话框后不消失
			mProgressDialog.show();
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}

	public void show(Context context, String title, int resid){
		if(mProgressDialog == null){
			mProgressDialog = new ProgressDialog(context);
		}
		is_show = true;
		mProgressDialog.setTitle(title);
		mProgressDialog.setMessage(context.getResources().getString(resid));
		mProgressDialog.setCanceledOnTouchOutside(false);	// 设置点击对话框后不消失
		mProgressDialog.show();
	}

	/**
	 * 等待对话框关闭
	 */
	public void close(){
		if(mProgressDialog != null){
			System.out.println("mProgressDialog-close===");
			System.out.println(mProgressDialog);
			mProgressDialog.hide();
			mProgressDialog.dismiss();
			is_show = false;
			mProgressDialog = null;
		}
	}

	/**
	 * 等待对话框关闭
	 */
	public void close(Activity ctx){
		ctx.runOnUiThread(new Runnable() {
			public void run() {
				if(mProgressDialog != null){
					mProgressDialog.hide();
					mProgressDialog.dismiss();
					is_show = false;
					mProgressDialog = null;
				}
			}
		});

	}

	public Boolean getIs_show() {
		if(mProgressDialog == null){
			return false;
		}
		return mProgressDialog.isShowing();
	}
}
