package org.keyscience.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.util.DisplayMetrics;
import android.util.Log;
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 ChartsAccordionAdapter extends BaseExpandableListAdapter {

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

    TextView txtChild;

    public ChartsAccordionAdapter(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) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.ww_accordion_child, null);
        }

        try {
            final String child = (String) getChild(i, i1);

            Html.ImageGetter imageGetter = new Html.ImageGetter() {
                @Override
                public Drawable getDrawable(final String source) {

                    int resourceId = context.getResources().getIdentifier(source, "drawable", context.getPackageName());

                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
                    int width = 400;
                    int height = (bitmap.getHeight() * width / bitmap.getWidth());
                    Bitmap scale = Bitmap.createScaledBitmap(bitmap, width, height, true);

                    Drawable drawable = new BitmapDrawable(context.getResources(), scale);

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

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

                    int dheight = drawable.getIntrinsicHeight() * dwidth
                            / drawable.getIntrinsicWidth();

                    drawable.setBounds(0, 0, dwidth, dheight);
                    return drawable;
                }
            };

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

            Spanned html_str = Html.fromHtml(child, imageGetter, null);
            txtChild.setText(html_str);
        } catch (OutOfMemoryError outOfMemoryError) {

            // If out of memory, downscale images and display them. Should work for older devices.
            final String child = (String) getChild(i, i1);

            Html.ImageGetter imageGetter = new Html.ImageGetter() {
                @Override
                public Drawable getDrawable(final String source) {

                    int resourceId = context.getResources().getIdentifier(source, "drawable", context.getPackageName());

                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
                    int width = 200;
                    int height = (bitmap.getHeight() * width / bitmap.getWidth());
                    Bitmap scale = Bitmap.createScaledBitmap(bitmap, width, height, true);

                    Drawable drawable = new BitmapDrawable(context.getResources(), scale);

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

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

                    int dheight = drawable.getIntrinsicHeight() * dwidth
                            / drawable.getIntrinsicWidth();

                    drawable.setBounds(0, 0, dwidth, dheight);
                    return drawable;
                }
            };

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