package com.example.onetap_ssh.ui.terminal;

/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
 * This program enables you to connect to sshd server and get the shell prompt.
 * $ CLASSPATH=.:../build javac Shell.java
 * $ CLASSPATH=.:../build java Shell
 * You will be asked username, hostname and passwd.
 * If everything works fine, you will get the shell prompt. Output may
 * be ugly because of lacks of terminal-emulation, but you can issue commands.
 */

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import com.example.onetap_ssh.ui.home.HomeFragment;

public class Terminal {
    private Session session;
    private JSch jsch;
    private ByteArrayOutputStream Output;
    private PipedOutputStream Input;


    public Terminal() {

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(HomeFragment.user, HomeFragment.ip, 22);
            session.setPassword(HomeFragment.password);

            session.setUserInfo(new MyUserInfo() {
                @Override
                public String getPassword() {
                    return super.getPassword();
                }
            });

            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(30000);

            Channel channel = session.openChannel("shell");


            Input = new PipedOutputStream();

            channel.setInputStream(new PipedInputStream(Input));

            Output = new ByteArrayOutputStream();

            channel.setOutputStream(Output);


            channel.connect(3 * 1000);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String getTerminalOutput() {
        return new String(Output.toByteArray()).replaceAll("\u001B\\[[;\\d]*m", "");
    }

    public void sendTerminalCommand(String terminalcommand) {
        try {
            Input.write((terminalcommand + "\n").getBytes());
            Input.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void closeTerminal() {
        session.disconnect();
    }


    public static abstract class MyUserInfo
            implements UserInfo, UIKeyboardInteractive {
        public String getPassword() {
            return null;
        }

        public boolean promptYesNo(String str) {
            return false;
        }

        public String getPassphrase() {
            return null;
        }

        public boolean promptPassphrase(String message) {
            return false;
        }

        public boolean promptPassword(String message) {
            return false;
        }

        public void showMessage(String message) {
        }

        public String[] promptKeyboardInteractive(String destination,
                                                  String name,
                                                  String instruction,
                                                  String[] prompt,
                                                  boolean[] echo) {
            return null;
        }

    }
}