import org.opencv.highgui.HighGui;
import processing.core.PApplet;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Thread.sleep;

public class GameLoop {
    int activePlayer = 0;

    private final int CAM = 0;                  // Kamera-ID (0 für Webcam, 1 für andere Cams)
    private final int EXP = -8;                 // Belichtungszeit (idealerweise: 0)
    private final int FPS = 20;                 // FPS
    private final int THV = 240;

    private final int TRAIL_LENGTH = 50;

    ImgAnalyzer tracker;
    CamPuffer camPuffer;

    GraphicsProgram graphics;

    GameLogic gl;

    Player player1;
    Player player2;

    public GameLoop(){
        player1 = new Player(0, TRAIL_LENGTH);
        player2 = new Player(1, TRAIL_LENGTH);

        camPuffer = new CamPuffer(CAM, EXP, FPS, THV);
        Thread pufferThread = new Thread(camPuffer);
        pufferThread.start();

        graphics = new GraphicsProgram();
        String[] args = {"GraphicsProgram"};
        PApplet.runSketch(args, graphics);

        tracker = new ImgAnalyzer(camPuffer, THV, graphics);
        Thread trackerThread = new Thread(tracker::run);
        trackerThread.start();


        /*
        gl = new GameLogic();
        String[] args_ = {"GameLogic"};
        PApplet.runSketch(args_, gl); */

        run();
    }

    private void run(){
        while(camPuffer.getCurrentMat().elemSize() == 0){
            System.out.println("Camera booting up");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        graphics.nextGameState();
        //Hier kommt Morphing rein
        graphics.nextGameState();
        while(true){

        }
    }

    private void communicate() {
        //offen für Auto Kommunikation
    }
}