using UnityEngine;
using UnityEngine.Rendering;
using INab.BetterFog.URP;
namespace Ashwild.Environment
{
///
/// Ties INab Studio's Better Fog to the day/night cycle: every frame it copies the
/// TimeOfDayManager's current sky colors into the Better Fog volume override so the fog
/// warms at dawn/dusk and turns cool/dark at night along with the sky. This is the ONLY
/// script that references the Better Fog asset — the manager itself stays fog-agnostic, so
/// swapping the fog solution later only touches this file. Runs in edit mode so the fog
/// tracks the time slider live.
///
[ExecuteAlways]
[DisallowMultipleComponent]
[AddComponentMenu("GAME/Environment/Better Fog Day-Night Driver")]
public class BetterFogDayNightDriver : MonoBehaviour
{
#region Serialized Fields
[Tooltip("Global Volume whose profile holds a Better Fog override.")]
[SerializeField] private Volume fogVolume;
[Tooltip("Push the current horizon color into Better Fog's Fog Color.")]
[SerializeField] private bool driveFogColor = true;
[Tooltip("Push the current sun color into Better Fog's Sun Color (only visible when Better Fog's Sun Light is enabled).")]
[SerializeField] private bool driveSunColor = true;
#endregion
#region State
private BetterFogVolumeComponent fog;
#endregion
#region Unity Lifecycle
///
/// Resolves the Better Fog override once so Update can drive it cheaply, and warns if the
/// wiring is incomplete instead of silently doing nothing.
///
private void OnEnable()
{
ResolveFog();
}
///
/// Copies the manager's current palette into the fog override. No-op until both the fog
/// volume and the manager exist, re-resolving the override if it was lost (profile swap).
///
private void Update()
{
if (fog == null && !ResolveFog()) return;
if (TimeOfDayManager.Instance == null) return;
if (driveFogColor)
{
fog._FogColor.overrideState = true;
fog._FogColor.value = TimeOfDayManager.Instance.CurrentHorizonColor;
}
if (driveSunColor)
{
fog._SunColor.overrideState = true;
fog._SunColor.value = TimeOfDayManager.Instance.CurrentSunColor;
}
}
#endregion
#region Internal Helpers
///
/// Fetches the Better Fog override from the assigned volume's profile, logging a clear
/// error when it is missing so the setup mistake is obvious. Returns true on success.
///
private bool ResolveFog()
{
if (fogVolume == null)
{
Debug.LogError($"[BetterFogDayNightDriver] '{name}' has no Fog Volume assigned.", this);
return false;
}
if (fogVolume.profile == null || !fogVolume.profile.TryGet(out fog))
{
Debug.LogError($"[BetterFogDayNightDriver] '{name}' volume profile has no Better Fog override.", this);
return false;
}
return true;
}
#endregion
}
}