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.models.NavDrawerItem;

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

public class WaterWatchListAdapter extends RecyclerView.Adapter<WaterWatchListAdapter.ChartsListViewHolder> {


    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;

    public WaterWatchListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

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

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

        String[] parentList = new String[]{"Water Charts",
                "Water Table",
                "Nutrient Charts",
                "Nutrient Table"};
        parentItems.addAll(Arrays.asList(parentList));

        String water_chartsTxt = context.getResources().getString(R.string.water_charts);
        String water_tableTxt = context.getResources().getString(R.string.water_table);
        String nutrient_chartsTxt = context.getResources().getString(R.string.nutrient_charts);
        String nutrient_tableTxt = context.getResources().getString(R.string.nutrient_table);

        List<String> water_charts = new ArrayList<>();
        water_charts.add(water_chartsTxt);

        List<String> water_table = new ArrayList<>();
        water_table.add(water_tableTxt);

        List<String> nutrient_charts = new ArrayList<>();
        nutrient_charts.add(nutrient_chartsTxt);

        List<String> nutrient_table = new ArrayList<>();
        nutrient_table.add(nutrient_tableTxt);

        childItems.put(parentItems.get(0), water_charts);
        childItems.put(parentItems.get(1), water_table);
        childItems.put(parentItems.get(2), nutrient_charts);
        childItems.put(parentItems.get(3), nutrient_table);
    }

    @Override
    public void onBindViewHolder(@NonNull WaterWatchListAdapter.ChartsListViewHolder ChartsListViewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return 1;
    }


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

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

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

            // -- We want to adjust the height of the ScrollView based on the groups children
            expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                                            int groupPosition, long id) {
                    setListViewHeight(parent, groupPosition);
                    return false;
                }

                private void setListViewHeight(ExpandableListView listView,
                                               int group) {
                    ChartsAccordionAdapter listAdapter = (ChartsAccordionAdapter) listView.getExpandableListAdapter();
                    int totalHeight = 0;
                    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                            View.MeasureSpec.EXACTLY);
                    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
                        View groupItem = listAdapter.getGroupView(i, false, null, listView);
                        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                        totalHeight += groupItem.getMeasuredHeight();

                        if (((listView.isGroupExpanded(i)) && (i != group))
                                || ((!listView.isGroupExpanded(i)) && (i == group))) {
                            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                                View listItem = listAdapter.getChildView(i, j, false, null,
                                        listView);
                                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                                totalHeight += listItem.getMeasuredHeight();

                            }
                        }
                    }

                    ViewGroup.LayoutParams params = listView.getLayoutParams();
                    int height = totalHeight
                            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
                    if (height < 10)
                        height = 300;
                    params.height = height;
                    listView.setLayoutParams(params);
                    listView.requestLayout();
                }
            });
        }
    }
}
