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.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 xn.hxp.databinding.AlreadyDialogBinding;

public class SelectAlreadyDialog extends Dialog {
    private Context context;
    private AlreadyDialogBinding binding;
    private List<String> alreadyList;
    private SelectedListener selectedListener;
    private String dialogName;

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

    public interface SelectedListener {
        void selected(String string);

        void clear();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = AlreadyDialogBinding.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 != alreadyList) {
            String[] alreadyListArray = alreadyList.toArray(new String[0]);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, alreadyListArray);
            binding.listView.setAdapter(arrayAdapter);
            binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String selectedItem = (String) parent.getItemAtPosition(position);
                    selectedListener.selected(selectedItem);
                    dismiss();
                }
            });
        }

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

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

    }
}
