# 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

class CustomObserver(Simplygon.Observer):
    def OnShadingCustomNodeEvaluate(self, subject: Simplygon.spObject):
        outputValue = Simplygon.ShadingColor( 0.0, 0.0, 0.0, 1.0 )
        if not subject.IsNull():
            customNode = Simplygon.spShadingCustomNode.SafeCast(subject)
            if not customNode.IsNull():
                inputValue = customNode.GetInputValue(0)
                outputValue.r = inputValue.r * 0.393 + inputValue.g * 0.769 + inputValue.b * 0.189
                outputValue.g = inputValue.r * 0.349 + inputValue.g * 0.686 + inputValue.b * 0.168
                outputValue.b = inputValue.r * 0.272 + inputValue.g * 0.534 + inputValue.b * 0.131
        return outputValue
    def OnShadingCustomNodeGenerateShaderCode(self, subject: Simplygon.spObject, shaderLanguage):
        if not subject.IsNull():
            customNode = Simplygon.spShadingCustomNode.SafeCast(subject)
            if not customNode.IsNull():
                customNode.SetCustomShaderCode( "result = float4(dot(rgba_custom_input_0, float3(0.393f, 0.769f, 0.189f)), dot(rgba_custom_input_0, float3(0.349f, 0.686f, 0.168f)), dot(rgba_custom_input_0, float3(0.272f, 0.534f, 0.131f)), 1.0f);")
                return True
        return False

customObserver = CustomObserver()

def RunReductionWithShadingNetworks(sg: Simplygon.ISimplygon):
    sgSceneImporter = sg.CreateSceneImporter()
    sgSceneImporter.SetImportFilePath( "../Assets/RiggedSimplygonMan/RiggedSimplygonMan.fbx" )
    if not sgSceneImporter.RunImport():
        raise Exception("Failed to load RiggedSimplygonMan/RiggedSimplygonMan.fbx.")
    sgScene = sgSceneImporter.GetScene()

    sgReductionProcessor = sg.CreateReductionProcessor()

    sgReductionProcessor.SetScene( sgScene )

    sgReductionSettings = sgReductionProcessor.GetReductionSettings()
    sgMappingImageSettings = sgReductionProcessor.GetMappingImageSettings()

    # Generates a mapping image which is used after the reduction to cast new materials to the new 
    # reduced object. 
    sgMappingImageSettings.SetGenerateMappingImage( True )
    sgMappingImageSettings.SetApplyNewMaterialIds( True )
    sgMappingImageSettings.SetGenerateTangents( True )
    sgMappingImageSettings.SetUseFullRetexturing( True )

    # Inject a sepia filter as a custom shading node into the shading network for the diffuse channel 
    # for each material in the scene. 
    materialCount = sgScene.GetMaterialTable().GetMaterialsCount()
    for i in range(0, materialCount):
        sgMaterial = sgScene.GetMaterialTable().GetMaterial(i)

        sgMaterialShadingNode = sgMaterial.GetShadingNetwork("Diffuse")
        sgSepiaNode = sg.CreateShadingCustomNode()

        # Add the custom observer to our custom shading node.
        sgSepiaNode.AddObserver(customObserver)
        
        # Set the number of input slots to the custom node. In this case we only use the diffuse color from the loaded material as input.
        sgSepiaNode.SetInputCount(1)


        sgSepiaNode.SetInput(0, sgMaterialShadingNode)

        sgMaterial.SetShadingNetwork("Diffuse", sgSepiaNode)


    # Start the reduction process. 
    sgReductionProcessor.RunProcessing()

    # Setup and run the diffuse material casting. 
    sgDiffuseCaster = sg.CreateColorCaster()
    sgDiffuseCaster.SetMappingImage( sgReductionProcessor.GetMappingImage() )
    sgDiffuseCaster.SetSourceMaterials( sgScene.GetMaterialTable() )
    sgDiffuseCaster.SetSourceTextures( sgScene.GetTextureTable() )
    sgDiffuseCaster.SetOutputFilePath( "DiffuseTexture" )

    sgDiffuseCasterSettings = sgDiffuseCaster.GetColorCasterSettings()
    sgDiffuseCasterSettings.SetMaterialChannel( "Diffuse" )
    sgDiffuseCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat_PNG )

    sgDiffuseCaster.RunProcessing()
    diffuseTextureFilePath = sgDiffuseCaster.GetOutputFilePath()

    # Update scene with new casted texture. 
    sgMaterialTable = sg.CreateMaterialTable()
    sgTextureTable = sg.CreateTextureTable()
    sgMaterial = sg.CreateMaterial()
    sgDiffuseTexture = sg.CreateTexture()
    sgDiffuseTexture.SetName( "Diffuse" )
    sgDiffuseTexture.SetFilePath( diffuseTextureFilePath )
    sgTextureTable.AddTexture( sgDiffuseTexture )

    sgDiffuseTextureShadingNode = sg.CreateShadingTextureNode()
    sgDiffuseTextureShadingNode.SetTexCoordLevel( 0 )
    sgDiffuseTextureShadingNode.SetTextureName( "Diffuse" )

    sgMaterial.AddMaterialChannel( "Diffuse" )
    sgMaterial.SetShadingNetwork( "Diffuse", sgDiffuseTextureShadingNode )

    sgMaterialTable.AddMaterial( sgMaterial )

    sgScene.GetTextureTable().Clear()
    sgScene.GetMaterialTable().Clear()
    sgScene.GetTextureTable().Copy(sgTextureTable)
    sgScene.GetMaterialTable().Copy(sgMaterialTable)

    sgSceneExporter = sg.CreateSceneExporter()
    sgSceneExporter.SetScene(sgScene)
    sgSceneExporter.SetExportFilePath( "ReductionOutput.fbx" )
    if not sgSceneExporter.RunExport():
        raise Exception("Failed to save ReductionOutput.fbx.")


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

    RunReductionWithShadingNetworks(sg)

    sg = None
    gc.collect()

