package com.secspace.lib.common.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.secspace.lib.common.R;


/**
 * Created by SKR-CYW on 2017/11/22.
 */

public class SimpleDialog implements DialogInterface.OnDismissListener {
    private Activity mAct;
    private AlertDialog mDialog;

    public SimpleDialog(Activity activity) {
        this.mAct = activity;
    }

    public SimpleDialog show(String title, String msg, boolean cancelable, DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mAct);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setCancelable(cancelable);
        if (positive != null)
            builder.setPositiveButton(R.string.common_sure, positive);
        if (negative != null)
            builder.setNegativeButton(R.string.common_cancel, negative);
        builder.setOnDismissListener(this);
        mDialog = builder.create();
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        return this;
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mAct = null;
        mDialog = null;
    }
}
