import bot.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.InputStream;
public class OCR extends Methods {
	public boolean autoSleep = true;
	public Client mc;
	public OCR(bot.Client mc){super(mc);this.mc=mc;}
	public void main(String args[]) {
		long lastFatigue = -1;
		try {
			System.out.println("OCR script started");
			while(true) {
			//System.out.println("lastFatigue=" + lastFatigue + " fatigue()=" + fatigue() + " loggedIn()=" + loggedIn());
				if (!loggedIn()) {
					lastFatigue = -1;
				}
				if (lastFatigue == -1 && loggedIn()) {
					wait(5000);
					lastFatigue = fatigue();
				}
				if (lastFatigue > -1 && loggedIn()) {
					if (fatigue() - lastFatigue > 6) {
						mc.irrationalFatigue();
					}
				}
				if (loggedIn()) {
					lastFatigue = fatigue();
				}
				String word = "", ocrWord = "";
				String path = "media" + File.separator + mc.aClass31_1156.aString294 + File.separator;
				File f = new File(path);
				if(sleeping() && !f.exists())
					System.out.println(f.getName() + ".mkdirs: " + f.mkdirs());
				if(sleeping()) {
					ImageIO.write(mc.aBufferedImage1258, "png", new File(path + "sleepword.png"));
				}
				//if (sleeping() && !mc.allowSleep) {
				//	mc.irrationalSleep();
				//}
				if(sleeping()) {
					wait(4000,5000);
					System.out.println("X" + "ocrutils" + File.separator + "nconvert -out ppm -o " + "\"" + path + "sleepword.ppm\" " + "\"" + path + "sleepword.png\"" + "X");
					Process p = Runtime.getRuntime().exec("ocrutils" + File.separator + "nconvert -out ppm -o " + "\"" + path + "sleepword.ppm\" " + "\"" + path + "sleepword.png\"");
					p.waitFor();
					p.destroy();
					p = Runtime.getRuntime().exec("ocrutils" + File.separator + "gocr048 -i " + "\"" + path + "sleepword.ppm\"");
					p.waitFor();
					if(p.getInputStream().available() > 0) {
					InputStream in = p.getInputStream();
						int c = 0;
						while((c = in.read()) != -1)
							word += (char) c;
						in.close();
					}
					p.destroy();
					System.out.println("File deleted: " + new File(path + "sleepword.ppm").delete());
					ocrWord = Functions.parseSleepWord(word);
					mc.aString39 = ocrWord;
					mc.sendSleepWord(mc.aString39);
					mc.allowSleep = false;
					wait(1000);
				}
				wait(250);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
