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

#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"


void RunReduction(Simplygon::ISimplygon* sg, std::string inputFile, std::string outputFile)
{
	// Create the reduction pipeline. 
	Simplygon::spReductionPipeline sgReductionPipeline = sg->CreateReductionPipeline();

	Simplygon::spReductionSettings 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.5f );

	// Start the reduction pipeline. 
	sgReductionPipeline->RunSceneFromFile(inputFile.c_str(), outputFile.c_str(), Simplygon::EPipelineRunMode::RunInNewProcess);

}

void main()
{
	Simplygon::ISimplygon* sg = NULL;
	Simplygon::EErrorCodes initval = Simplygon::Initialize( &sg );
	if( initval != Simplygon::EErrorCodes::NoError )
	{
		return;
	}

	for(auto& itr: std::filesystem::recursive_directory_iterator("../Assets/"))
	{
		if (itr.path().extension().compare(".gltf") != 0)
			continue;
		std::string inputFile = itr.path().generic_u8string();
		std::string outputFile = itr.path().stem().concat("_LOD").concat( itr.path().extension().generic_u8string() ).generic_u8string();
		RunReduction(sg, inputFile, outputFile);
	}

	Simplygon::Deinitialize(sg);
}

