package net.halbear.Terrain4J.EngineCore.Threads;

import net.halbear.Terrain4J.EngineCore.Display.Render;
import net.halbear.Terrain4J.EngineCore.Display.Window;
import net.halbear.Terrain4J.EngineCore.Logic.*;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import net.halbear.Terrain4J.EngineCore.Main.PrimaryRuntime;
import org.tinylog.Logger;

public class RenderThread extends EngineThread {
    private final Render render;
    private Window window;

    public RenderThread(Window window, EngineInstance engineInstance, InitData initData, GameLogic appLogic) {
        //this.window = window;
        render = new Render(engineInstance);
        super(engineInstance,appLogic,"Render");
        render.Initialise(initData);
    }

    @Override
    public void TickEvent(long nsTimeDiff){

    }
    @Override
    public void FrameEvent(long now, long InitialTime){
        gameLogic.Input(PrimaryRuntime.GetEngineInstance(), (now - InitialTime));
        render.render(PrimaryRuntime.GetEngineInstance());
    }
    @Override
    public void CycleEvent(){

    }
    @Override
    public void SecondEvent(){
        Logger.info("Render FPS: " + Frames);
    }


    @Override
    public void cleanup() {
        render.cleanup();
        PrimaryRuntime.ThreadsOpen--;
        Logger.debug("Closing Render Thread, Thread: [{}]",Thread.currentThread().toString());
        if (PrimaryRuntime.ThreadsOpen <= 0 && VulkanContext.VulkanLayersOpen < 0) PrimaryRuntime.CanClose = true;
    }
}
