package xn.hxp.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.ClickUtils;
import com.blankj.utilcode.util.LogUtils;

import xn.hxp.databinding.ItemInStorageBinding;
import xn.hxp.ui.plan.room.RoomTool;
import xn.hxp.ui.plan.room.dao.HxpInventoryDAO;

import com.rc.httpcore.bean.HxpChemicalVo;

import xn.hxp.ui.plan.room.bean.input_add.InventoryItemBean;
import xn.hxp.weidith.TextTool;

import java.util.List;

public class PlanAdapter extends BaseAdapter {

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

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

    @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);
            binding.itemDelIV.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    InventoryItemBean inventoryItemBean = inventoryItemBeanList.get(position);
                    for (int i = 0; i < inventoryItemBeanList.size(); i++) {
                        InventoryItemBean inventoryItem = inventoryItemBeanList.get(i);
                        if (inventoryItem.getId() == inventoryItemBean.getId()) {
                            inventoryItemBeanList.remove(i);
                            hxpInventoryDAO.delete(inventoryItemBean.getId());
                            notifyDataSetChanged();
                            return;
                        }
                    }
                }
            });
        } 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);
            binding.itemNameTV.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    TextTool.showPop(context, binding.itemNameTV);
                }
            });
            // cas
            String casNum = hxpChemicalVo.getCasNum();
            binding.casTV.setText(TextUtils.isEmpty(casNum) ? "" : casNum);
            // 化学品类型
            String chemicalCategoryName = hxpChemicalVo.getChemicalCategoryName();
            binding.itemTypeTV.setText(TextUtils.isEmpty(chemicalCategoryName) ? "" : chemicalCategoryName);
            // 管控非管控
            int chemicalLevel = hxpChemicalVo.getChemicalLevel();
            binding.itemLevelTV.setText(chemicalLevel == 1 ? "管控" : "非管控");
            // 规格
            double specNum = hxpChemicalVo.getSpecNum();
            String specUnit = hxpChemicalVo.getSpecUnit();
            binding.itemNormsTV.setText(specNum + (TextUtils.isEmpty(specUnit) ? "" : specUnit));
            // 净含量
            double netWt = hxpChemicalVo.getNetContent();
            binding.itemNetWtTV.setText(netWt + (TextUtils.isEmpty(specUnit) ? "" : specUnit));
            // 数量
            int packNum = inventoryItemBean.getSize();
            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;
        }
    }
}
