package org.keyscience.fragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import org.keyscience.R;
import org.keyscience.adapters.SeaLevelRiseExpandableListAdapter;

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

public class SeaLevelRiseFragment extends Fragment {

    //parent headers
    public static final String TOWNHALL_1 = "1st Townhall Community Forum,\nNovember 11, 2016";
    public static final String TOWNHALL_2 = "2st Townhall Community Forum,\nFebruary 16, 2017";
    public static final String WORKSHOP = "Workshop: Managing Flood Risk on a Barrier Island,\nMay 25, 2017";
    public static final String SUBTITLE = "Agenda, Fact Sheet, & Videos";

    // child elements of first header
    public static final String AGENDA = "Agenda";
    public static final String AGENDA_1_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Agenda-for-SLR-Townhall-11-10-2016.pdf";
    public static final String FACT_SHEET = "Fact Sheet";
    public static final String FACT_SHEET_1_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Townhall-Fact-Sheet-11-10-2016.pdf";
    public static final String DRAFT_VULNERABILTY = "Draft Vulnerability Assessment";
    public static final String DRAFT_VULNERABILTY_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Vulnerability_assessment_11-11-2016.pdf";
    public static final String VIDEO_PART_1 = "Townhall Video: Part 1";
    public static final String VIDEO_PART_1_LINK = "https://www.youtube.com/watch?v=9JcaAzFVneM";
    public static final String VIDEO_PART_2 = "Townhall Video: Part 2";
    public static final String VIDEO_PART_2_LINK = "https://www.youtube.com/watch?v=BLKoeOMG7bA";
    public static final String VIDEO_PART_3 = "Townhall Video: Part 3";
    public static final String VIDEO_PART_3_LINK = "https://www.youtube.com/watch?v=HA7JKK1RKh4";

    // child elements of second header
    public static final String AGENDA_2_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Agenda-for-SLR-Townhall-02-16-2017.pdf";
    public static final String FACT_SHEET_2_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Townhall-Fact-Sheet-02-16-2017.pdf";

    public static final String RESILIENT_CITIES = "100 Resilient Cities";
    public static final String RESILIENT_CITIES_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/100-Resilient-Cities.pdf";
    public static final String GREATER_MIAMI = "Greater Miami Resilience Strategy";
    public static final String GREATER_MIAMI_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Greater-Miami-and-the-Beaches-Resilience-Strategy.pdf";
    public static final String SLR_ADAPTATION = "SLR Adaptation Master Plan";
    public static final String SLR_ADAPTATION_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/SLR-Adaptation-Master-Plan.pdf";

    public static final String VULNERABILITY = "Vulnerability Assessment Report";
    public static final String ADAPTATION = "Adaptation Solutions Presentation";
    public static final String VULNERABILITY_LINK_PDF = "https://www.keyscience.org/wp-content/uploads/2017/02/VKB-Adaptation-Strategies-v2.pdf";
    public static final String ADAPTATION_LINK_PPTX = "https://www.keyscience.org/wp-content/uploads/2017/02/VKB-Adaptation-Strategies-v2.pptx";

    public static final String COASTAL_RISK = "Coastal Risk Consulting Final Report";
    public static final String COASTAL_RISK_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/CRC-VillageOfKeyBiscayne-FinaltVulnAssess-SM.pdf";

    public static final String VIDEOS = "Videos: 2nd Community Forum";
    public static final String VIDEOS_LINK = "https://www.youtube.com/watch?v=-n83qzHdHUA";

    // child elements of third header
    public static final String PRESENTATION = "Presentation on Adaptation";
    public static final String PRESENTATION_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Workshop-Powerpoint-Slides.pdf";
    public static final String WORKSHOP_SUMMARY = "Workshop Summary: Adaptation to Sea Level Rise Best Practices";
    public static final String WORKSHOP_SUMMARY_LINK = "https://www.keyscience.org/wp-content/uploads/2017/02/Workshop-Summary-Report_06232017.pdf";

    //URLS
    private static final String PREZI_URL = "https://prezi.com/cj5dj1zp9ujg/village-of-key-biscayne";
    private static final String SURVEY_RESULTS_URL = "https://www.keyscience.org/wp-content/uploads/2017/02/Survey-Results-2-6-2017.pdf";

    private Button surveyResultsButton;
    private ImageView seaLevelPrezi;
    private ExpandableListView expandableListView;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sealevelrise_fragment, container, false);
        seaLevelPrezi = view.findViewById(R.id.sea_level_rise_prezi);
        surveyResultsButton = view.findViewById(R.id.surveyResultsButton);
        expandableListView = view.findViewById(R.id.SLRexpandableList);


        seaLevelPrezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(PREZI_URL));
                if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(intent);
                }
            }
        });

        surveyResultsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SURVEY_RESULTS_URL));
                if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(intent);
                }
            }
        });

        parentItems = getParentItems();
        childItems = getChildItems();

        final SeaLevelRiseExpandableListAdapter adapter = new SeaLevelRiseExpandableListAdapter(getContext(), parentItems, childItems);

        expandableListView.setAdapter(adapter);

        // -- 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) {
                SeaLevelRiseExpandableListAdapter listAdapter = (SeaLevelRiseExpandableListAdapter) 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();
            }
        });
        expandableListView.setGroupIndicator(null);
        return view;
    }


    private List<String> getParentItems() {
        if (parentItems == null) {
            List<String> items = new ArrayList<>();
            items.add(TOWNHALL_1);
            items.add(TOWNHALL_2);
            items.add(WORKSHOP);
            return items;
        }
        return parentItems;
    }

    private Map<String, List<String>> getChildItems() {
        if (childItems == null) {
            List<String> parentItems = getParentItems();
            Map<String, List<String>> map = new HashMap<>();

            // children of first header
            List<String> childItems = new ArrayList<>();
            childItems.add(AGENDA);
            childItems.add(FACT_SHEET);
            childItems.add(DRAFT_VULNERABILTY);
            childItems.add(VIDEO_PART_1);
            childItems.add(VIDEO_PART_2);
            childItems.add(VIDEO_PART_3);
            map.put(parentItems.get(0), childItems);

            // children of second header
            List<String> childItems2 = new ArrayList<>();
            childItems2.add(AGENDA);
            childItems2.add(FACT_SHEET);
            childItems2.add(RESILIENT_CITIES);
            childItems2.add(GREATER_MIAMI);
            childItems2.add(SLR_ADAPTATION);
            childItems2.add(VULNERABILITY);
            childItems2.add(ADAPTATION);
            childItems2.add(VIDEOS);
            childItems2.add(COASTAL_RISK);
            map.put(parentItems.get(1), childItems2);

            // children of third header
            List<String> childItems3 = new ArrayList<>();
            childItems3.add(PRESENTATION);
            childItems3.add(WORKSHOP_SUMMARY);
            map.put(parentItems.get(2), childItems3);

            return map;
        }
        return childItems;
    }

}
