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.ImageView;
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 WorkerAdapter extends BaseExpandableListAdapter {

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

    public WorkerAdapter(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_online, parent, false);
            itemHolder = new ChildViewHolder();
            itemHolder.personStatusView = convertView.findViewById(R.id.personStatusView);
            itemHolder.childNameView = convertView.findViewById(R.id.childNameView);
            itemHolder.onlineTimeView = convertView.findViewById(R.id.onlineTimeView);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ChildViewHolder) convertView.getTag();
        }
        boolean onLine = childData.get(groupPosition).get(childPosition).isOnLine();
        if (onLine) {
            itemHolder.personStatusView.setBackgroundResource(R.drawable.ic_online);
        } else {
            itemHolder.personStatusView.setBackgroundResource(R.drawable.ic_offline);
        }
        itemHolder.childNameView.setText(childData.get(groupPosition).get(childPosition).getPersonName());
        itemHolder.onlineTimeView.setText(childData.get(groupPosition).get(childPosition).getLastTimeFmt());
        return convertView;
    }

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

    private static class GroupViewHolder {
        private TextView groupNameView;
    }

    private static class ChildViewHolder {
        private ImageView personStatusView;
        private TextView childNameView;
        private TextView onlineTimeView;
    }
}
