package xn.xxp.home.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;

import java.util.List;

import http.vo.response.LabPersonVo;
import xn.xxp.R;
import xn.xxp.databinding.ItemHomePersonFlipperBinding;

public class PeopleFlipperAdapter extends BaseAdapter {
    private final int type;
    private final List<LabPersonVo> labPersonVoList;

    public PeopleFlipperAdapter(int type, List<LabPersonVo> labPersonVoList) {
        this.type = type;
        this.labPersonVoList = labPersonVoList;
    }

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

    @Override
    public LabPersonVo getItem(int position) {
        return null == labPersonVoList || labPersonVoList.isEmpty() ? null : labPersonVoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemHomePersonFlipperBinding binding = ItemHomePersonFlipperBinding.inflate(LayoutInflater.from(parent.getContext()));
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LabPersonVo labPersonVo = getItem(position);
        if (null != labPersonVo) {
            ItemHomePersonFlipperBinding binding = viewHolder.binding;
            if (!TextUtils.isEmpty(labPersonVo.avatar)) {
                Glide.with(binding.pPhoto)
                        .load(labPersonVo.avatar)
                        .placeholder(R.mipmap.icon_sign_in_avatar)
                        .error(R.mipmap.icon_sign_in_avatar)
                        .into(binding.pPhoto);
            }
            binding.pName.setText(TextUtils.isEmpty(labPersonVo.userName) ? "" : labPersonVo.userName);
            // 值班人员
            if (type == 1) {
                binding.pCardNumber.setText(TextUtils.isEmpty(labPersonVo.userPhone) ? "" : labPersonVo.userPhone);
            }
            // 实验室人员
            else if (type == 2) {
                try {
                    String signTime = labPersonVo.signTime;
                    if (!TextUtils.isEmpty(signTime)) {
                        signTime = signTime.substring(5, 16);
                        binding.pCardNumber.setText("签到时间:" + signTime);
                    }
                } catch (Exception e) {
                    LogUtils.e(Log.getStackTraceString(e));
                }
            }
            // 准入人员
            else if (type == 3) {
                try {
                    String validEndTime = labPersonVo.validEndTime;
                    if (!TextUtils.isEmpty(validEndTime)) {
                        validEndTime = validEndTime.substring(2);
                        binding.pCardNumber.setText("准入期限:" + validEndTime);
                    }
                } catch (Exception e) {
                    LogUtils.e(Log.getStackTraceString(e));
                }
            }
        }
        return convertView;
    }

    static class ViewHolder {
        private final ItemHomePersonFlipperBinding binding;

        public ViewHolder(ItemHomePersonFlipperBinding binding) {
            this.binding = binding;
        }
    }
}
