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 AddButtonsDialogDouble extends DialogFragment {
    private AddButtonsDialogListenerDouble listener;

    private EditText editText_name, editText_command1, editText_button1, editText_command2, editText_button2;
    private TabsFragment frag;

    @NotNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.addbuttonsdialog_double, null);

        builder.setView(view)
                .setTitle("ON/OFF BUTTON")
                .setNegativeButton(getString(R.string.cancel), null)
                .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String command1 = editText_command1.getText().toString();
                        String command2 = editText_command2.getText().toString();
                        String name = editText_name.getText().toString();
                        String button1 = editText_button1.getText().toString();
                        String button2 = editText_button2.getText().toString();
                        if(command1.isEmpty()||command2.isEmpty()||name.isEmpty()||button1.isEmpty()||button2.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.applyTextsDouble(name,command1,command2,button1,button2);
                    }
                });

        editText_name = view.findViewById(R.id.dialogDouble_editText_name);
        editText_command1 = view.findViewById(R.id.dialogDouble_editText_command1);
        editText_command2 = view.findViewById(R.id.dialogDouble_editText_command2);
        editText_button1 = view.findViewById(R.id.dialogDouble_editText_button1);
        editText_button2 = view.findViewById(R.id.dialogDouble_editText_button2);

        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 = (AddButtonsDialogListenerDouble) frag;
        } catch (ClassCastException ignored) {

        }
    }

    public interface AddButtonsDialogListenerDouble {
        void applyTextsDouble(String name, String command1, String command2, String button1, String button2);
    }
}

