package com.bonait.bnframework.HBL.Dialog;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.DataBus;

import java.lang.ref.WeakReference;

public class WaitDialog {

    private WeakReference<Activity> activityWeakReference;
    private WeakReference<Context> contextWeakReference;
    private ProgressDialog progressDialog;
    private static WaitDialog mInstance; //实例变量设置私有，防止直接通过类名访问

    private WaitDialog() {
        //默认构造函数私有，防止类外直接new创建对象
    }

    public static synchronized WaitDialog getInstance() { //静态同步方法作为唯一的实例对象获取方式
        if (mInstance==null) {
            synchronized (WaitDialog.class ){
                if(mInstance==null){
                    mInstance = new WaitDialog();
                }
            }
        }
        return mInstance;
    }
    //end

    public void Show(String title,String msg,Context _context,Activity _activity){
        contextWeakReference=new WeakReference<>(_context);
        activityWeakReference = new WeakReference<>(_activity);
       if(_context==null) return;
       if(progressDialog!=null){
           progressDialog.dismiss();
       }
        progressDialog = new ProgressDialog(_context);
        progressDialog.setTitle(title);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(msg);
        progressDialog.show();
    }

    public void AddText(String info){
        if(contextWeakReference.get()==null) return;
        if(activityWeakReference.get()==null) return;
        if(progressDialog==null) return;
        activityWeakReference.get().runOnUiThread(()->{ progressDialog.setMessage(info);});
    }

    public void Dismiss(){
        if(activityWeakReference.get()==null) return;
        if(progressDialog==null) return;
        activityWeakReference.get().runOnUiThread(()->{ progressDialog.dismiss();});
    }

    public void TimeOut(String info){
        if(activityWeakReference.get()==null) return;
        if(progressDialog==null) return;
        Dismiss();
        DialogManager.showError(activityWeakReference.get(),info,AlertDialogButton.OK,null);
    }


}
