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

package com.ntels.kneet.v3.view.dialog;

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;

import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.R;

public class Dialog_Custom extends Dialog {

    public Dialog_Custom(Context context, int theme) {

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

    public Dialog_Custom(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         util         = 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;

        public Builder(Context context) {

            this.context = context;
        }

        public Builder setCancelable(Boolean bStr) {

            this.bCancelable = bStr;
            return this;
        }

        public Builder setMessage(String message) {

            this.message = message;
            return this;
        }
        public Builder setMessage(int message) {

            this.message = context.getText(message).toString();
            return this;
        }
        public Builder setTitle(int title) {

            this.title = context.getText(title).toString();
            return this;
        }
        public Builder setTitle(String title) {

            this.title = title;
            return this;
        }
        public Builder setDialogType(int iDialogType) {

            this.iDialogType = iDialogType;
            return this;
        }
        public Builder setErrNo(String msgno) {

            this.msgno = msgno;
            return this;
        }
        public Builder setContentView(View v) {

            this.contentView = v;
            return this;
        }
        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {

            this.positiveButtonText = context.getText(positiveButtonText).toString();
            this.positiveButtonClickListener = listener;
            return this;
        }
        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {

            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
        public Builder setPositiveButton(DialogInterface.OnClickListener listener) {

            this.positiveButtonClickListener = listener;
            return this;
        }
        public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {

            this.negativeButtonText = context.getText(negativeButtonText).toString();
            this.negativeButtonClickListener = listener;
            return this;
        }
        public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {

            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {

            this.negativeButtonClickListener = listener;
            return this;
        }
        public Builder setOnCancelListener(DialogInterface.OnCancelListener listener) {
            this.OnCancelClickListener = listener;
            return this;
        }
        public Dialog_Custom create() {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final Dialog_Custom dialog = new Dialog_Custom(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 ( util.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 ( util.find(Constants.MSG_BTN_1_DELETE, msgno) >= 0 ) {
                            positiveButtonText = context.getString(R.string.layout_delete);
                        }
                        if ( util.find(Constants.MSG_BTN_1_CANCEL, msgno) >= 0 ) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_CANCEL);
                        }

                        if ( util.find(Constants.MSG_BTN_1_RETRAY, msgno) >= 0 ) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_RETRY);
                        }
                        if ( util.find(Constants.MSG_BTN_1_LATER, msgno) >= 0 ) {
                            positiveButtonText = context.getString(R.string.MSG_BTN_LATER);
                        }
                        if ( util.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 ( util.find(Constants.MSG_BTN_COUNT_2, msgno) >= 0 ) {
                            if ( util.find(Constants.MSG_BTN_2_CONFIRM, msgno) >= 0 ) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_CONFIRM);
                            }

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

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

                            if ( util.find(Constants.MSG_BTN_2_LATER, msgno) >= 0 ) {
                                negativeButtonText = context.getString(R.string.MSG_BTN_LATER);
                            }
                            if ( util.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 = util.getString(context, R.string.MSG_BTN_APP_START);
                //                negativeButtonText = util.getString(context, R.string.MSG_BTN_CLOSE);
            }

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

            if ( !util.null2string(positiveButtonText).equals("") ) {
                ((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 {
                layout.findViewById(R.id.llBtn1).setVisibility(View.GONE);
            }
            if ( !util.null2string(negativeButtonText).equals("") ) {
                ((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 {
                //                if ( iDialogType == 1 ) {
                layout.findViewById(R.id.llBtn2).setVisibility(View.GONE);
                //                }
            }
            // 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;
        }
    }
}
