package xn.xxp.main.things;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import http.vo.response.LotDeviceVo;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
import xn.xxp.R;
import xn.xxp.databinding.ItemIotThingsBinding;

public class ThingsListAdapter extends BaseAdapter {
    private final List<LotDeviceVo> lotDeviceVoList;

    public ThingsListAdapter(List<LotDeviceVo> lotDeviceVoList) {
        this.lotDeviceVoList = lotDeviceVoList;
    }

    @Override
    public int getCount() {
        return null != lotDeviceVoList && !lotDeviceVoList.isEmpty() ? lotDeviceVoList.size() : 0;
    }

    @Override
    public LotDeviceVo getItem(int position) {
        return null != lotDeviceVoList && !lotDeviceVoList.isEmpty() ? lotDeviceVoList.get(position) : null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemIotThingsBinding binding = ItemIotThingsBinding.inflate(LayoutInflater.from(parent.getContext()));
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LotDeviceVo lotDeviceVo = getItem(position);
        if (null != lotDeviceVo) {
            boolean isPowerLot = lotDeviceVo.hardwareName.contains("电源");
            int resId = isPowerLot ? R.mipmap.icon_dykz : R.mipmap.icon_dspf_fs;
            viewHolder.binding.lotController.setLotInfo(lotDeviceVo.hardwareName, resId);
            viewHolder.binding.lotController.controller(lotDeviceVo.isSwitchOn(), false, null);
        }

        return convertView;
    }

    static class ViewHolder {
        private final ItemIotThingsBinding binding;

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