#include "MaterialConstantZoneStateT6.h"

#include "Game/T6/CommonT6.h"
#include "Game/T6/GameT6.h"
#include "ObjWriting.h"
#include "Zone/ZoneRegistry.h"

namespace
{
    const char* KNOWN_CONSTANT_NAMES[]{
        "AngularVelocityScale",
        "AnimSpeed",
        "Background",
        "BackgroundColor",
        "BackgroundNoise",
        "BakedLightingIntensity",
        "BloodBrightness",
        "BloodIntensity",
        "BlurAmount",
        "CapWidth",
        "Char_Size",
        "Char_Width",
        "Coarseness",
        "Color",
        "ColorAmount",
        "ColorBias",
        "Color_Map_Noise",
        "Color_Map_Scale",
        "Color_Map_Size_Scale",
        "DDXScale",
        "DDYScale",
        "DarkenAmount",
        "DarkenPower",
        "Detail_Amount",
        "Detail_Normal_Tile",
        "Diffuse_Normal_Height_Facing",
        "Dimensions",
        "DispersionAmount",
        "Dolly",
        "EdgeColor",
        "EdgeHarshness",
        "EdgeIntensity",
        "EdgeMaxDist",
        "EdgeMinDist",
        "EdgeSize",
        "Edge_Color_Multiplier",
        "Emissive_Amount",
        "EnemiesColor",
        "Exposure",
        "FPS",
        "Fade_Distance",
        "Fill_Direction",
        "Fill_Direction2",
        "FirstFrame",
        "FlareIntensity",
        "FlareScale",
        "FlattenEdges",
        "Flicker_Max",
        "Flicker_Min",
        "Flicker_Seed",
        "Flicker_Speed",
        "Font_Color",
        "Gamma",
        "GlossAmount",
        "Gloss_Amount",
        "Glow_Alt_Color",
        "Glow_Color",
        "Glow_Falloff",
        "GradientColor",
        "GradientMax",
        "GradientMin",
        "Grain_Amount",
        "Grain_Color",
        "Grid",
        "Hardness",
        "Heart_Rate_Offset",
        "Heart_Rate_Scale",
        "Highlight_1_Brightness",
        "Highlight_1_Sharpness",
        "Highlight_2_Brightness",
        "Highlight_2_Sharpness",
        "Highlight_2_Size",
        "Hightlight_1_Size",
        "Holo_Scale",
        "LastFrame",
        "Layer1Alpha",
        "Layer1Depth",
        "Layer1Offset",
        "Layer1OffsetBobbleDelay",
        "Layer1OffsetBobbleSpeedAndSize",
        "Layer1Origin",
        "Layer1Rotation",
        "Layer1Scale",
        "Layer1ScaleBobbleDelay",
        "Layer1ScaleBobbleSpeedAndSize",
        "Layer1Scroll",
        "Layer2Alpha",
        "Layer2Depth",
        "Layer2Offset",
        "Layer2OffsetBobbleDelay",
        "Layer2OffsetBobbleSpeedAndSize",
        "Layer2Origin",
        "Layer2Rotation",
        "Layer2Scale",
        "Layer2ScaleBobbleDelay",
        "Layer2ScaleBobbleSpeedAndSize",
        "Layer2Scroll",
        "Layer3Alpha",
        "Layer3Depth",
        "Layer3Offset",
        "Layer3Origin",
        "Layer3Rotation",
        "Layer3Scale",
        "Layer3Scroll",
        "Layer4Alpha",
        "Layer4Depth",
        "Layer4Offset",
        "Layer4Origin",
        "Layer4Rotation",
        "Layer4Scale",
        "Layer4Scroll",
        "LineColor",
        "LineNoise",
        "LineWidth",
        "MaxDepth",
        "MaxFlickerColor",
        "MaxPulseDepth",
        "MaxResolution",
        "Max_Color",
        "Maximum_Distance",
        "Midlayer_Depth",
        "MinDepth",
        "MinFlickerColor",
        "MinResolution",
        "MinStatic",
        "MinVelocityFraction",
        "Min_Color",
        "Min_Player_Intensity",
        "MomentumColor",
        "NegativeColor",
        "NoisePower",
        "Noise_Scale",
        "NormalHeightMultiplier",
        "Normal_Detail_Height",
        "Normal_Detail_Scale",
        "Normal_Map_Size_Scale",
        "Normal_Variance_Scale",
        "NumFrames",
        "Outline_Lookup_Scale",
        "OverallAmount",
        "OverallBrightness",
        "Overlay_Color",
        "P1",
        "P2",
        "Padding",
        "Player_Color_Multiplier",
        "Player_Lookup_Scale",
        "PositiveColor",
        "Power",
        "PulseColor",
        "PulseInterval",
        "PulseTime",
        "Pulse_Color_Multiplier",
        "Pulse_Lookup_Scale",
        "Radius",
        "ReflectionAmount",
        "Reflection_Amount",
        "Reflection_Blur",
        "Reticle_Alt_Color",
        "Reticle_Color",
        "Row_Chars_",
        "Scale",
        "ScanlineColor",
        "ScanlineIntensity",
        "ScanlineOffset",
        "ScanlinePower",
        "ScanlineSpeed",
        "ScatterAmount",
        "ScatterSize",
        "SceneNoise",
        "SparkleBrightness",
        "SparkleDensity",
        "SparklePower",
        "SparkleProbeAmount",
        "SparkleScale",
        "SparkleSpecAmount",
        "SparkleWash",
        "SpecGloss_Map_Size_Scale",
        "SpecularAmount",
        "SpecularColor",
        "Specular_Amount",
        "Specular_Decay_Threshold",
        "Speed",
        "StaticAmount",
        "StaticLookupSpeed",
        "StaticLookupX",
        "StaticScale",
        "Static_Size",
        "Static_amount",
        "TearLookupMaxX",
        "TearLookupMinX",
        "TearLookupSpeed",
        "TearMultiplier",
        "TearPower",
        "Thickness",
        "TickMarkColorAndHarshness",
        "Tint",
        "VelocityScale",
        "VignetteMultiplier",
        "VignettePower",
        "WarpAmount",
        "WarpHeight",
        "WarpScale",
        "WarpSpeed",
        "WashOut",
        "WashoutMultiply",
        "WaterDirection",
        "WaterHeight",
        "WaterRefraction",
        "WaterScale1",
        "WaterScale2",
        "WaterSpeed1",
        "WaterSpeed2",
        "Zoom",
        "alphaDissolveParms",
        "alphaRevealParms",
        "alphaRevealParms1",
        "alphaRevealParms2",
        "alphaRevealParms3",
        "alphaRevealParms4",
        "clipSpaceLookupOffset",
        "clipSpaceLookupScale",
        "cloudsFeather",
        "cloudsHeights",
        "cloudsUVMad1",
        "cloudsUVMad2",
        "cloudsUVMul1",
        "cloudsUVMul2",
        "codeMeshArg",
        "colorDetailScale",
        "colorObjMax",
        "colorObjMaxBaseBlend",
        "colorObjMin",
        "colorObjMinBaseBlend",
        "colorTint",
        "debugBumpmap",
        "debugPerformance",
        "detailScale",
        "detailScale1",
        "detailScale2",
        "detailScale3",
        "detailScale4",
        "distortionScale",
        "dofEquationScene",
        "dofEquationViewModelAndFarBlur",
        "dofLerpBias",
        "dofLerpDownBias",
        "dofLerpDownScale",
        "dofLerpScale",
        "dofLerpUpBias",
        "dofLerpUpScale",
        "dofRowDelta",
        "eyeOffsetParms",
        "falloffBeginColor",
        "falloffEndColor",
        "falloffParms",
        "featherParms",
        "flagParams",
        "framebufferRead",
        "gameTime",
        "hdrAmount",
        "inverseTransposeWorldMatrix",
        "inverseTransposeWorldViewMatrix",
        "inverseWorldMatrix",
        "inverseWorldViewMatrix",
        "motionblurDirectionAndMagnitude",
        "occlusionAmount",
        "occlusionAmount1",
        "occlusionAmount2",
        "occlusionAmount3",
        "occlusionAmount4",
        "particleCloudColor",
        "particleCloudMatrix",
        "particleCloudVelWorld",
        "resizeParams1",
        "resizeParams2",
        "scaleRGB",
        "scriptVector0",
        "scriptVector1",
        "scriptVector2",
        "scriptVector3",
        "scriptVector4",
        "scriptVector5",
        "scriptVector6",
        "scriptVector7",
        "skyBoxCloudWeights",
        "skyBoxRotationSize",
        "skyColorParms",
        "spotLightWeight",
        "treeCanopyLightingParms",
        "treeCanopyScatterColor",
        "treeCanopySwayParms",
        "ui3dUVSetup0",
        "ui3dUVSetup1",
        "ui3dUVSetup2",
        "ui3dUVSetup3",
        "ui3dUVSetup4",
        "ui3dUVSetup5",
        "uvAnimParms",
        "uvScroll",
        "viewMatrix",
        "weaponParam0",
        "weaponParam1",
        "weaponParam2",
        "weaponParam3",
        "weaponParam4",
        "weaponParam5",
        "weaponParam6",
        "weaponParam7",
        "weaponParam8",
        "weaponParam9",
        "worldViewMatrix",
        "worldViewProjectionMatrix",
    };

