package com.example.chemical.ui.plan.unlock;

import android.content.Context;
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.example.chemical.R;
import com.example.chemical.databinding.ItemDoorUnlockBinding;
import com.example.chemical.ui.plan.room.RoomTool;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetVo;
import com.example.chemical.ui.plan.room.dao.HxpDoorDAO;

import java.util.List;

public class UnlockAdapter extends BaseAdapter {
    List<HxpCabinetVo> cabinetList;
    List<HxpCabinetDoorVo> doorList;
    private Context context;
    private HxpDoorDAO doorDAO;

    public UnlockAdapter(Context context, List<HxpCabinetVo> cabinetList, List<HxpCabinetDoorVo> doorList) {
        this.cabinetList = cabinetList;
        this.doorList = doorList;
        this.context = context;
        doorDAO = RoomTool.getInstance().hxpDoorDAO();
    }

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

    @Override
    public HxpCabinetVo getItem(int position) {
        return null == cabinetList ? null : cabinetList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemDoorUnlockBinding binding = ItemDoorUnlockBinding.inflate(LayoutInflater.from(context), parent, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
            // 上门
            binding.upDoorUnlockBT.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            // 下门
            binding.downDoorUnlockBT.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        HxpCabinetVo cabinet = getItem(position);
        if (null != cabinet) {
            try {
                String cabinetName = cabinet.getCabinetName();
                viewHolder.binding.cabinetNameTV.setText(TextUtils.isEmpty(cabinetName) ? "" : cabinetName);

                List<HxpCabinetDoorVo> hxpCabinetDoorVoList = doorDAO.getByCabinetId(cabinet.getCabinetId());
                int doorNum = cabinet.getDoorNum();
                HxpCabinetDoorVo upDoor = hxpCabinetDoorVoList.get(0);
                String doorName = upDoor.getDoorName();
                viewHolder.binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);
                if (doorNum > 1) {
                    HxpCabinetDoorVo downDoor = hxpCabinetDoorVoList.get(1);
                    String downDoorName = downDoor.getDoorName();
                    viewHolder.binding.downDoorNameTV.setText(TextUtils.isEmpty(downDoorName) ? "" : downDoorName);
                    viewHolder.binding.downDoorNameTV.setVisibility(View.VISIBLE);
                    viewHolder.binding.cabinetLL.setBackgroundResource(R.mipmap.img_skm);
                }
            } catch (Exception e) {
                LogUtils.e(Log.getStackTraceString(e));
            }
        }

        return convertView;
    }

    static class ViewHolder {
        ItemDoorUnlockBinding binding;

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