/*
 * 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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
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;

import java.util.ArrayList;
import java.util.HashMap;

public class Dialog_Custom_List extends Dialog {

    public Dialog_Custom_List(Context context, int theme) {

        super(context, theme);
    }

    public Dialog_Custom_List(Context context) {

        super(context);
    }

    /**
     * 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 int                                iSelectType = -1;
        private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();

        private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;

        public Builder(Context context) {

            this.context = context;
        }

        public Builder setCancelable(Boolean bStr) {

            this.bCancelable = bStr;
            return this;
        }

        /**
         * Set the Dialog message from String
         *
         * @param title
         *
         * @return
         */
        public Builder setList(ArrayList<HashMap<String, Object>> alTotalList) {

            this.alTotalList = alTotalList;
            return this;
        }

        /**
         * Set the Dialog message from String
         *
         * @param title
         *
         * @return
         */
        public Builder setMessage(String message) {

            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from String
         *
         * @param title
         *
         * @return
         */
        public int getSelectType() {

            return iSelectType;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param title
         *
         * @return
         */
        public Builder setMessage(int message) {

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

        /**
         * Set the Dialog title from resource
         *
         * @param title
         *
         * @return
         */
        public Builder setTitle(int title) {

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

        /**
         * Set the Dialog title from String
         *
         * @param title
         *
         * @return
         */
        public Builder setTitle(String title) {

            this.title = title;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         *
         * @return
         */
        public Builder setDialogType(int iDialogType) {

            this.iDialogType = iDialogType;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         *
         * @return
         */
        public Builder setErrNo(String msgno) {

            this.msgno = msgno;
            return this;
        }

        /**
         * Set a custom content view for the Dialog. If a message is set, the contentView is not added to the Dialog...
         *
         * @param v
         *
         * @return
         */
        public Builder setContentView(View v) {

            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @param listener
         *
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {

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

        /**
         * Set the positive button text and it's listener
         *
         * @param positiveButtonText
         * @param listener
         *
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {

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

        /**
         * Set the positive button text and it's listener
         *
         * @param positiveButtonText
         * @param listener
         *
         * @return
         */
        public Builder setPositiveButton(DialogInterface.OnClickListener listener) {

            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param negativeButtonText
         * @param listener
         *
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {

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

        /**
         * Set the negative button text and it's listener
         *
         * @param negativeButtonText
         * @param listener
         *
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {

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

        /**
         * Set the negative button text and it's listener
         *
         * @param negativeButtonText
         * @param listener
         *
         * @return
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {

            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public Dialog_Custom_List create() {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final Dialog_Custom_List dialog = new Dialog_Custom_List(context, R.style.Translucent);
            View layout = null;
            layout = inflater.inflate(R.layout.dialog_custom_list, null);

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

            LinearLayout llPageRoot = (LinearLayout) layout.findViewById(R.id.llPageRoot);
            llPageRoot.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            });
            if ( alTotalList.size() > 0 ) {
                String sOldTitle = "";
                String sBody = "";
                LinearLayout llBodyList = (LinearLayout) layout.findViewById(R.id.llBodyList);
                llBodyList.removeAllViews();
                for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                    HashMap<String, Object> hmItem = new HashMap<String, Object>();
                    hmItem = alTotalList.get(iRow);
                    LinearLayout llChildView = (LinearLayout) View.inflate(context, R.layout.row_dialog_custom_list, null);

                    String sHTTP_image_file_name = util.null2string(hmItem.get(Constants.HTTP_image_file_name));
                    String sHTTP_title = util.null2string(hmItem.get(Constants.HTTP_title));
                    final int iHTTP_select_type = util.null2int(hmItem.get(Constants.HTTP_select_type));

                    int getRes = context.getResources().getIdentifier(sHTTP_image_file_name, "drawable", context.getPackageName());
                    ((ImageView) llChildView.findViewById(R.id.ivImage)).setBackgroundResource(getRes);
                    ((TextView) llChildView.findViewById(R.id.tvTitle)).setText(sHTTP_title);
                    llChildView.findViewById(R.id.llRowBody).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {

                            positiveButtonClickListener.onClick(dialog, iHTTP_select_type);
                        }
                    });
                    if ( iRow < alTotalList.size() - 1 ) {

                        View vLine = new View(context);
                        vLine.setBackgroundResource(R.drawable.color_line_02);
                        vLine.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
                        llChildView.addView(vLine);
                    }
                    llBodyList.addView(llChildView);
                }
            }
            dialog.setCancelable(bCancelable);
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
