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

package com.ntels.onecable.view.dialog;

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

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 java.util.ArrayList;
import java.util.HashMap;

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

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

        super(context, theme);
    }

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

        super(context);
    }

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

        private Util mUtil = new Util();
        private CustomDialog mCustomDialog = new CustomDialog();
        private Context mContext;
        private String mTitle;
        //mDialogType 1= 일반, 2=push
        private int mDialogType = 1;
        private String mMsgNo;
        private String mMessage;
        private Boolean isCancelable = true;
        private String mPositiveButtonText;
        private String mNegativeButtonText;
        private View mContentView;
        private View mBodyView = null;
        private int mSelectType = -1;
        private ArrayList<HashMap<String, Object>> mTotalList = new ArrayList<>();

        private DialogInterface.OnClickListener mPositiveButtonClickListener;
        private DialogInterface.OnClickListener mNegativeButtonClickListener;

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

            this.mContext = context;
        }

        /**
         * Dialog cancel 가능여부 설정.
         *
         * @param isCancel true or false
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setCancelable(Boolean isCancel) {

            this.isCancelable = isCancel;
            return this;
        }

        /**
         * Set the Dialog mMessage from String.
         *
         * @param totalList Dialog data list
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setList(ArrayList<HashMap<String, Object>> totalList) {

            this.mTotalList = totalList;
            return this;
        }

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

            this.mMessage = message;
            return this;
        }

        /**
         * {@link #mSelectType} getter.
         *
         * @return Select type
         */
        public int getSelectType() {

            return mSelectType;
        }

        /**
         * {@link #mMessage} setter.
         *
         * @param message String resource id
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setMessage(String)
         */
        public Builder setMessage(int message) {

            this.mMessage = mContext.getText(message).toString();
            return this;
        }

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

            this.mTitle = mContext.getText(title).toString();
            return this;
        }

        /**
         * {@link #mTitle} setter.
         *
         * @param title Dialog title
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setTitle(String title) {

            this.mTitle = title;
            return this;
        }

        /**
         * {@link #mDialogType} setter.
         *
         * @param dialogType Dialog type
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setDialogType(int dialogType) {

            this.mDialogType = dialogType;
            return this;
        }

        /**
         * {@link #mMsgNo} setter.
         *
         * @param msgNo Error message resource id
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setErrNo(String msgNo) {

            this.mMsgNo = msgNo;
            return this;
        }

        /**
         * Set a custom content view for the Dialog. If a mMessage is set, the mContentView is not
         * added
         * to the Dialog...
         *
         * @param v Displayed view for dialog
         *
         * @return {@link DialogCustomList.Builder} object
         */
        public Builder setContentView(View v) {

            this.mContentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener.
         *
         * @param positiveButtonText Positive button title resource id
         * @param listener           Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setPositiveButton(String, OnClickListener)
         * @see #setPositiveButton(OnClickListener)
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {

            this.mPositiveButtonText = mContext.getText(positiveButtonText).toString();
            this.mPositiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the positive button text and it's listener.
         *
         * @param positiveButtonText Positive button title
         * @param listener           Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setPositiveButton(int, OnClickListener)
         * @see #setPositiveButton(OnClickListener)
         */
        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {

            this.mPositiveButtonText = positiveButtonText;
            this.mPositiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the positive button text and it's listener.
         *
         * @param listener Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setPositiveButton(int, OnClickListener)
         * @see #setPositiveButton(String, OnClickListener)
         */
        public Builder setPositiveButton(DialogInterface.OnClickListener listener) {

            this.mPositiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener.
         *
         * @param negativeButtonText Negative button title resource id
         * @param listener           Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setNegativeButton(String, OnClickListener)
         * @see #setNegativeButton(OnClickListener)
         */
        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {

            this.mNegativeButtonText = mContext.getText(negativeButtonText).toString();
            this.mNegativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button text and it's listener.
         *
         * @param negativeButtonText Negative button title
         * @param listener           Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setNegativeButton(int, OnClickListener)
         * @see #setNegativeButton(OnClickListener)
         */
        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {

            this.mNegativeButtonText = negativeButtonText;
            this.mNegativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button text and it's listener.
         *
         * @param listener Click listener
         *
         * @return {@link DialogCustomList.Builder} object
         *
         * @see #setNegativeButton(int, OnClickListener)
         * @see #setNegativeButton(String, OnClickListener)
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {

            this.mNegativeButtonClickListener = listener;
            return this;
        }

        /**
         * Create the custom dialog.
         */
        public DialogCustomList create() {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final DialogCustomList dialog = new DialogCustomList(mContext, 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) {

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

                    String imageFileName = mUtil.null2string(hmItem.get(Constants.KEY_IMAGE_FILE_NAME));
                    String title = mUtil.null2string(hmItem.get(Constants.KEY_TITLE));
                    final int selectType = mUtil.null2int(hmItem.get(Constants.KEY_SELECT_TYPE));

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

                            mPositiveButtonClickListener.onClick(dialog, selectType);
                        }
                    });
                    // 라인 생성 막음
//                    if (iRow < mTotalList.size() - 1) {
//
//                        View vLine = new View(mContext);
//                        vLine.setBackgroundResource(R.drawable.color_line_02);
//                        vLine.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
//                        llChildView.addView(vLine);
//                    }
                    LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
                    childParam1.weight = 1f;
                    llChildView.setLayoutParams(childParam1);

                    llBodyList.addView(llChildView);
//                    llChildView.
                }
            }
            dialog.setCancelable(isCancelable);
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
