package org.keyscience.fragments;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.keyscience.R;
import org.keyscience.adapters.HomeItemsAdapter;
import org.keyscience.adapters.OurPartnersAdapter;
import org.keyscience.models.HomePageItem;
import org.keyscience.models.OurPartnersModel;

import java.util.List;

public class OurPartnersFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ourpartners_fragment, container, false);


        RecyclerView recyclerViewOne;
        RecyclerView recyclerViewTwo;

        recyclerViewOne = view.findViewById(R.id.our_partners_recyclerOne);
        recyclerViewTwo = view.findViewById(R.id.our_partners_recyclerTwo);
        // setup first recycler
        setUpRecyclerView(recyclerViewOne, view, OurPartnersModel.getDataOnlyFour(getContext()));
        //set up second recycler
        setUpRecyclerView(recyclerViewTwo, view, OurPartnersModel.getDataLastItems(getContext()));

        return view;
    }


    private void setUpRecyclerView(RecyclerView recyclerView, View view, List<OurPartnersModel> data) {
        recyclerView.setFocusable(false);
        OurPartnersAdapter adapter = new OurPartnersAdapter(data, view.getContext());
        recyclerView.setAdapter(adapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), 2); // (Context context, int spanCount)
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);

        //evenly distribute column
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view); // item position
                int spanCount = 2;
                int spacing = 15;//spacing between views in grid

                if (position >= 0) {
                    int column = position % spanCount; // item column

                    outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                    outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                    if (position < spanCount) { // top edge
                        outRect.top = spacing;
                    }
                    outRect.bottom = spacing; // item bottom
                } else {
                    outRect.left = 0;
                    outRect.right = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                }
            }
        });
    }
}
