package com.example.onetap_ssh;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
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 AddButtonDialogSingle extends DialogFragment {
    private EditText editText_command, editText_name, editText_button_name;
    private AddButtonDialogListenerSingle listener;
    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.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(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton(R.string.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();
                        if(command.isEmpty()||button.isEmpty()||name.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.applyTextsSingle(name,command,button);
                    }
                });


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

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

}