package com.example.onetap_ssh;

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 org.jetbrains.annotations.NotNull;

import java.util.Objects;

import com.example.onetap_ssh.ui.tabs.TabsFragment;

public class AddHeadlineDialog extends DialogFragment {
    private AddHeadlineDialogListener listener;

    private EditText editText_name;
    private TabsFragment frag;

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

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

        builder.setView(view)
                .setTitle(R.string.heading)
                .setNegativeButton(R.string.cancel, (dialogInterface, i) -> {

                })
                .setPositiveButton(R.string.add, (dialogInterface, i) -> {
                    String headline = editText_name.getText().toString();
                    if(headline.isEmpty()){

                        AlertDialog mDialog = new AlertDialog.Builder(getContext())
                                .setTitle(getString(R.string.invalid))
                                .setMessage(getString(R.string.inputfields_cant_be_empty))

                                // Specifying a listener allows you to take an action before dismissing the dialog.
                                // The dialog is automatically dismissed when a dialog button is clicked.
                                .setPositiveButton(android.R.string.yes, null )
                                .create();
                        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                        mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
                        mDialog.show();
                        return;
                    }
                    listener.applyTextsHeadline(headline);
                });


        AlertDialog mDialog = builder.create();
        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
        mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
        return mDialog;
    }

    public void setFragment(TabsFragment frag)
    {
        this.frag = frag;
    }

    @Override
    public void onAttach(@NotNull 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);
    }

}