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

package com.ntels.onecable.admin.view.dialog;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.util.Util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

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

    /**
     * 생성자.
     *
     * @param context 컨텍스트
     * @param theme   Theme resource id
     */
    public DialogCustom(Context context, int theme) {

        super(context, theme);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams
                .SOFT_INPUT_ADJUST_RESIZE);
    }

    /**
     * 생성자.
     *
     * @param context 컨텍스트
     */
    public DialogCustom(Context context) {

        super(context);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams
                .SOFT_INPUT_ADJUST_RESIZE);
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {

        private Util mUtil = new Util();
        private CustomDialog customDialog = new CustomDialog();
        private Context context;
        private String title;
        //iDialogType 1= 일반, 2=push
        private int iDialogType = 1;
        private String msgno;
        private String message;
        private Boolean bCancelable = true;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private View bodyView = null;

        private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;
        private DialogInterface.OnCancelListener OnCancelClickListener;

        /**
         * 생성자.
         *
         * @param context 컨텍스트
         */
        public Builder(Context context) {

            this.context = context;
        }

        /**
         * {@link #bCancelable} setter.
         *
         * @param bStr 캔슬 가능 여부
         *
         * @return Builder instance
         */
        public Builder setCancelable(Boolean bStr) {

            this.bCancelable = bStr;
            return this;
        }

        /**
         * {@link #message} setter.
         *
         * @param message Dialog message string
         *
         * @return Builder instance
         *
         * @see #setMessage(int)
         */
        public Builder setMessage(String message) {

            this.message = message;
            return this;
        }

        /**
         * {@link #message} setter.
         *
         * @param message Dialog message string resource id
         *
         * @return Builder instance
         *
         * @see #setMessage(String)
         */
        public Builder setMessage(int message) {

            this.message = context.getText(message).toString();
            return this;
        }

        /**
         * {@link #title} setter.
         *
         * @param title Dialog title resource id
         *
         * @return Builder instance
         *
         * @see #setTitle(String)
         */
        public Builder setTitle(int title) {

            this.title = context.getText(title).toString();
            return this;
        }

        /**
         * {@link #title} setter.
         *
         * @param title Dialog title string
         *
         * @return Builder instance
         *
         * @see #setTitle(int)
         */
        public Builder setTitle(String title) {

            this.title = title;
            return this;
        }

        /**
         * {@link #iDialogType} setter.
         *
         * @param iDialogType Dialog type
         *
         * @return Builder instance
         */
        public Builder setDialogType(int iDialogType) {

            this.iDialogType = iDialogType;
            return this;
        }

        /**
         * {@link #msgno} setter.
         *
         * @param msgno Error message number
         *
         * @return Builder instance
         */
        public Builder setErrNo(String msgno) {

            this.msgno = msgno;
            return this;
        }

        /**
         * {@link #contentView} setter.
         *
         * @param v Displayed view in Dialog
         *
         * @return Builder instance
         */
        public Builder setContentView(View v) {

            this.contentView = v;
            return this;
        }

        /**
         * Dialog positive button setting.
         *
         * @param positiveButtonText Button text resource id
         * @param listener           Click listener
         *
         * @return Builder instance
         *
         * @see #setPositiveButton(String, OnClickListener)
         * @see #setPositiveButton(OnClickListener)
         */
        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {

            this.positiveButtonText = context.getText(positiveButtonText).toString();
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog positive button setting.
         *
         * @param positiveButtonText Button text string
         * @param listener           Click listener
         *
         * @return Builder instance
         *
         * @see #setPositiveButton(int, OnClickListener)
         * @see #setPositiveButton(OnClickListener)
         */
        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {

            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog positive button setting.
         *
         * @param listener Click listener
         *
         * @return Builder instance
         *
         * @see #setPositiveButton(int, OnClickListener)
         * @see #setPositiveButton(String, OnClickListener)
         */
        public Builder setPositiveButton(DialogInterface.OnClickListener listener) {

            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog negative button setting.
         *
         * @param negativeButtonText Button text resource id
         * @param listener           Click listener
         *
         * @return Builder instance
         *
         * @see #setNegativeButton(String, OnClickListener)
         * @see #setNegativeButton(OnClickListener)
         */
        public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {

            this.negativeButtonText = context.getText(negativeButtonText).toString();
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog negative button setting.
         *
         * @param negativeButtonText Button text string
         * @param listener           Click listener
         *
         * @return Builder instance
         *
         * @see #setNegativeButton(int, OnClickListener)
         * @see #setNegativeButton(OnClickListener)
         */
        public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {

            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog negative button setting.
         *
         * @param listener Click listener
         *
         * @return Builder instance
         *
         * @see #setNegativeButton(String, OnClickListener)
         * @see #setNegativeButton(int, OnClickListener)
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {

            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Dialog cancel listener setting.
         *
         * @param listener Cancel listener
         *
         * @return Builder instance
         */
        public Builder setOnCancelListener(DialogInterface.OnCancelListener listener) {
            this.OnCancelClickListener = listener;
            return this;
        }

        /**
         * Dialog create.
         *
         * @return DialogCustom instance
         */
        public DialogCustom create() {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final DialogCustom dialog = new DialogCustom(context, R.style.NTLES_Dialog1);
            View layout = null;
            if (iDialogType == 1) {
                layout = inflater.inflate(R.layout.dialog_custom, null);
            } else if (iDialogType == 2) {
                layout = inflater.inflate(R.layout.dialog_custom1, null);
            }

            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if (mUtil.null2string(title).equals("")) {
                title = context.getString(R.string.MSG_TITLE_000);
            }

            if (iDialogType == 1) {
                if (msgno != null) {
                    int getRes = context.getResources().getIdentifier(msgno, "string", context.getPackageName());
                    String msg = context.getResources().getString(getRes);

                    if (!"".equals(msg)) {
                        if (message != null) {
                            message = msg + message;
                        } else {
                            message = msg;
                        }
                    }

                    if (positiveButtonText == null) {
                        if (mUtil.find(Constants.MSG_BTN_1_DELETE, msgno) >= 0) {
                            positiveButtonText = context.getString(R.string.layout_delete);
                        }
                        if (mUtil.find(Constants.MSG_BTN_1_CANCEL, msgno) >= 0) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_CANCEL);
                        }

                        if (mUtil.find(Constants.MSG_BTN_1_RETRAY, msgno) >= 0) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_RETRY);
                        }
                        if (mUtil.find(Constants.MSG_BTN_1_LATER, msgno) >= 0) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_LATER);
                        }
                        if (mUtil.find(Constants.MSG_BTN_1_NOW, msgno) >= 0) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_NOW);
                        }

                        if (positiveButtonText == null) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_CONFIRM);
                        }
                    }

                    if (negativeButtonText == null) {
                        if (mUtil.find(Constants.MSG_BTN_COUNT_2, msgno) >= 0) {
                            if (mUtil.find(Constants.MSG_BTN_2_CONFIRM, msgno) >= 0) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_CONFIRM);
                            }

                            if (mUtil.find(Constants.MSG_BTN_2_CANCEL, msgno) >= 0) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_CANCEL);
                            }

                            if (mUtil.find(Constants.MSG_BTN_2_RETRAY, msgno) >= 0) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_RETRY);
                            }

                            if (mUtil.find(Constants.MSG_BTN_2_LATER, msgno) >= 0) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_LATER);
                            }
                            if (mUtil.find(Constants.MSG_BTN_2_NOW, msgno) >= 0) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_NOW);
                            }

                            if (negativeButtonText == null) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_CANCEL);
                            }
                        }
                    }
                } else {
                    positiveButtonText = context.getString(R.string.MSG_BTN_CONFIRM);
                }
            } else if (iDialogType == 2) {
                //                positiveButtonText = mUtil.getString(mContext, R.string.MSG_BTN_APP_START);
                //                negativeButtonText = mUtil.getString(mContext, R.string.MSG_BTN_CLOSE);
            }

            ((TextView) layout.findViewById(R.id.tvTitle)).setText(title);

            Boolean positiveButtonCheck = false;
            Boolean negativeButtonCheck = false;

            if (!mUtil.null2string(positiveButtonText).equals("")) {
                positiveButtonCheck = true;
                ((TextView) layout.findViewById(R.id.tvBtn1)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((LinearLayout) layout.findViewById(R.id.llBtn1)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {

                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                positiveButtonCheck = false;
                layout.findViewById(R.id.llBtn1).setVisibility(View.GONE);
            }

            if (!mUtil.null2string(negativeButtonText).equals("")) {
                negativeButtonCheck = true;
                ((TextView) layout.findViewById(R.id.tvBtn2)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((LinearLayout) layout.findViewById(R.id.llBtn2)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {

                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                negativeButtonCheck = false;
                //                if ( iDialogType == 1 ) {
                layout.findViewById(R.id.llBtn2).setVisibility(View.GONE);
                //                }
            }

            int getResBtnLeft = context.getResources().getIdentifier("d_img_popup_btn_left", "drawable", context.getPackageName());
            int getResBtnRight = context.getResources().getIdentifier("d_img_popup_btn_right", "drawable", context.getPackageName());
            int getResBtn = context.getResources().getIdentifier("d_img_popup_btn", "drawable", context.getPackageName());

            if (positiveButtonCheck && negativeButtonCheck) {
                layout.findViewById(R.id.llBtn2).setBackgroundResource(getResBtnLeft);
                layout.findViewById(R.id.llBtn1).setBackgroundResource(getResBtnRight);
            } else {
                layout.findViewById(R.id.llBtn2).setBackgroundResource(getResBtn);
                layout.findViewById(R.id.llBtn1).setBackgroundResource(getResBtn);
            }

            // set the content message
            if (message != null) {
                if (message.contains("<")) {
                    ((TextView) layout.findViewById(R.id.tvBody)).setText(Html.fromHtml(message));
                } else {
                    ((TextView) layout.findViewById(R.id.tvBody)).setText(message);
                    // android:gravity="center"
                }
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.llBody)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.llBody)).addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
            }
            // ((Button) layout.findViewById(R.id.btnExit)).setOnClickListener(new View.OnClickListener() {
            // public void onClick(View v) {
            // positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
            // }
            // });

            dialog.setCancelable(bCancelable);
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
