package xn.hxp.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ClickUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import xn.hxp.R;
import xn.hxp.utils.ConversionUtils;
import xn.hxp.weidith.TextTool;

import com.rc.httpcore.bean.ChemistryBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UsageLabelDialogAdapter extends BaseQuickAdapter<ChemistryBean, BaseViewHolder> {
    private final Context ct;
    private final List<String> spinnerArray = new ArrayList<>();

    public UsageLabelDialogAdapter(Context context) {
        super(R.layout.item_usage_label_dialog);
        this.ct = context;
        addChildClickViewIds(R.id.imgDelete);
        spinnerArray.add("否");
        spinnerArray.add("是");
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ChemistryBean item) {
        holder.setText(R.id.chemicalName, item.getChemicalName() != null ? item.getChemicalName() : "");
        TextView chemicalName = holder.getView(R.id.chemicalName);
        chemicalName.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                TextTool.showPop(ct, chemicalName);
            }
        });
        holder.setText(R.id.code, item.getCode() != null ? item.getCode() : "");
        TextView codeTV = holder.getView(R.id.code);
        codeTV.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                TextTool.showPop(ct, codeTV);
            }
        });

        if (item.getWeigh() != null) {
            if ("g".equals(item.getSpecUnit())) {
                holder.setText(R.id.surplus, item.getWeigh().subtract(item.getPackNum()) + item.getSpecUnit());
            } else if ("ml".equals(item.getSpecUnit())) {
                String gramsToMilliliters = String.valueOf(ConversionUtils.INSTANCE.gramsToMilliliters(item.getWeigh().subtract(item.getPackNum()), item.getChemicalDensity()));
                holder.setText(R.id.surplus, gramsToMilliliters + item.getSpecUnit());
            } else {
                holder.setText(R.id.surplus, (item.getSurplus() != null ? item.getSurplus() : "") + item.getSpecUnit());
            }
        } else {
            holder.setText(R.id.surplus, (item.getSurplus() != null ? item.getSurplus() : "") + item.getSpecUnit());
        }

        holder.setText(R.id.spec, item.getSpec() != null ? item.getSpec() : "");
        holder.setText(R.id.duration, item.getDuration() != null ? item.getDuration() : "");

        Spinner sPinner = holder.getView(R.id.sPinner);
        CustomSpinnerFoutAdapter adapter = new CustomSpinnerFoutAdapter(ct, spinnerArray);
        sPinner.setAdapter(adapter);
        sPinner.setSelection(0);
        sPinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = parent.getItemAtPosition(position).toString();
                item.setAllBottle(!"否".equals(selectedItem));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 处理未选择任何选项情况
            }
        });
    }
}