package com.example.fingerprint_tlk1_uart_demo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

import androidx.annotation.NonNull;

public class WaitDialog {
    private Activity activity=null;
    private ProgressDialog progressDialog=null;
    private int durationTime=0;
    private boolean isWait=false;

    Handler handler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            int w= msg.what;
            switch (msg.what){
                case 0:
                    if(progressDialog!=null){
                        progressDialog.cancel();
                        progressDialog=null;
                    }
                    break;
                case 1:
                    if(progressDialog!=null){
                        progressDialog.setMessage(msg.obj.toString());
                    }
                    break;
            }
        }
    };

    public WaitDialog(Activity activity){
       this.activity=activity;
    }


    /**
     * @param msg 显示的消息
     * @param durationTime 等待条显示时间 单位：毫秒
     */
    public void show(String msg,int durationTime){
        if(activity==null){
            throw new NullPointerException("activity is null");
        }
        if(progressDialog==null) {
            isWait=true;
            this.durationTime=durationTime;
            progressDialog = new ProgressDialog(activity);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(msg);
            progressDialog.show();
            new WaitThread().start();
        }
    }
    public void show(String msg){
        if(activity==null){
            throw new NullPointerException("activity is null");
        }
        if(progressDialog==null) {
            isWait=true;
            this.durationTime=1000000;
            progressDialog = new ProgressDialog(activity);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(msg);
            progressDialog.show();
            new WaitThread().start();
        }
    }
    public void hide(){
        handler.sendEmptyMessage(0);
        isWait=false;
    }
    public void setMsg(String msg){
        Message message=new Message();
        message.what=1;
        message.obj=msg;
        handler.sendMessage(message);
    }

    class WaitThread extends Thread{
        public void run(){
            long startTime=SystemClock.uptimeMillis();
            while (isWait){
                if((SystemClock.uptimeMillis()-startTime)>durationTime){
                    hide();
                    return;
                }
                SystemClock.sleep(10);
            }

        }
    }


}
