package com.rehome.sgbaxj.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.rehome.sgbaxj.DBModel.WjbjxitemList;
import com.rehome.sgbaxj.DBModel.WjbzjdList;
import com.rehome.sgbaxj.R;

import java.util.List;

/**
 * author: Johnny Chen
 * Date: 2018/7/28
 */
public class WjDetailsListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<WjbzjdList> wjbzjdListBeans;


    public WjDetailsListAdapter(Context mContext, List<WjbzjdList> wjbzjdListBeans) {
        this.mContext = mContext;
        this.wjbzjdListBeans = wjbzjdListBeans;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getGroupCount() {
        return wjbzjdListBeans == null ? 0 : wjbzjdListBeans.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return wjbzjdListBeans.get(groupPosition).getJxitem().size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return wjbzjdListBeans.get(groupPosition).getJxitem().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) {

        GroupHolder groupHolder;
        if (convertView == null) {
            groupHolder = new GroupHolder();
            convertView = mInflater.inflate(R.layout.item_group_wj_details, null);
            groupHolder.WjDetailsItemGroupTv = convertView.findViewById(R.id.WjDetails_item_group_tv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.WjDetailsItemGroupTv.setText(wjbzjdListBeans.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder;
        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = mInflater.inflate(R.layout.item_child_wj_details, null);
            childHolder.WjDetailsItemChildIndexTv = convertView.findViewById(R.id.WjDetails_item_child_index_tv);
            childHolder.WjDetailsItemChildContentTv = convertView.findViewById(R.id.WjDetails_item_child_content_tv);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        List<WjbjxitemList> wjbjxitemListBeans = wjbzjdListBeans.get(groupPosition).getJxitem();

        childHolder.WjDetailsItemChildIndexTv.setText(childPosition + 1 + ":");
        childHolder.WjDetailsItemChildContentTv.setText(wjbjxitemListBeans.get(childPosition).getItemname());
        return convertView;
    }

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

    class GroupHolder {
        TextView WjDetailsItemGroupTv;
    }

    class ChildHolder {
        TextView WjDetailsItemChildIndexTv;
        TextView WjDetailsItemChildContentTv;
    }
}