package org.keyscience.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
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.TextView;
import org.keyscience.R;

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

public class AccordionListAdapter extends BaseExpandableListAdapter {

    Context context;
    List<String> parentItems;
    Map<String, List<String>> childItems;

    public AccordionListAdapter(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 b, 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.kc_accordion_parent, null);
        }

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

        return view;
    }

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

        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);

        return view;
    }

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