Shader "Eagle/HeatShimmer" {
    Properties {
        _ProvinceIDMap ("Province ID Map (rawGray)", 2D) = "black" {}
        _TargetProvinceId ("Target Province ID", Float) = 0
        _DistortionStrength ("Distortion Strength", Range(0, 0.05)) = 0.01
        _DistortionSpeed ("Distortion Speed", Range(0, 5)) = 1.5
        _DistortionScale ("Distortion Scale", Range(1, 50)) = 15
    }

    SubShader {
        Tags {
            "Queue"="Transparent+100"
            "RenderType"="Transparent"
        }

        // Grab the screen behind the object
        GrabPass { "_GrabTexture" }

        Pass {
            ZWrite Off
            Blend Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _GrabTexture;
            float4 _GrabTexture_TexelSize;
            sampler2D _ProvinceIDMap;
            float _TargetProvinceId;
            float _DistortionStrength;
            float _DistortionSpeed;
            float _DistortionScale;

            // Map bounds in screen space (pixels)
            float4 _MapBounds; // x=minX, y=minY, z=width, w=height

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float4 grabPos : TEXCOORD0;
                float4 screenPos : TEXCOORD1;
                float2 worldPos : TEXCOORD2;
            };

            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.grabPos = ComputeGrabScreenPos(o.pos);
                o.screenPos = ComputeScreenPos(o.pos);

                // Calculate world position for distortion animation
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.worldPos = worldPos.xy;

                return o;
            }

            fixed4 frag(v2f i) : SV_Target {
                // Calculate screen UV and convert to map UV
                float2 screenUV = i.screenPos.xy / i.screenPos.w;
                float2 screenPixel = screenUV * _ScreenParams.xy;
                float2 mapUV = float2(
                    (screenPixel.x - _MapBounds.x) / _MapBounds.z,
                    (screenPixel.y - _MapBounds.y) / _MapBounds.w
                );

                // Sample province ID from rawGray
                float sampledId = tex2D(_ProvinceIDMap, mapUV).r * 255.0;

                // Check if this pixel belongs to the target province
                if (abs(sampledId - _TargetProvinceId) > 0.5) {
                    // Not in province - return undistorted background
                    return tex2Dproj(_GrabTexture, i.grabPos);
                }

                // Create heat shimmer distortion using sine waves
                float time = _Time.y * _DistortionSpeed;
                float2 distortion;
                distortion.x = sin(i.worldPos.y * _DistortionScale + time) * _DistortionStrength;
                distortion.y = sin(i.worldPos.x * _DistortionScale * 0.7 + time * 1.3) * _DistortionStrength;

                // Apply distortion to grab UV
                float4 distortedGrabPos = i.grabPos;
                distortedGrabPos.xy += distortion * i.grabPos.w;

                // Sample distorted background
                fixed4 col = tex2Dproj(_GrabTexture, distortedGrabPos);

                // Slight yellow/orange tint for heat effect
                col.rgb = lerp(col.rgb, col.rgb * float3(1.05, 1.0, 0.9), 0.3);

                return col;
            }
            ENDCG
        }
    }

    Fallback Off
}
