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;

import de.jg_cody.Teraplex.ui.Flur.FlurFragment;

public class AddButtonDialogSingle extends DialogFragment {
    private EditText editText_command, editText_name, editText_button_name;
    private AddButtonDialogListenerSingle listener;
    private FlurFragment frag;

    /**
     *
     * @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);

        editText_command = view.findViewById(R.id.dialogSingle_editText_command);
        editText_name = view.findViewById(R.id.dialogheadline_editText_name);
        editText_button_name = view.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 command = editText_command.getText().toString();
                        String name = editText_name.getText().toString();
                        String button = editText_button_name.getText().toString();
                        listener.applyTextsSingle(name,command,button);
                    }
                });


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

    public void setFragment(FlurFragment frag)
    {
        this.frag = frag;
    }
    public FlurFragment getFragment()
    {
        return frag;
    }

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

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



    public interface AddButtonDialogListenerSingle {
        void applyTextsSingle(String name, String command, String button);
    }



}