package com.example.chemical.ui.plan;

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.databinding.ItemInStorageBinding;
import com.rc.httpcore.bean.HxpChemicalVo;
import com.example.chemical.ui.plan.room.bean.input_add.InventoryItemBean;

import java.util.List;

public class PlanAdapter extends BaseAdapter {

    private List<InventoryItemBean> inventoryItemBeanList;
    private final Context context;

    public PlanAdapter(Context context, List<InventoryItemBean> inventoryItemBeanList) {
        this.inventoryItemBeanList = inventoryItemBeanList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return inventoryItemBeanList.size();
    }

    @Override
    public InventoryItemBean getItem(int position) {
        return inventoryItemBeanList.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) {
            ItemInStorageBinding binding = ItemInStorageBinding.inflate(LayoutInflater.from(context), parent, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        try {
            InventoryItemBean inventoryItemBean = getItem(position);
            HxpChemicalVo hxpChemicalVo = inventoryItemBean.getHxpChemicalVo();
            ItemInStorageBinding binding = viewHolder.binding;
            // 化学品名字
            String chemicalName = hxpChemicalVo.getChemicalName();
            binding.itemNameTV.setText(TextUtils.isEmpty(chemicalName) ? "" : chemicalName);
            // cas
            String casNum = hxpChemicalVo.getCasNum();
            binding.casTV.setText(TextUtils.isEmpty(casNum) ? "" : casNum);
            // 化学品类型
            String chemicalCategoryName = hxpChemicalVo.getChemicalCategoryName();
            binding.itemTypeTV.setText(TextUtils.isEmpty(chemicalCategoryName) ? "" : chemicalCategoryName);
            // 管控非管控
            String chemicalLevelName = hxpChemicalVo.getChemicalLevelName();
            binding.itemLevelTV.setText(TextUtils.isEmpty(chemicalLevelName) ? "" : chemicalLevelName);
            // 纯度
            String purity = hxpChemicalVo.getChemicalPurity();
            binding.itemNormsTV.setText(TextUtils.isEmpty(purity) ? "" : purity);
            // 净含量
            double specNum = 0;
            binding.itemNetWtTV.setText(TextUtils.isEmpty(String.valueOf(specNum)) ? "" : String.valueOf(specNum));
            // 数量
            double packNum = 0;
            binding.itemNumTV.setText(TextUtils.isEmpty(String.valueOf(packNum)) ? "" : String.valueOf(packNum));
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        return convertView;
    }

    static class ViewHolder {
        ItemInStorageBinding binding;

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