package oracle;

import oracle.cli.CLI;
import oracle.cli.Line;
import oracle.web.Webserver;
import processing.core.PApplet;

import java.awt.event.KeyEvent;

/**
 * Created by mrzl on 31.03.2016.
 */
public class LacunaLabOracle extends PApplet{

    public static String EXPORT_FILENAME_PREFIX = "lacuna_markov_export-order";
    public static int MAX_INPUT_WORDS = 4;
    private CLI cli;
    private MarkovManager markov;

    OracleLogger logger ;

    long millisLastInteraction;
    long idleDelay = 5 * 60 * 1000; // 5 minutes

    Webserver server;
    private boolean intercept;


    public void settings() {
        size( 640, 480 );
        logger = new OracleLogger(this);

        //fullScreen( );

        millisLastInteraction = System.currentTimeMillis();
        server = new Webserver(this);
    }

    public void setup() {
        cli = new CLI( this );
        markov = new MarkovManager();

        //markov.save();
        markov.load();

        noCursor();
    }

    public void draw() {
        background( 0 );
        cli.draw();

        if( System.currentTimeMillis() > millisLastInteraction + idleDelay ){
            cli.reset();
        }
    }

    public void keyPressed() {
        millisLastInteraction = System.currentTimeMillis();

        if(cli.isActive())
            return;

        if( key == CODED ){
            switch ( keyCode ) {
                case KeyEvent.VK_F1:
                    cli.reset();
                    break;
            }
        } else {
            switch ( key ) {
                case BACKSPACE:
                    cli.backspace();
                    break;
                case ENTER:
                    if( !cli.available() ){
                        return;
                    }

                    String inputWordsString = cli.getLastLine().getText(true).trim();
                    while( inputWordsString.startsWith( "." ) ||
                            inputWordsString.startsWith( "," ) ||
                            inputWordsString.startsWith( ";" ) ||
                            inputWordsString.startsWith( ":" ) ||
                            inputWordsString.startsWith( "-" ) ||
                            inputWordsString.startsWith( "_" ) ) {
                        // removing some leading special characters
                        inputWordsString = inputWordsString.substring( 1 );
                    }
                    inputWordsString = inputWordsString.trim();
                    System.out.println(inputWordsString);
                    if(intercept) {
                        server.sendInput(inputWordsString);
                        logger.logInput(inputWordsString);
                        cli.waitForAnswer();
                        return;
                    } else {
                        String result = markov.getAnswer(inputWordsString);
                        if (result.contains("lacuna")) {
                            cli.startEmojiEasterEgg();
                        }
                        logger.log(inputWordsString,result);
                        System.out.println(result);
                        cli.finish(result, calculateDelayByInputLength(inputWordsString.split(" ").length));
                    }
                    break;
                case TAB:
                case DELETE:
                    break;
                case ESC:
                    key = 0;
                    cli.reset();
                    break;
                default:
                    if(!cli.inputLimitReached() && !cli.isActive()){
                        cli.type( key );
                    }
                    break;
            }
        }
    }


    private long calculateDelayByInputLength( int length ) {
        return ( long ) map( length, 1, 8, 400, 7000 );
    }

    public boolean intercept() {
        if(intercept)
            return true;
        intercept = true;
        return false;
    }

    public void responseFromTheWeb(String response) {
        if (response.contains("lacuna")) {
            cli.startEmojiEasterEgg();
        }
        intercept = false;
        logger.logResponse(response,false);
        System.out.println(response);
        cli.finishFromWeb(response);
    }
}

@@ -150,16 +150,142 @@
 eyEvent;
+%0Aimport java.net.InetAddress;%0Aimport java.net.NetworkInterface;%0Aimport java.net.SocketException;%0Aimport java.util.Enumeration;
 %0A%0A/**%0A *
@@ -1098,24 +1098,45 @@
  noCursor();
+%0A%0A        printIps();
 %0A    %7D%0A%0A    
@@ -4388,11 +4388,983 @@
 );%0A    %7D
+%0A%0A    public void printIps() %7B%0A        System.out.println(%22*** Networks interfaces:%22);%0A        String ip;%0A        try %7B%0A            Enumeration%3CNetworkInterface%3E interfaces = NetworkInterface.getNetworkInterfaces();%0A            while (interfaces.hasMoreElements()) %7B%0A                NetworkInterface iface = interfaces.nextElement();%0A                // filters out 127.0.0.1 and inactive interfaces%0A                if (iface.isLoopback() %7C%7C !iface.isUp())%0A                    continue;%0A%0A                Enumeration%3CInetAddress%3E addresses = iface.getInetAddresses();%0A                while(addresses.hasMoreElements()) %7B%0A                    InetAddress addr = addresses.nextElement();%0A                    ip = addr.getHostAddress();%0A                    System.out.println(iface.getDisplayName() + %22 %22 + ip);%0A                %7D%0A            %7D%0A        %7D catch (SocketException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A        System.out.println(%22******%22);%0A    %7D
 %0A%7D%0A

print network interface