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 android.widget.FrameLayout;
import android.widget.ImageView;

import org.keyscience.R;
import org.keyscience.adapters.CspLabAdapter;
import org.keyscience.adapters.HomeItemsAdapter;
import org.keyscience.models.CspLabModel;
import org.keyscience.models.HomePageItem;

import java.util.ArrayList;

public class HomeFragment extends Fragment {
    private static final String TAG = "Home";
    private RecyclerView recyclerViewOne;
    private RecyclerView recyclerViewTwo;
    private boolean isHidden = true;

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

        recyclerViewOne = view.findViewById(R.id.recyclerViewOne);
        recyclerViewTwo = view.findViewById(R.id.recyclerViewTwo);

        setUpRecycler(view, recyclerViewOne, new ArrayList<>(HomePageItem.getData().subList(0, 4)),0);
        setUpRecycler(view, recyclerViewTwo, new ArrayList<>(HomePageItem.getData().subList(4, HomePageItem.getData().size())),1);

        //cspLab recycler
        final RecyclerView csp_recyclerView = view.findViewById(R.id.csp_recyclerView);
        csp_recyclerView.setFocusable(false);
        CspLabAdapter adapter = new CspLabAdapter(CspLabModel.getData(), view.getContext());
        csp_recyclerView.setAdapter(adapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), 2); // (Context context, int spanCount)
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        csp_recyclerView.setLayoutManager(gridLayoutManager);
        csp_recyclerView.setVisibility(View.GONE);
        setItemDecoration(csp_recyclerView);
        FrameLayout frameLayout = view.findViewById(R.id.CSP_Lab_layout);
        final ImageView indicator = view.findViewById(R.id.indicatorCsp);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isHidden) {
                    indicator.setImageResource(R.drawable.ic_chevron_arrow_up);
                    csp_recyclerView.setVisibility(View.VISIBLE);
                    isHidden = false;
                } else {
                    indicator.setImageResource(R.drawable.ic_chevron_arrow_down);
                    csp_recyclerView.setVisibility(View.GONE);
                    isHidden = true;
                }
            }
        });
        return view;
        //cspLab recycler setup ends
    }

    private void setUpRecycler(View view, RecyclerView recyclerView, ArrayList<HomePageItem> data, int gridLevel) {
        recyclerView.setFocusable(false);
        HomeItemsAdapter adapter = new HomeItemsAdapter(data, view.getContext(), gridLevel);
        recyclerView.setAdapter(adapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), 2); // (Context context, int spanCount)
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        setItemDecoration(recyclerView);
    }

    private void setItemDecoration(RecyclerView recyclerView) {
        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 = 2;//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;
                }
            }
        });
    }

}
