package org.keyscience.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.models.OurBiomesModel;

import java.util.List;

public class OurBiomesAdapter extends RecyclerView.Adapter<OurBiomesAdapter.OB_ViewHolder> {

    private List<OurBiomesModel> mData;
    private LayoutInflater mInflater;

    public OurBiomesAdapter(List<OurBiomesModel> mData, Context context) {
        this.mData = mData;
        mInflater = LayoutInflater.from(context);
    }


    @NonNull
    @Override
    public OB_ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.our_biomes_item, viewGroup, false);
        return new OB_ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OB_ViewHolder ob_viewHolder, int i) {
        ob_viewHolder.setData(mData.get(i));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class OB_ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title;
        TextView sub;
        Button learMore;
        boolean fullText = false;

        public OB_ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.ob_headerImage);
            title = itemView.findViewById(R.id.ob_title);
            sub = itemView.findViewById(R.id.ob_sub);
            learMore = itemView.findViewById(R.id.ob_LearnMoreButton);

        }

        void setData(final OurBiomesModel model) {
            image.setImageResource(model.getImage());
            title.setText(model.getTitle());
            sub.setText(model.getSub());

            // set the button to display or hide full information
            learMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!fullText) {
                        fullText = true;
                        sub.setText(model.getContents());
                        learMore.setText("Show Less");
                    } else {
                        fullText = false;
                        sub.setText(model.getSub());
                        learMore.setText("Learn More");
                    }

                }
            });

        }
    }

}
