package org.usfirst.frc.team4828.Pixy;

import edu.wpi.first.wpilibj.Timer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class PixyThread extends Thread {
    private static final String HOST = "pixyco.local";
    private static final int PORT = 5800;
    public static int frame = 0;
    public static int type = 0;
    public static int sig = 0;
    public static int x = 0;
    public static int y = 0;
    public static int width = 0;
    public static int height = 0;
    private BufferedReader in;
    private Socket soc;
    private String visionData;
    private Thread t;
    private boolean enabled;
    private String threadName = "pixy thread";


    public PixyThread() {
        System.out.println("Thread starting: " + threadName);
        try {
            soc = new Socket(HOST, PORT);
            in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Socket connection established");
    }

    @Override
    public void run() {
        while (enabled) {
            try {
                visionData = in.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(visionData);
            frame = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            type = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            sig = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            x = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            y = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            width = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));
            visionData = visionData.substring(visionData.indexOf(' '));
            height = Integer.parseInt(visionData);
        }
    }

    public void start(){
        enabled = true;
        if(t==null){
            t = new Thread(this, threadName);
            t.start();
        }
    }

    public void terminate(){
        enabled = false;
        t = null;
    }
}

@@ -158,16 +158,41 @@
 .Socket;
+%0Aimport java.util.Arrays;
 %0A%0Apublic
@@ -617,16 +617,18 @@
 e String
+%5B%5D
  visionD
@@ -1225,16 +1225,27 @@
 adLine()
+.split(%22 %22)
 ;%0A      
@@ -1356,16 +1356,32 @@
 println(
+Arrays.toString(
 visionDa
@@ -1375,32 +1375,33 @@
 ring(visionData)
+)
 ;%0A            fr
@@ -1437,907 +1437,265 @@
 Data
-.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' '));%0A            type = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' '));%0A            sig = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' '));%0A            x = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' '));%0A            y = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' '));%0A            width = Integer.parseInt(visionData.substring(0, visionData.indexOf(' ')));%0A            visionData = visionData.substring(visionData.indexOf(' ')
+%5B0%5D);%0A            type = Integer.parseInt(visionData%5B1%5D);%0A            sig = Integer.parseInt(visionData%5B2%5D);%0A            x = Integer.parseInt(visionData%5B3%5D);%0A            y = Integer.parseInt(visionData%5B4%5D);%0A            width = Integer.parseInt(visionData%5B5%5D
 );%0A 
@@ -1741,16 +1741,19 @@
 sionData
+%5B6%5D
 );%0A     

Clarify string splitting logic