package csci432.camera;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static java.lang.Runtime.getRuntime;

public class RaspberryPiCam implements Camera{
    private Integer picNumber;
    private final String saveLocation;

    public RaspberryPiCam(String saveLocation){
        this.saveLocation = saveLocation;
        this.picNumber = 0;
    }

    @Override
    public void takePicture() {
        try {
            Process p = Runtime.getRuntime().exec("raspistill -o "+saveLocation + picNumber + ".jpg --nopreview --timeout 1");
            p.waitFor();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (InterruptedException ix) {
            ix.printStackTrace();
        }
        picNumber++;
    }

    @Override
    public void takePictureOnInterval(Long pause, Long duration) {
        new Thread(() -> {
            try {
                getRuntime().exec("raspistill -w 500 -h 500 -tl " + pause + " -t " + duration + " -o " + saveLocation + "original_%03d.jpg --nopreview");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }).start();
    }

    /**
     * Gets an unfiltered image to filter
     *
     * @return an unfiltered image
     */
    public BufferedImage getUnfilteredImage() {
        String location = String.format(saveLocation+"original_%1$03d.jpg", picNumber);
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(location));
            picNumber++;
        } catch (IOException e) {
            System.out.println("Image not taking yet, waiting...");
        } finally {
            return image;
        }
    }
}

@@ -905,39 +905,8 @@
 ) %7B%0A
-        new Thread(() -%3E %7B%0A    
     
@@ -907,36 +907,32 @@
 %7B%0A        try %7B%0A
-    
             getR
@@ -1069,28 +1069,24 @@
 %22);%0A        
-    
 %7D catch (IOE
@@ -1092,36 +1092,32 @@
 Exception ex) %7B%0A
-    
             ex.p
@@ -1146,33 +1146,9 @@
     
-    %7D%0A        %7D).start();
+%7D
 %0A   

Remove stupid thread thing I did