package com.zztrapp.zztrapp.http;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.ui.MainActivity;


/**
 * Describe Dialog处理Handle
 * Created by Bby on 2021/10/2
 * Email bby0856@163.com
 */

public class ProgressDialogHandler extends Handler {

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

    private Dialog pd;

    private Context context;
    private boolean cancelable;
    private IProgressCancelListener mProgressCancelListener;
    private String tvMsg;
    private final Activity mActivity;

    public ProgressDialogHandler(Context context, IProgressCancelListener mProgressCancelListener,
                                 boolean cancelable) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
        mActivity = (Activity) this.context;
        tvMsg = context.getString(R.string.loading);
    }

    public void setTvMsg(String tvMsg) {
        this.tvMsg = tvMsg;
    }

    private void initProgressDialog() {
        if (pd == null) {
            pd = new Dialog(context, R.style.LoadingDialogStyle);
            pd.setContentView(R.layout.layout_dialog);
            pd.setCancelable(true);
            pd.setCanceledOnTouchOutside(false);
            pd.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            TextView msg = pd.findViewById(R.id.id_tv_loadingmsg);
            msg.setText(tvMsg);
            msg.setVisibility(View.GONE);
            pd.setCancelable(cancelable);

            pd.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                    KLog.e("//此处把dialog dismiss掉，然后把本身的activity finish掉11");
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        dialog.dismiss();
                        if (mActivity != MainActivity.mainActivity) {
                            if (mActivity != null) {
                                mProgressCancelListener.onCancelProgress();
                                mActivity.finish();
                            }
                        }
                        return true;
                    } else {
                        return false;
                    }
                }
            });


            if (cancelable) {
                pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }
            if (!pd.isShowing() && cancelable) {
                pd.show();
            }

        }
    }

    private void dismissProgressDialog() {
        if (pd != null && !mActivity.isFinishing()) {
            pd.dismiss();
            pd = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }

}
