package electrosphere.engine.profiler;

import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.remotery.Remotery;

/**
 * A profiler for monitoring engine performance
 */
public class Profiler {

    //controls whether to profile or not
    public static boolean PROFILE = true;

    //pointer to the global instance
    long pointer = -1;
    
    /**
     * Creates the profiler
     */
    public Profiler(){
        if(PROFILE){
            try(MemoryStack stack = MemoryStack.stackPush()){
                PointerBuffer allocBuffer = stack.mallocPointer(1);
                Remotery.rmt_CreateGlobalInstance(allocBuffer);
                pointer = allocBuffer.get();
            }
        }
    }

    /**
     * Begins a CPU sample
     * @param sampleName The name of the sample
     */
    public void beginCpuSample(String sampleName){
        if(PROFILE){
            Remotery.rmt_BeginCPUSample(sampleName, Remotery.RMTSF_None, null);
        }
    }

    /**
     * Begins an aggregate CPU sample (must create a regular cpu sample of same type outside function this is inside of to encapsule all calls to aggregate)
     * @param sampleName The name of the sample
     */
    public void beginAggregateCpuSample(String sampleName){
        if(PROFILE){
            Remotery.rmt_BeginCPUSample(sampleName, Remotery.RMTSF_Aggregate, null);
        }
    }

    /**
     * Begins a Root CPU sample (will assert if another sample is not ended before this one)
     * @param sampleName The name of the root sample
     */
    public void beginRootCpuSample(String sampleName){
        if(PROFILE){
            Remotery.rmt_BeginCPUSample(sampleName, Remotery.RMTSF_Root, null);
        }
    }

    /**
     * Ends a CPU sample
     * @param sampleName The name of the sample
     */
    public void endCpuSample(){
        if(PROFILE){
            Remotery.rmt_EndCPUSample();
        }
    }

}
