package xn.hxp.weidith;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import xn.hxp.R;
import xn.hxp.databinding.DcaeDialogBinding;
import xn.hxp.ui.adapter.SelectChemicalDialogAdapter;

import com.blankj.utilcode.util.ClickUtils;
import com.rc.httpcore.bean.HxpChemicalVo;

import java.util.List;

public class SelectChemicalDialog extends Dialog {
    private DcaeDialogBinding binding;
    private Context context;
    private SelectChemicalDialogAdapter selectChemicalDialogAdapter;
    private List<HxpChemicalVo> hxpChemicalVoList;
    private SelectedListener selectedListener;

    public SelectChemicalDialog(@NonNull Context context, List<HxpChemicalVo> hxpChemicalVoList, SelectedListener selectedListener) {
        super(context);
        this.context = context;
        this.hxpChemicalVoList = hxpChemicalVoList;
        this.selectedListener = selectedListener;
    }

    public interface SelectedListener {
        void selected(HxpChemicalVo hxpChemicalVo);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DcaeDialogBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(false);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        binding.rvView.setLayoutManager(layoutManager);
        selectChemicalDialogAdapter = new SelectChemicalDialogAdapter(R.layout.chemical_search_rv, hxpChemicalVoList);
        binding.rvView.setAdapter(selectChemicalDialogAdapter);

        selectChemicalDialogAdapter.setOnItemClickListener((adapter, view, position) -> {
            selectedListener.selected(selectChemicalDialogAdapter.getData().get(position));
            dismiss();
        });

        binding.butClos.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                dismiss();
            }
        });
    }
}
