package com.handset.serialportsensor.activity;

import android.app.AlertDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.handset.serialportlib.CRCUtils;

public abstract class BaseActivity extends AppCompatActivity {
    private static final String logTag = "HANDSET-JE002-EX";

    private boolean mResume = false;
    boolean mNeedToDismissProgressDlg = false;
    boolean mNeedToShowProgressDlg = false;
    AlertDialog mLoadingDialog;

    @Override
    protected void onResume() {
        super.onResume();
        this.mResume = true;
        checkProgress();
    }

    @Override
    protected void onPause() {
        this.mResume = false;
        super.onPause();
    }

    protected String genCmd(String sensorNo, int cmdId) {
        try {
            String content = sensorNo.concat("00" + Integer.toHexString(cmdId) + "00");
            return "DDDD" + content + CRCUtils.genCrc(content);
        } catch (Exception e) {
            Log.e(logTag, e.getMessage());
            return null;
        }
    }

    public void showDialog(final AlertDialog loadingDialog) {
        if (loadingDialog == null) {
            return;
        }
        mLoadingDialog = loadingDialog;
        if (mResume) {
            mLoadingDialog.show();
            mNeedToShowProgressDlg = false;
        } else {
            mNeedToShowProgressDlg = true;
        }
    }

    private void checkProgress() {
        if (mLoadingDialog != null && mResume) {
            if (mNeedToShowProgressDlg) {
                mLoadingDialog.show();
                mNeedToShowProgressDlg = false;
            }
            if (mNeedToDismissProgressDlg) {
                mLoadingDialog.dismiss();
                mNeedToDismissProgressDlg = false;
                mLoadingDialog = null;
            }
        }
    }

    public void dismissDialog(final AlertDialog loadingDialog) {
        if (loadingDialog == null && mLoadingDialog == null) {
            return;
        }
        if (mResume) {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            } else if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }

            mNeedToDismissProgressDlg = false;
            mLoadingDialog = null;
        } else {
            mLoadingDialog = loadingDialog;
            mNeedToDismissProgressDlg = true;
        }
    }

    public void disMissDialogDelay(final AlertDialog alertDialog, int delayMillis) {
        new Handler().postDelayed(() -> dismissDialogD(alertDialog), delayMillis);
    }


    protected void dismissDialogD(final AlertDialog alertDialog) {
        if (mLoadingDialog == null && alertDialog == null) {
            return;
        }

        if (mLoadingDialog != null && alertDialog != null && mLoadingDialog == alertDialog) {
            dismissDialog(alertDialog);
            return;
        }

        if (mLoadingDialog == null) {
            if (mResume && alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
        }

    }


    public void disMissLoadingDialog(final AlertDialog loadingDialog) {
        runOnUiThread(() -> {
            /*if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }*/
            dismissDialog(loadingDialog);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
