package in.techware.carrefour.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Calendar;

import in.techware.carrefour.R;
import in.techware.carrefour.util.AppConstants;

/**
 * Created by Jemsheer K D on 24 July, 2018.
 * Package in.techware.carrefour.adapters
 * Project Carrefour
 */
public class SelectRecyclerAdapter extends RecyclerView.Adapter<SelectRecyclerAdapter.ViewHolder> {

    private static final String TAG = "SelectRecyclerAdapter";
    private final Activity mContext;
    private String action;
    private SelectRecyclerAdapterListener selectRecyclerAdapterListener;
    private String[] choiceList;
    private boolean isLoadMore;

    public SelectRecyclerAdapter(Activity mContext, String[] choiceList, String action) {
        this.mContext = mContext;
        this.choiceList = choiceList;
        this.action = action;

    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String[] getChoiceList() {
        return choiceList;
    }

    public void setChoiceList(String[] choiceList) {
        this.choiceList = choiceList;
    }

    public boolean isLoadMore() {
        return isLoadMore;
    }

    public void setLoadMore(boolean loadMore) {
        isLoadMore = loadMore;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemLayout = inflater.inflate(R.layout.item_select, parent, false);
        return new ViewHolder(itemLayout);

    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        setLayoutSelect(holder, position);
    }

    @Override
    public int getItemCount() {
        // dummy count.. to be changed.
        int count;
        try {
            if (AppConstants.ACTION_CHOOSE_YEAR.equals(action)) {
                int num = Calendar.getInstance().get(Calendar.YEAR) - AppConstants.YEAR_START - 18 + 1;
                count = num;
            } else
                count = choiceList.length;
        } catch (Exception e) {
            return 0;
        }
        return count;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void setLayoutSelect(final ViewHolder holder, final int position) {
        final String bean = choiceList[position];
        holder.txtName.setText(bean);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtName;

        ViewHolder(View lytItem) {
            super(lytItem);

            txtName = (TextView) lytItem.findViewById(R.id.txt_item_select_name);


            lytItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
//                    mVibrator.vibrate(25);

                    String bean = choiceList[getLayoutPosition()];
                    selectRecyclerAdapterListener.onItemSelected(getLayoutPosition(), bean);
                    /* mContext.startActivity(new Intent(mContext, DetailsActivity.class)
                            .putExtra("bean", bean));*/
                }
            });
        }
    }

    public static interface SelectRecyclerAdapterListener {

        void onItemSelected(int position, String value);

    }

    public SelectRecyclerAdapterListener getSelectRecyclerAdapterListener() {
        return selectRecyclerAdapterListener;
    }


    public void setSelectRecyclerAdapterListener(SelectRecyclerAdapterListener selectRecyclerAdapterListener) {
        this.selectRecyclerAdapterListener = selectRecyclerAdapterListener;
    }
}
