package leigh.lighting.show;

import leigh.lighting.net.SACNTransmitter;
import leigh.lighting.processor.ExampleProcessor;
import leigh.lighting.processor.Processor;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.VelocityWatch;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Random;

public class Show implements Runnable {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(Show.class);
    private final byte[] input = new byte[512];
    private final VelocityWatch vw = new VelocityWatch(logger);
    private final Universe univs[];

    public Show() throws UnknownHostException, SocketException {
        univs = new Universe[1000];
        Processor p = new ExampleProcessor();
        Processor[] procs = new Processor[512];
        Arrays.fill(procs, p);
        Random r = new Random();
        Arrays.fill(univs, new Universe(new SACNTransmitter(InetAddress.getByName("10.52.3.127"), r.nextInt(65535), 1), procs));
    }

    @Override
    public void run() {
        while (true) {
            long now = Instant.now().toEpochMilli();
            for (Universe univ : univs) {
                try {
                    univ.process(now, input);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            vw.event("frame");
        }
    }

    public static void main(String[] args) throws SocketException, UnknownHostException, InterruptedException {
        for (int i = 0; i < 3; i++) {
            logger.info("Starting show...");
            Thread t = new Thread(new Show());
            t.join();
            t.start();
        }
    }
}
