package de.jg_cody.Teraplex;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
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 AddHeadlineDialog extends DialogFragment {
    private AddHeadlineDialogListener listener;

    private EditText editText_name;
    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.addheadlinedialog, null);

        editText_name = view.findViewById(R.id.dialogheadline_editText_name);


        builder.setView(view)
                .setTitle("SINGLE BUTTON")
                .setNegativeButton("CANCEL", (dialogInterface, i) -> {

                })
                .setPositiveButton("ADD", (dialogInterface, i) -> {
                    String headline = editText_name.getText().toString();
                    listener.applyTextsHeadline(headline);
                });


        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 = (AddHeadlineDialogListener) frag;
            } catch (ClassCastException e) {
            throw new ClassCastException(frag.toString() +
                    "must implement ExampleDialogListener");
            }
        }



    public interface AddHeadlineDialogListener {
        void applyTextsHeadline(String headline);
    }



}