# Copyright (c) Microsoft Corporation. 
# Licensed under the MIT license. 

import math
import os
import sys
import glob
import gc
import threading

from pathlib import Path
from simplygon import simplygon_loader
from simplygon import Simplygon


def RunReduction(sg: Simplygon.ISimplygon, inputFile, outputFile):
    # Create the reduction pipeline. 
    sgReductionPipeline = sg.CreateReductionPipeline()

    sgReductionSettings = sgReductionPipeline.GetReductionSettings()

    # Set reduction target to triangle ratio with a ratio of 50%. 
    sgReductionSettings.SetReductionTargets( Simplygon.EStopCondition_All, True, False, False, False )
    sgReductionSettings.SetReductionTargetTriangleRatio( 0.5 )

    # Start the reduction pipeline. 
    sgReductionPipeline.RunSceneFromFile(inputFile, outputFile, Simplygon.EPipelineRunMode_RunInNewProcess)


if __name__ == '__main__':
    sg = simplygon_loader.init_simplygon()

    inputFiles = glob.glob('../Assets/' + '/**/*.gltf', recursive=True)
    for inputFile in inputFiles:
        outputFile = Path(inputFile).stem + "_LOD" + Path(inputFile).suffix
        RunReduction(sg, inputFile, outputFile)

    sg = None
    gc.collect()

