package com.example.onetap_ssh.ui.tabs;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.view.View;

import com.example.onetap_ssh.MainActivity;
import com.example.onetap_ssh.SSH_connection;

public class CommandExecutor implements View.OnClickListener {

    private final String command;
    public CommandExecutor(String command) {
        this.command = command;
    }
    @Override
    public void onClick(View v) {
        if (MainActivity.button_vibration) {
            Vibrator vr = (Vibrator) v.getContext().getSystemService(Context.VIBRATOR_SERVICE);
            assert vr != null;
            vr.vibrate(100);
        }
        SharedPreferences prefs = v.getContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
        String user = prefs.getString("user", null);
        String password = prefs.getString("password", null);
        String ip = prefs.getString("ip", null);
        if (user == null||ip == null||password == null){
            return;
        }
        try {
            SSH_connection.executeRemoteCommand(ip, user, password, command);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
