package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.ComparisonCategory;
import com.cygnusa.hyperm.wrappers.ComparisonField;

import java.util.List;

public class ComparisionExpandableListAdapter extends BaseExpandableListAdapter {


  private Context _context;
  private List<ComparisonCategory> comparisonCategories;

  public ComparisionExpandableListAdapter(Context context, List<ComparisonCategory> comparisonCategories) {
    this._context = context;
    this.comparisonCategories = comparisonCategories;

  }

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

  @Override
  public int getChildrenCount(int groupPosition) {
    return comparisonCategories.get(groupPosition).getFields().size();
  }

  @Override
  public ComparisonCategory getGroup(int groupPosition) {
    return comparisonCategories.get(groupPosition);
  }

  @Override
  public ComparisonField getChild(int groupPosition, int childPosition) {
    return comparisonCategories.get(groupPosition).getFields().get(childPosition);
  }

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

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

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

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ComparisonCategory category = getGroup(groupPosition);
    ParentViewHolder holder;
    if (convertView == null) {
      LayoutInflater infalInflater = (LayoutInflater) this._context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = infalInflater.inflate(R.layout.list_header, null);
      holder = new ParentViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ParentViewHolder) convertView.getTag();
    }
    holder.list_header.setText(category.getTitle());
    return convertView;
  }

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

    ComparisonField field = getChild(groupPosition, childPosition);
    ChildViewHolder holder;
    if (convertView == null) {
      LayoutInflater infalInflater = (LayoutInflater) this._context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = infalInflater.inflate(R.layout.child_list_item, null);
      holder = new ChildViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ChildViewHolder) convertView.getTag();
    }
    holder.list_item_type.setText(field.getTitle());
    holder.list_item_value.setText(field.getValue());

    return convertView;
  }

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

  static class ParentViewHolder {
    TextView list_header;

    ParentViewHolder(View view) {
      list_header = view.findViewById(R.id.list_header);
    }
  }

  static class ChildViewHolder {
    TextView list_item_type, list_item_value;

    ChildViewHolder(View view) {
      list_item_type = view.findViewById(R.id.list_item_type);
      list_item_value = view.findViewById(R.id.list_item_value);
    }
  }
}
