package de.jg_cody.Teraplex;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.AlertDialog;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.fragment.app.DialogFragment;


public class AddButtonDialogSingle extends DialogFragment {
    private EditText editTextUsername;
    private EditText editTextPassword;

    private EditText command, name, button_name;
    private AddButtonDialogListener listener;

    /**
     *
     * @param savedInstanceState
     * @return
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.addbuttondialog_single, null);

        command = getActivity().findViewById(R.id.dialogSingle_editText_command);
        name = getActivity().findViewById(R.id.dialogSingle_editText_name);
        button_name = getActivity().findViewById(R.id.dialogSingle_editText_buttonName);


        builder.setView(view)
                .setTitle("SINGLE BUTTON")
                .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton("ADD", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String username = editTextUsername.getText().toString();
                        String password = editTextPassword.getText().toString();
                        listener.applyTexts(username, password);
                    }
                });


        AlertDialog mDialog = builder.create();
        mDialog.getWindow().setBackgroundDrawableResource(R.drawable.button_round);
        return mDialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (AddButtonDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() +
                    "must implement ExampleDialogListener");
        }
    }


    public interface AddButtonDialogListener {
        void applyTexts(String username, String password);
    }

    public String getName()
    {
        return name.getText().toString();
    }
    public String getButtonName()
    {
        return button_name.getText().toString();
    }
    public String getCommand()
    {
        return command.getText().toString();
    }
}