package com.ntels.onecable.view.join;

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

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <pre>
 * 1. ClassName :   JoinStep3rdMasterMenuAdapter
 * 2. FileName  :   JoinStep3rdMasterMenuAdapter.java
 * 3. Package   :   com.ntels.onecable.view.homemember
 * 4. Date      :   2017-04-05
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */

public class JoinStep3rdMasterMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<HashMap<String, Object>> mQuizList;
    private View.OnClickListener mQuizOnClickListener;
    private Util mUtil = new Util();

    JoinStep3rdMasterMenuAdapter(Context context, ArrayList<HashMap<String, Object>> quizList, View.OnClickListener quizOnClickListener) {
        this.mContext = context;
        this.mQuizList = quizList;
        this.mQuizOnClickListener = quizOnClickListener;
    }

    public void setData(ArrayList<HashMap<String, Object>> quizList) {
        this.mQuizList = quizList;
    }

    /**
     * Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
     * an item.
     * <p>
     * This new ViewHolder should be constructed with a new View that can represent the items
     * of the given type. You can either create a new View manually or inflate it from an XML
     * layout file.
     * <p>
     * The new ViewHolder will be used to display items of the adapter using
     * {@link #onBindViewHolder(ViewHolder, int, List)}. Since it will be re-used to display
     * different items in the data set, it is a good idea to cache references to sub views of
     * the View to avoid unnecessary {@link View#findViewById(int)} calls.
     *
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return A new ViewHolder that holds a View of the given view type.
     * @see #getItemViewType(int)
     * @see #onBindViewHolder(ViewHolder, int)
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_homemember_add_menu, parent, false);
        TextView tvQuiz = ButterKnife.findById(view, R.id.tvQuiz);
        tvQuiz.setOnClickListener(mQuizOnClickListener);
        return new ItemViewHolder(view);
    }

    /**
     * Called by RecyclerView to display the data at the speci\fied position. This method should
     * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
     * position.
     * <p>
     * Note that unlike {@link ListView}, RecyclerView will not call this method
     * again if the position of the item changes in the data set unless the item itself is
     * invalidated or the new position cannot be determined. For this reason, you should only
     * use the <code>position</code> parameter while acquiring the related data item inside
     * this method and should not keep a copy of it. If you need the position of an item later
     * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will
     * have the updated adapter position.
     *
     * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can
     * handle efficient partial bind.
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.tvQuiz.setText(mUtil.null2string(mQuizList.get(position).get(Constants.KEY_QUIZ)));
            itemViewHolder.tvQuiz.setTag(position);
        }
    }

    /**
     * Returns the total number of items in the data set held by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return mQuizList.size();
    }

    /**
     * 1. ClassName :   ItemViewHolder
     * 2. FileName  :   JoinStep3rdMasterMenuAdapter.java
     * 3. Package   :   com.ntels.onecable.view.homemember
     * 4. Date      :   2017-04-05
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvQuiz) public TextView tvQuiz;

        /**
         * 생성자.
         *
         * @param itemView Item view holder에 사용 할 view.
         */
        public ItemViewHolder(View itemView) {

            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
