package org.keyscience.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.fragments.SeaLevelRiseFragment;

import java.util.List;
import java.util.Map;

public class SeaLevelRiseExpandableListAdapter extends BaseExpandableListAdapter {

    Context context;
    List<String> parentItems;
    Map<String, List<String>> childItems;
    private boolean isExpanded = false;

    public SeaLevelRiseExpandableListAdapter(Context context, List<String> parentItems, Map<String, List<String>> childItems) {
        this.context = context;
        this.parentItems = parentItems;
        this.childItems = childItems;
    }

    @Override
    public int getGroupCount() {
        return parentItems.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childItems.get(parentItems.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return parentItems.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childItems.get(parentItems.get(i)).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, final boolean isExpanded, View view, final ViewGroup viewGroup) {
        final String parent = (String) getGroup(i);

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.slr_parent, null);
        }

        TextView txtParent = view.findViewById(R.id.slr_title);
        txtParent.setText(parent);

        TextView txtSubParent = view.findViewById(R.id.slr_subtitle);
        if (parent.equals(SeaLevelRiseFragment.TOWNHALL_1) || parent.equals(SeaLevelRiseFragment.TOWNHALL_2)) {
            txtSubParent.setText(SeaLevelRiseFragment.SUBTITLE);
        } else {
            txtSubParent.setText("");
        }

        final ImageView indicator = view.findViewById(R.id.indicator);
        indicator.setImageResource(R.drawable.ic_plus_sign);

        //switch icon when expanded/closed
        if (isExpanded) {
            indicator.setImageResource(R.drawable.ic_minus);
        } else {
            indicator.setImageResource(R.drawable.ic_plus_sign);
        }

        return view;
    }

    @Override
    public View getChildView(final int parentIndex, final int childIndex, final boolean b, View view, final ViewGroup viewGroup) {
        final String child = (String) getChild(parentIndex, childIndex);

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.kc_accordion_child, null);
        }

        Html.ImageGetter imageGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                int resourceId = context.getResources().getIdentifier(source, "drawable", context.getPackageName());
                Drawable drawable = context.getResources().getDrawable(resourceId);

                DisplayMetrics metrics = new DisplayMetrics();
                metrics = Resources.getSystem().getDisplayMetrics();

                int width = (metrics.widthPixels * 70) / 100;

                int height = drawable.getIntrinsicHeight() * width
                        / drawable.getIntrinsicWidth();

                drawable.setBounds(0, 0, width, height);
                return drawable;
            }
        };

        TextView txtChild = view.findViewById(R.id.txtAccordionChild);

        Spanned html_str = Html.fromHtml(child, imageGetter, null);
        txtChild.setText(html_str);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String parent = (String) getGroup(parentIndex);

                // parent 1 and 2 have children with same names
                // need to differentiate between children to open corresponding links.
                switch (parent) {
                    // children of first parent
                    case SeaLevelRiseFragment.TOWNHALL_1:
                        switch (child) {
                            case SeaLevelRiseFragment.AGENDA:
                                launchActivity(SeaLevelRiseFragment.AGENDA_1_LINK);
                                break;
                            case SeaLevelRiseFragment.FACT_SHEET:
                                launchActivity(SeaLevelRiseFragment.FACT_SHEET_1_LINK);
                                break;
                            case SeaLevelRiseFragment.DRAFT_VULNERABILTY:
                                launchActivity(SeaLevelRiseFragment.DRAFT_VULNERABILTY_LINK);
                                break;
                            case SeaLevelRiseFragment.VIDEO_PART_1:
                                launchActivity(SeaLevelRiseFragment.VIDEO_PART_1_LINK);
                                break;
                            case SeaLevelRiseFragment.VIDEO_PART_2:
                                launchActivity(SeaLevelRiseFragment.VIDEO_PART_2_LINK);
                                break;
                            case SeaLevelRiseFragment.VIDEO_PART_3:
                                launchActivity(SeaLevelRiseFragment.VIDEO_PART_3_LINK);
                                break;
                        }
                        break;

                    // children of second parent
                    case SeaLevelRiseFragment.TOWNHALL_2:
                        switch (child) {
                            case SeaLevelRiseFragment.AGENDA:
                                launchActivity(SeaLevelRiseFragment.AGENDA_2_LINK);
                                break;
                            case SeaLevelRiseFragment.FACT_SHEET:
                                launchActivity(SeaLevelRiseFragment.FACT_SHEET_2_LINK);
                                break;
                            case SeaLevelRiseFragment.RESILIENT_CITIES:
                                launchActivity(SeaLevelRiseFragment.RESILIENT_CITIES_LINK);
                                break;
                            case SeaLevelRiseFragment.GREATER_MIAMI:
                                launchActivity(SeaLevelRiseFragment.GREATER_MIAMI_LINK);
                                break;
                            case SeaLevelRiseFragment.SLR_ADAPTATION:
                                launchActivity(SeaLevelRiseFragment.SLR_ADAPTATION_LINK);
                                break;
                            case SeaLevelRiseFragment.VULNERABILITY:
                                launchActivity(SeaLevelRiseFragment.VULNERABILITY_LINK_PDF);
                                break;
                            case SeaLevelRiseFragment.ADAPTATION:
                                launchActivity(SeaLevelRiseFragment.ADAPTATION_LINK_PPTX);
                                break;
                            case SeaLevelRiseFragment.COASTAL_RISK:
                                launchActivity(SeaLevelRiseFragment.COASTAL_RISK_LINK);
                                break;
                            case SeaLevelRiseFragment.VIDEOS:
                                launchActivity(SeaLevelRiseFragment.VIDEOS_LINK);
                                break;
                        }
                        break;

                    // children of third parent
                    case SeaLevelRiseFragment.WORKSHOP:
                        switch (child) {
                            case SeaLevelRiseFragment.PRESENTATION:
                                launchActivity(SeaLevelRiseFragment.PRESENTATION_LINK);
                                break;
                            case SeaLevelRiseFragment.WORKSHOP_SUMMARY:
                                launchActivity(SeaLevelRiseFragment.WORKSHOP_SUMMARY_LINK);
                                break;
                        }
                        break;
                }


            }
        });

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    // open content related to link
    private void launchActivity(String link) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

}

