package xn.xxp.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import java.util.List;

import http.vo.response.HazardBook;
import xn.xxp.databinding.SelectDialogBinding;


public class SelectHazardBookDialog extends Dialog {
    private Context context;
    private SelectDialogBinding binding;
    private List<HazardBook> hazardBookList;
    private SelectedListener selectedListener;
    private String dialogName;

    public SelectHazardBookDialog(@NonNull Context context, String dialogName, List<HazardBook> hazardBookList, SelectedListener selectedListener) {
        super(context);
        this.context = context;
        this.hazardBookList = hazardBookList;
        this.selectedListener = selectedListener;
        this.dialogName = dialogName;
    }

    public interface SelectedListener {
        void selected(HazardBook hazardBook);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = SelectDialogBinding.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);

        binding.dialogName.setText(TextUtils.isEmpty(dialogName) ? "" : dialogName);
        if (null != hazardBookList) {
            String[] hazardBookArray = new String[hazardBookList.size()];
            for (int i = 0; i < hazardBookList.size(); i++) {
                HazardBook hazardBook = hazardBookList.get(i);
                hazardBookArray[i] = hazardBook.name;
            }
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, hazardBookArray);
            binding.listView.setAdapter(arrayAdapter);
            binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    HazardBook hazardBook = hazardBookList.get(position);
                    selectedListener.selected(hazardBook);
                    dismiss();
                }
            });
        }

        binding.close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }
}
