package com.example.moslah_hamza.statsdemo;

/**
 * Created by Moslah_Hamza on 29/04/2016.
 */

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

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

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private SparseBooleanArray selectedItems;
    private Context _context;
    private List<String> _listDataHeader=new ArrayList<String>(); // header titles
    // child data in format of header title, child title
    // private HashMap<String, List<String>> _listDataChild;
    private List<Float> sommes=new ArrayList<Float>();
    private float sum;
    private List<View> views;
    private List<String> categories;
    private Context context;
    int screenWidth,screenHeight,px;

    public List<View> getViews() {
        return views;
    }

    public void setViews(List<View> views) {
        this.views = views;
    }

    public void toggleSelection(int pos) {
        if (selectedItems.get(pos, false)) {
            selectedItems.delete(pos);
        }
        else {
            selectedItems.put(pos, true);
        }
        //notifyItemChanged(pos);
    }

    public void clearSelections() {
        selectedItems.clear();
        notifyDataSetChanged();
    }

    public int getSelectedItemCount() {
        return selectedItems.size();
    }

    public List<Integer> getSelectedItems() {
        List<Integer> items =
                new ArrayList<Integer>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); i++) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }

    public ExpandableListAdapter(String genre,float sum,List<Float> sommes,List<String> categories,
                                 Context context,int screenWidth,int screenHeight,int px) {
        this._context = context;
        this._listDataHeader.add(genre);
        //this._listDataChild = listChildData;
        if(!this.sommes.isEmpty())
        this.sommes.clear();
        this.sommes = sommes;
        this.sum = sum;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        this.px=px;
        this.categories = categories;
        this.context=context;
        this.views=new ArrayList<View>();
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.categories
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.single_item, null);
        }


        TextView textView1 = (TextView) convertView.findViewById(R.id.cat);
        View stat = convertView.findViewById(R.id.statsview);
        TextView textView2 = (TextView) convertView.findViewById(R.id.percent);

        textView1.setText(categories.get(childPosition));
        textView2.setText(String.valueOf(sommes.get(childPosition)) + "%");

        int width= Math.round(screenWidth * (sommes.get(childPosition)) / 100);
        Log.d("Width 1 ", String.valueOf(sommes.get(childPosition)));
        stat.setLayoutParams(new LinearLayout.LayoutParams(width, px));
        stat.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.categories
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        TextView sumtot = (TextView) convertView
                .findViewById(R.id.sum_tot);
        sumtot.setTypeface(null, Typeface.BOLD);
        sumtot.setText(String.valueOf(sum)+" DT");
        if(headerTitle.equals("Dépenses")){
            sumtot.setTextColor(context.getResources().getColor(R.color.red));
        }else {
            sumtot.setTextColor(context.getResources().getColor(R.color.green));
        }

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}