package de.jg_cody.Teraplex.ui.Konsole;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Build;
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.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import java.util.Base64;
import java.util.Objects;

import de.jg_cody.Teraplex.MainActivity;
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;

    @RequiresApi(api = Build.VERSION_CODES.O)
    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);
        SharedPreferences p = requireContext().getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
        String Background = p.getString("Background", null);
        if (Background != null) {
            ImageView I = root.findViewById(R.id.Background);
            byte[] BA = Base64.getDecoder().decode(Background);
            I.setImageDrawable(new BitmapDrawable(getResources(), MainActivity.scaleCenterCrop(BitmapFactory.decodeByteArray(BA, 0, BA.length), MainActivity.getScreenHeight(), MainActivity.getScreenWidth())));
            I.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
        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 ignored) {
                }
            }

        });
        reboot_Button = (Button) root.findViewById(R.id.reboot);
        reboot_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.state_change_confirm_up);
                mp.start();
                AlertDialog mDialog = new AlertDialog.Builder(getContext())
                        .setTitle("SERVER NEUSTARTEN")
                        .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 {
                                    MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.state_change_confirm_down);
                                    mp.start();
                                    Toast.makeText(getContext(), "Reboot wird ausgeführt...", Toast.LENGTH_SHORT).show();
                                    SSH_connection.executeRemoteCommand(ip, user, password, "reboot");
                                } 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)
                        .create();
                Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                mDialog.show();




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

                MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.state_change_confirm_up);
                mp.start();
                AlertDialog mDialog = 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 {
                                    MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.state_change_confirm_down);
                                    mp.start();
                                    Toast.makeText(getContext(), "Server wird heruntergefahren...", Toast.LENGTH_SHORT).show();
                                    SSH_connection.executeRemoteCommand(ip, user, password, "shutdown now");
                                } catch (Exception ignored) {
                                }

                                // 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)
                        .create();
                Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                mDialog.show();

            }
        });
        return root;
    }
}