package org.keyscience.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
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.app.HomeActivity;

public class LabPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    private int layout;
    private int[] images;
    private String[] titles;
    private String[] paragraphs;
    private Button learnMore;

    public LabPagerAdapter(Context context, int layout, int[] images, String[] titles, String[] paragraphs) {
        this.context = context;
        this.layout = layout;
        this.images = images;
        this.titles = titles;
        this.paragraphs = paragraphs;
    }


    @Override
    public int getCount() {
        return titles.length;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(layout, null);

        ImageView imageView = view.findViewById(R.id.headerImage);
        imageView.setImageResource(images[position]);

        TextView title = view.findViewById(R.id.ls_title);
        title.setText(titles[position]);

        TextView paragraph = view.findViewById(R.id.ls_paragraph);
        paragraph.setText(paragraphs[position]);

        // learn more button is only set for this cardView
        if (layout == R.layout.program_list_item) {
            learnMore = view.findViewById(R.id.programsLearnMoreButton);
            learnMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((HomeActivity) context).setViewPager(position + 1);
                }
            });
        }


        //TODO: setOnclickListener link to the labs (Need Nathan's approval)

        ViewPager vp = (ViewPager) container;
        vp.addView(view, 0);
        return view;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, Object object) {

        ViewPager vp = (ViewPager) container;
        View view = (View) object;
        vp.removeView(view);

    }
}
