package com.example.myapplication.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.R;


public class AlertDialogHelper
{
    private AlertDialog.Builder builder;
    private Context mContext;

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with message
     * @param       context - Application context
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context)
    {
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
    }//End AlertDialogHelper

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with message
     * @param       context - Application context
     *              dialogMsg - Dialog Message
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context, @Nullable String dialogMsg)
    {
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
        this.setDialogMsg(dialogMsg);
        this.setDialogIsCancelable(true);
        this.setDialogButtonDefault(this.mContext.getString(R.string.BTN_DONE),null);
    }//End AlertDialogHelper

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with message and title
     * @param       context - Application context
     *              dialogMsg - Dialog Message
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context, int iconResId, @Nullable String dialogTitle, @Nullable String dialogMsg)
    {
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
        if(iconResId > 0)
        {
            this.setDialogIcon(iconResId);
            if(dialogTitle == null)
            {
                dialogTitle = " ";
            }
        }//End if

        this.setDialogTitle(dialogTitle);
        this.setDialogMsg(dialogMsg);
        this.setDialogIsCancelable(true);

        this.setDialogButtonDefault(this.mContext.getString(R.string.BTN_DONE),null);
    }//End AlertDialogHelper

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with message and title
     * @param       context - Application context
     *              dialogMsg - Dialog Message
     *              onClickListener - Event handler for buttons
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context, int iconResId, @Nullable String dialogTitle, @Nullable String dialogMsg, DialogInterface.OnClickListener onClickListener)
    {
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
        if(iconResId > 0)
        {
            this.setDialogIcon(iconResId);
            if(dialogTitle == null)
            {
                dialogTitle = " ";
            }
        }//End if

        this.setDialogTitle(dialogTitle);
        this.setDialogMsg(dialogMsg);
        this.setDialogIsCancelable(true);
        this.setDialogButtons(this.mContext.getString(R.string.BTN_DONE),null,onClickListener);
    }//End AlertDialogHelper

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with title, message, and buttons
     * @param       context - Application context
     *              dialogTitle - Dialog title
     *              dialogMsg - Dialog Message
     *              iconResId - Resources id pointed to a icon
     *              postiveMsg - Message shown on the positive button
     *              negativeMsg - Message shown on the negative button
    *               onClickListener - Event handler for buttons
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context, String dialogTitle, @Nullable String dialogMsg, @Nullable int iconResId, @NonNull String postiveMsg, @NonNull String negativeMsg, @NonNull DialogInterface.OnClickListener onClickListener)
    {
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
        this.setDialogIcon(iconResId);
        this.setDialogTitle(dialogTitle);
        this.setDialogMsg(dialogMsg);
        this.setDialogButtons(postiveMsg,negativeMsg,onClickListener);
    }//End AlertDialogHelper

    /**
     * AlertDialogHelper
     * @brief       Constructor to create a default Alert dialog with title, message, and buttons
     * @param       context - Application context
     *              dialogTitle - Dialog title
     *              dialogMsg - Dialog Message
     *              iconResId - Resources id pointed to a icon
     *              postiveMsg - Message shown on the positive button
     *              negativeMsg - Message shown on the negative button\
     * @exception   None
     * @return      None
     */
    public AlertDialogHelper(@NonNull Context context, String dialogTitle, @Nullable String dialogMsg, @Nullable int iconResId, @Nullable String postiveMsg, @Nullable String negativeMsg)
    {
        AlertDialogDefaultOnClickListener defaultClickListener = new AlertDialogDefaultOnClickListener();
        this.mContext = context;
        this.builder = new AlertDialog.Builder(this.mContext);
        this.setDialogIcon(iconResId);
        this.setDialogTitle(dialogTitle);
        this.setDialogMsg(dialogMsg);
        this.setDialogButtons(postiveMsg,negativeMsg,defaultClickListener);
    }//End AlertDialogHelper


    /**
     * setDialogIcon
     * @brief       Set if the dialog's icon
     * @param       iconResId - Alert dialog message
     * @exception   None
     * @return      None
     */
    public void setDialogIcon(int iconResId)
    {
        this.builder.setIcon(iconResId);
    }

    /**
     * setDialogIsCancelable
     * @brief       Set if the dialog is cancelable
     * @param       isCancelable - True
     * @exception   None
     * @return      None
     */
    public void setDialogIsCancelable(boolean isCancelable)
    {
        this.builder.setCancelable(isCancelable);
    }

    /**
     * setDialogMsg
     * @brief       Set alert dialog message
     * @param       dialogMsg - Alert dialog message
     * @exception   None
     * @return      None
     */
    public void setDialogMsg(String dialogMsg)
    {
        this.builder.setMessage(dialogMsg);
    }//End setDialogMsg

    /**
     * setDialogTitle
     * @brief       Set alert dialog title
     * @param       title - Alert dialog title
     * @exception   None
     * @return      None
     */
    public void setDialogTitle(String dialogTitle)
    {
        this.builder.setTitle(dialogTitle);
    }//End setDialogTitle

    /**
     * setDialogButtons
     * @brief       Set alert dialog buttons
     * @param       postiveMsg - Message shown on the positive button
     *              negativeMsg - Message shown on the negative button
     *              onClickListener - Event handler
     * @exception   None
     * @return      None
     */
    public void setDialogButtons(@Nullable String postiveMsg,@Nullable String negativeMsg, DialogInterface.OnClickListener onClickListener)
    {
        if(postiveMsg == null && negativeMsg == null)
        {
            return;
        }

        if(postiveMsg != null)
        {
            this.builder.setPositiveButton(postiveMsg, onClickListener);
        }

        if(negativeMsg != null)
        {
            this.builder.setNegativeButton(negativeMsg, onClickListener);
        }
    }//End setDialogButtons

    /**
     * setDialogFeatures
     * @brief       Set alert dialog features
     * @param       iconResId - Resources id pointed to a icon
     *              postiveMsg - Message shown on the positive button
     *              negativeMsg - Message shown on the negative button
     * @exception   None
     * @return      None
     */
    public void setDialogButtonDefault(@Nullable String postiveMsg, @Nullable String negativeMsg)
    {
        AlertDialogDefaultOnClickListener defaultClickListener = new AlertDialogDefaultOnClickListener();
        this.setDialogButtons(postiveMsg,negativeMsg,defaultClickListener);
    }

    /**
     * show
     * @brief       Show alert dialog
     * @param       None
     * @exception   None
     * @return      None
     */
    public void show()
    {
        if(this.builder == null)
        {
            return;
        }
        AlertDialog alert = this.builder.create();
        alert.show();
    }


    private class AlertDialogDefaultOnClickListener implements DialogInterface.OnClickListener
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            switch (which)
            {
                case DialogInterface.BUTTON_POSITIVE:
                    dialog.dismiss();
                break;
                case DialogInterface.BUTTON_NEGATIVE:
                    dialog.cancel();
                break;
            }//End switch
        }//End onClick
    };
}//AlertDialogHelper