    const char* KNOWN_TEXTURE_DEF_NAMES[]{
        "AddMap",
        "Blip_Mask",
        "BlockNoise",
        "CS_Z_buffer",
        "Camo_Detail_Map",
        "Color_Map",
        "CompassMap",
        "Detail_Map",
        "Diffuse",
        "Diffuse_Map",
        "DpadTexture",
        "FontTextutre",
        "Grain_Map",
        "GridTexture",
        "GrimeMap",
        "Heart_Rate_Image",
        "Hologram_Diffuse",
        "Image",
        "Layer1Map",
        "Layer2Map",
        "Layer3Map",
        "Layer4Map",
        "Lookup",
        "Lookup2",
        "LookupMap",
        "Mask",
        "Noise",
        "Noise_Texture",
        "NormalDetailMap",
        "Normal_Detail_Map",
        "Normal_Map",
        "Overlay_Map",
        "Reflection_Mask",
        "Reveal_Map",
        "Rim_Color_Mask",
        "Rim_Specular_Mask",
        "Rim_Occlusion_Mask",
        "Scanline",
        "SparkleMap",
        "SpecularAndGloss",
        "SpecularAndGloss2",
        "Specular_Color_Map",
        "Specular_Gloss_Map",
        "Specular_Map",
        "SpotShadowSamplerState",
        "SpotShadowState",
        "SpriteMap",
        "Static",
        "StaticMap",
        "Static_Noise_Map",
        "SunShadowSamplerState",
        "SunShadowState",
        "Surface_Normal_Map",
        "ThermalMapMask",
        "Thermal_Gradient",
        "Thermal_Map",
        "TickMarkMaterial",
        "Tile",
        "WarpMap",
        "WaterNormalMap",
        "Weapon_Normal_Map",
        "Weapon_Specular_Map",
        "Wireframe",
        "ZBuffer_Map",
        "attenuation",
        "attenuationSampler",
        "baseLut2D",
        "baseLut2DSampler",
        "cinematicA",
        "cinematicASampler",
        "cinematicCb",
        "cinematicCbSampler",
        "cinematicCr",
        "cinematicCrSampler",
        "cinematicY",
        "cinematicYSampler",
        "codeTexture0",
        "codeTexture1",
        "codeTexture2",
        "color",
        "colorDetailMap",
        "colorDetailMapSampler",
        "colorMap",
        "colorMap1",
        "colorMap2",
        "colorMap3",
        "colorMap2D",
        "colorMapPostSun",
        "colorMapPostSunSampler",
        "colorMapSampler",
        "colorMapSampler1",
        "colorMapSampler2",
        "colorSampler",
        "detailMap",
        "detailMapSampler",
        "dlightAttenuation",
        "dlightAttenuationSampler",
        "floatZ",
        "floatZSampler",
        "imageSampler",
        "lightmapSamplerSecondary",
        "lightmapSecondary",
        "lut2D",
        "lut2DSampler",
        "lut3D",
        "lut3DSampler",
        "missileCam",
        "missileCamSampler",
        "modelLighting",
        "modelLightingSampler",
        "normalMap",
        "normalMap1",
        "normalMap2",
        "normalMapSampler",
        "normalMapSampler1",
        "normalMapSampler2",
        "occlusionMap",
        "occlusionMapSampler",
        "occMap",
        "occMapSampler",
        "outdoorMap",
        "outdoorMapSampler",
        "radiantDiffuseMap",
        "rawFloatZ",
        "rawFloatZSampler",
        "reflectionProbe",
        "reflectionProbeSampler",
        "shadowmapSamplerSpot",
        "shadowmapSamplerSun",
        "shadowmapSpot",
        "shadowmapSun",
        "sonarColor",
        "sonarColorSampler",
        "sonarDepth",
        "sonarDepthSampler",
        "source",
        "specularMap",
        "specularMap1",
        "specularMap2",
        "specularMapSampler",
        "specularMapSampler1",
        "specularMapSampler2",
        "stencil",
        "stencilSampler",
        "ui3d",
        "ui3dSampler",
    };
} // namespace

