package de.jg_cody.Teraplex.ui.Konsole;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import de.jg_cody.Teraplex.R;
import de.jg_cody.Teraplex.SSH_connection;

import static de.jg_cody.Teraplex.ui.home.HomeFragment.ip;
import static de.jg_cody.Teraplex.ui.home.HomeFragment.password;
import static de.jg_cody.Teraplex.ui.home.HomeFragment.user;

public class KonsoleFragment extends Fragment {

    public static String command;

    public static EditText befehlInput;
    Button SendCommandButton;
    Button shutdown_Button;
    Button reboot_Button;

    private KonsoleViewModel konsoleViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        konsoleViewModel =
                new ViewModelProvider(this).get(KonsoleViewModel.class);
        View root = inflater.inflate(R.layout.fragment_konsole, container, false);
        final TextView textView = root.findViewById(R.id.text_konsole);
        konsoleViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        befehlInput = (EditText) root.findViewById(R.id.befehlInput);
        SendCommandButton = (Button) root.findViewById(R.id.SendCommandButton);
        SendCommandButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (befehlInput.getText().toString().trim().length() == 0) {
                        Toast.makeText(getContext(), "Feld darf nicht leer sein!", Toast.LENGTH_SHORT).show();
                    } else {
                        command = befehlInput.getText().toString();
                        Toast.makeText(getContext(), "Kommando gesendet...", Toast.LENGTH_SHORT).show();
                        Toast.makeText(root.getContext(), command, Toast.LENGTH_SHORT).show();
                        SSH_connection.executeRemoteCommand(ip, user, password, command);
                    }
                } catch (Exception e) {
                }
            }

        });
        reboot_Button = (Button) root.findViewById(R.id.reboot);
        reboot_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Reboot wird ausgeführt...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "reboot");
                } catch (Exception e) {
                }
            }
        });
        shutdown_Button = (Button) root.findViewById(R.id.shutdown);
        shutdown_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                new AlertDialog.Builder(getContext())
                        .setTitle("Server herunterfahren")
                        .setMessage("Bist du sicher?")

                        // 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, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    Toast.makeText(getContext(), "Server wird heruntergefahren...", Toast.LENGTH_SHORT).show();
                                    SSH_connection.executeRemoteCommand(ip, user, password, "shutdown now");
                                } catch (Exception e) {
                                }

                                // Continue with delete operation
                            }
                        })

                        // A null listener allows the button to dismiss the dialog and take no further action.
                        .setNegativeButton(android.R.string.no, null)
                        .show();

            }
        });
        return root;
    }
}