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.ExpandableListAdapter;
import android.widget.ExpandableListView;

import org.keyscience.R;
import org.keyscience.app.Constants;
import org.keyscience.models.NavDrawerItem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NavDrawerAdapter extends RecyclerView.Adapter<NavDrawerAdapter.NavDrawerViewHolder> {


    private LayoutInflater inflater;
    private Context context;
    private List<NavDrawerItem> dataList;

    private ExpandableListView expandableListView;
    private ExpandableListAdapter expandableListAdapter;
    private List<String> parentItems;
    private Map<String, List<String>> childItems;
    private int lastPosition = -1;

    public int groupPos;

    public NavDrawerAdapter(Context context, List<NavDrawerItem> dataList) {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public NavDrawerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.nav_drawer_list_item, viewGroup, false);
        NavDrawerViewHolder navDrawerViewHolder = new NavDrawerViewHolder(view);
        return navDrawerViewHolder;
    }

    // Hacky way of resetting the viewGroup so that the icon update can reflect.
    public void CollapseExpandGroup() {
        expandableListView.collapseGroup(groupPos);
        expandableListView.expandGroup(groupPos);
    }

    public void fillData() {
        parentItems = new ArrayList<>();
        childItems = new HashMap<>();

        String[] parentList = new String[]{Constants.HOME, Constants.LOGIN, Constants.CSP_LAB, Constants.PROGRAMS, Constants.EVENTS, Constants.NEWS, Constants.RESOURCES};
        parentItems.addAll(Arrays.asList(parentList));

        List<String> homeList = new ArrayList<>();
        List<String> profileList = new ArrayList<>();
        List<String> eventsList = new ArrayList<>();
        List<String> newsList = new ArrayList<>();

        List<String> cspList = new ArrayList<>();
        cspList.add(Constants.CSP_LEARN);
        cspList.add(Constants.CSP_EXPLORE);
        cspList.add(Constants.CSP_RECORD);
        cspList.add(Constants.CSP_REVIEW);

        List<String> programsList = new ArrayList<>();
        programsList.add(Constants.KEY_CHALLENGE);
        programsList.add(Constants.WATER_QUALITY);
        programsList.add(Constants.SEA_LEVEL_RISE);
        programsList.add(Constants.WATER_WATCH);
        programsList.add(Constants.REEF_RESTORATION);
        programsList.add(Constants.LECTURE_SERIES);
        programsList.add(Constants.FIELD_ACTIVITIES);

        List<String> resourceList = new ArrayList<>();
        resourceList.add(Constants.ABOUT_US);
        resourceList.add(Constants.OUR_BIOMES);
        resourceList.add(Constants.NEWSLETTERS);
        resourceList.add(Constants.OUR_PARTNERS);
        resourceList.add(Constants.CONTACT_US);

        childItems.put(parentItems.get(0), homeList);
        childItems.put(parentItems.get(1), profileList);
        childItems.put(parentItems.get(2), cspList);
        childItems.put(parentItems.get(3), programsList);
        childItems.put(parentItems.get(4), eventsList);
        childItems.put(parentItems.get(5), newsList);
        childItems.put(parentItems.get(6), resourceList);
    }


    @Override
    public void onBindViewHolder(@NonNull NavDrawerViewHolder navDrawerViewHolder, int i) {
        NavDrawerItem current = dataList.get(i);
    }

    @Override
    public int getItemCount() {
        return 1;   // Changed for ExpandableListView
    }


    class NavDrawerViewHolder extends RecyclerView.ViewHolder {
        public NavDrawerViewHolder(@NonNull View itemView) {
            super(itemView);

            expandableListView = itemView.findViewById(R.id.expandable_drawer);
            fillData();

            expandableListAdapter = new ExpListAdapter(context, parentItems, childItems);
            expandableListView.setAdapter(expandableListAdapter);

            expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    groupPos = groupPosition;

                    if (lastPosition != -1
                            && groupPosition != lastPosition) {
                        expandableListView.collapseGroup(lastPosition);
                    }
                    lastPosition = groupPosition;
                }
            });
        }
    }
}