namespace T6
{
    void MaterialConstantZoneState::ExtractNamesFromZoneInternal()
    {
        for (const auto* zone : ZoneRegistry::GetRegistryForGame(GameId::T6)->Zones())
        {
            for (const auto* techniqueSetInfo : zone->m_pools.PoolAssets<AssetTechniqueSet>())
            {
                const auto* techniqueSet = techniqueSetInfo->Asset();

                for (const auto* technique : techniqueSet->techniques)
                {
                    if (technique)
                        ExtractNamesFromTechnique(technique);
                }
            }
        }
    }

    unsigned MaterialConstantZoneState::HashString(const std::string& str) const
    {
        return Common::R_HashString(str.c_str());
    }

    void MaterialConstantZoneState::ExtractNamesFromTechnique(const MaterialTechnique* technique)
    {
        if (!ShouldDumpFromStruct(technique))
            return;

        for (auto passIndex = 0u; passIndex < technique->passCount; passIndex++)
        {
            const auto& pass = technique->passArray[passIndex];

            if (pass.vertexShader && pass.vertexShader->prog.loadDef.program)
                ExtractNamesFromShader(pass.vertexShader->prog.loadDef.program, pass.vertexShader->prog.loadDef.programSize);

            if (pass.pixelShader && pass.pixelShader->prog.loadDef.program)
                ExtractNamesFromShader(pass.pixelShader->prog.loadDef.program, pass.pixelShader->prog.loadDef.programSize);
        }
    }

    void MaterialConstantZoneState::AddStaticKnownNames()
    {
        for (const auto* knownConstantName : KNOWN_CONSTANT_NAMES)
            AddConstantName(knownConstantName);
        for (const auto* knownTextureDefName : KNOWN_TEXTURE_DEF_NAMES)
            AddTextureDefName(knownTextureDefName);
    }
} // namespace T6
