package org.keyscience.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.adapters.AccordionListAdapter;

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

public class PreviousWinnersFragment extends Fragment {

    ExpandableListView expandableListView;
    private Context context;

    private List<String> parentItems;
    private Map<String, List<String>> childItems;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        context = getContext();

        View view = inflater.inflate(R.layout.previouswinners_fragment, container, false);

        // Paragraphs that require HTML
        String prevwinnersParagraph = getResources().getString(R.string.prevwinners_mainParagraphTxt);
        TextView prevwinners_mainParagraph = view.findViewById(R.id.prevwinners_mainParagraph);
        prevwinners_mainParagraph.setText(Html.fromHtml(prevwinnersParagraph));

        expandableListView = view.findViewById(R.id.keychallenge_accordions);
        fillData();

        ExpandableListAdapter expandableListAdapter = new AccordionListAdapter(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) {
                AccordionListAdapter listAdapter = (AccordionListAdapter) 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();
            }
        });
        //

        initButtons(view);

        return view;
    }

    private void initButtons(View v) {

    }

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

        String[] parentList = new String[]{"Pre K - 1st Grades",
                "2nd - 3rd Grades",
                "4th - 5th Grades",
                "6th - 8th Grades",
                "9th - 12th Grades"};
        parentItems.addAll(Arrays.asList(parentList));

        String preK_content = getResources().getString(R.string.winners_PreK_FirstGrades);
        String second_third_content = getResources().getString(R.string.winners_secondThirdGrades);
        String fourth_fifth_content = getResources().getString(R.string.winners_fourthFifthGrades);
        String sixth_eight_content = getResources().getString(R.string.winners_sixthEightGrades);
        String ninth_twelfth_content = getResources().getString(R.string.ninthTwelthGrades);

        List<String> preK_first = new ArrayList<>();
        preK_first.add(preK_content);

        List<String> second_third = new ArrayList<>();
        second_third.add(second_third_content);

        List<String> fourth_fifth = new ArrayList<>();
        fourth_fifth.add(fourth_fifth_content);

        List<String> sixth_eight = new ArrayList<>();
        sixth_eight.add(sixth_eight_content);

        List<String> ninth_twelfth = new ArrayList<>();
        ninth_twelfth.add(ninth_twelfth_content);

        childItems.put(parentItems.get(0), preK_first);
        childItems.put(parentItems.get(1), second_third);
        childItems.put(parentItems.get(2), fourth_fifth);
        childItems.put(parentItems.get(3), sixth_eight);
        childItems.put(parentItems.get(4), ninth_twelfth);
    }
}
