/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.view.dialog;

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

/**
 * <pre>
 * 1. ClassName :   CustomDialog
 * 2. FileName  :   CustomDialog.java
 * 3. Package   :   com.ntels.onecable.view.dialog
 * 4. Date      :   2016-12-05
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class CustomDialog {

    private static final String TAG = CustomDialog.class.getSimpleName();
    private OnMsgAlterToResult _msg_result_event;

    public CustomDialog() {

    }

    /**
     * message box dialog.
     *
     * @param ctx    컨텍스트
     * @param iMsgNo Error message constant
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, int)
     * @see #msgAlert(Context, int, int, String, String)
     * @see #msgAlert(Context, int, String)
     * @see #msgAlert(Context, int, String, int)
     * @see #msgAlert(Context, int, String, int, String, String)
     */
    public void msgAlert(Context ctx, final int iMsgNo) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            dialog_builder.setPositiveButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * message box dialog.
     *
     * @param ctx    컨텍스트
     * @param iMsgNo Error message constant
     * @param sMsg   Dialog message
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, int)
     * @see #msgAlert(Context, int, String, int, String, String)
     * @see #msgAlert(Context, int, int, String, String)
     * @see #msgAlert(Context, int)
     * @see #msgAlert(Context, int, String, int)
     */
    public void msgAlert(Context ctx, final int iMsgNo, String sMsg) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            if (!sMsg.equals("")) {
                dialog_builder.setMessage(sMsg);
            }
            dialog_builder.setPositiveButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * message box dialog.
     *
     * @param ctx       컨텍스트
     * @param iMsgNo    Error message constant
     * @param iMsgTitle Dialog title resource id
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, String)
     * @see #msgAlert(Context, int, String, int, String, String)
     * @see #msgAlert(Context, int, int, String, String)
     * @see #msgAlert(Context, int)
     * @see #msgAlert(Context, int, String, int)
     */
    public void msgAlert(Context ctx, final int iMsgNo, int iMsgTitle) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            dialog_builder.setTitle(iMsgTitle);
            dialog_builder.setPositiveButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * message box dialog.
     *
     * @param ctx       컨텍스트
     * @param iMsgNo    Error message constant
     * @param sMsg      Dialog message string
     * @param iMsgTitle Dialog title resource id
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, String)
     * @see #msgAlert(Context, int, String, int, String, String)
     * @see #msgAlert(Context, int, int, String, String)
     * @see #msgAlert(Context, int)
     * @see #msgAlert(Context, int, int)
     */
    public void msgAlert(Context ctx, final int iMsgNo, String sMsg, int iMsgTitle) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            if (!sMsg.equals("")) {
                dialog_builder.setMessage(sMsg);
            }
            dialog_builder.setTitle(iMsgTitle);
            dialog_builder.setPositiveButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * message box dialog.
     *
     * @param ctx 컨텍스트
     * @param iMsgNo Error message constant
     * @param iMsgTitle Dialog title resource id
     * @param sPositiveText Positive button text
     * @param sNegativeText Negative button text
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, String)
     * @see #msgAlert(Context, int, String, int, String, String)
     * @see #msgAlert(Context, int, String, int)
     * @see #msgAlert(Context, int)
     * @see #msgAlert(Context, int, int)
     */
    public void msgAlert(Context ctx, final int iMsgNo, int iMsgTitle, String sPositiveText, String sNegativeText) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            dialog_builder.setTitle(iMsgTitle);

            dialog_builder.setPositiveButton(sPositiveText, new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(sNegativeText, new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * message box dialog.
     *
     * @param ctx 컨텍스트
     * @param iMsgNo Error message constant
     * @param sMsg Dialog message string
     * @param iMsgTitle Dialog title resource id
     * @param sPositiveText Positive button text
     * @param sNegativeText Negative button text
     * @see com.ntels.onecable.common.Constants
     * @see #msgAlert(Context, int, String)
     * @see #msgAlert(Context, int, int, String, String)
     * @see #msgAlert(Context, int, String, int)
     * @see #msgAlert(Context, int)
     * @see #msgAlert(Context, int, int)
     */
    public void msgAlert(Context ctx, final int iMsgNo, String sMsg, int iMsgTitle, String sPositiveText, String sNegativeText) {

        try {
            DialogCustom.Builder dialog_builder;
            dialog_builder = new DialogCustom.Builder(ctx);
            if (iMsgNo != 0) {
                dialog_builder.setErrNo("MSG" + iMsgNo);
            }
            if (!sMsg.equals("")) {
                dialog_builder.setMessage(sMsg);
            }
            dialog_builder.setTitle(iMsgTitle);

            dialog_builder.setPositiveButton(sPositiveText, new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 1);
                }
            });
            dialog_builder.setNegativeButton(sNegativeText, new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });

            dialog_builder.setOnCancelListener(new AlertDialog.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                    dialog.dismiss();
                    _msg_result_event.resultCustomDialogResponse(iMsgNo, 2);
                }
            });
            dialog_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Dialog button click listener interface.
     */
    public interface OnMsgAlterToResult {

        /**
         * Dialog click listener method.
         *
         * @param iMsgID Message constant
         * @param iBtnResult Selected button index (Positive - 1, Negative - 2)
         */
        void resultCustomDialogResponse(int iMsgID, int iBtnResult);
    }

    /**
     * {@link #_msg_result_event} setter.
     *
     * @param _event Dialog button click listener
     */
    public void setOnMsgAlterToResultEvent(OnMsgAlterToResult _event) {

        if (_event != null) {
            this._msg_result_event = _event;
        }
    }
}
