package com.casic.dcms.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 androidx.annotation.Nullable;

import com.casic.dcms.R;
import com.casic.dcms.model.PersonBean;

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

/**
 * @author : Pengxh
 * @time : 2021/4/15 13:11
 * @email : 290677893@qq.com
 **/
public class DeptExpandableAdapter extends BaseExpandableListAdapter {

    private final LayoutInflater layoutInflater;
    private List<String> groupData = new ArrayList<>();
    private List<List<PersonBean>> childData = new ArrayList<>();

    public DeptExpandableAdapter(Context context) {
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setData(@Nullable List<String> groupList, @Nullable List<List<PersonBean>> childList) {
        groupData.clear();
        childData.clear();
        if (groupList != null) {
            this.groupData = groupList;
            if (childList != null) {
                this.childData = childList;
            }
        }
        notifyDataSetChanged();
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).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) {
        GroupViewHolder groupHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_group_list, parent, false);
            groupHolder = new GroupViewHolder();
            groupHolder.groupNameView = convertView.findViewById(R.id.groupNameView);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupViewHolder) convertView.getTag();
        }
        groupHolder.groupNameView.setText(groupData.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder itemHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_child_list, parent, false);
            itemHolder = new ChildViewHolder();
            itemHolder.childNameView = convertView.findViewById(R.id.childNameView);
            itemHolder.childPhoneView = convertView.findViewById(R.id.childPhoneView);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ChildViewHolder) convertView.getTag();
        }
        itemHolder.childNameView.setText(childData.get(groupPosition).get(childPosition).getPersonName());
        itemHolder.childPhoneView.setText(childData.get(groupPosition).get(childPosition).getPersonPhone());
        return convertView;
    }

    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class GroupViewHolder {
        private TextView groupNameView;
    }

    private static class ChildViewHolder {
        private TextView childNameView;
        private TextView childPhoneView;
    }
}
