namespace ReactorMaintenance.Simulation;

internal static class ConsumerSystem
{
    public static LevelState Resolve(LevelState level)
    {
        var props = level.Props.ToArray();
        foreach (var position in LevelTraversal.AllPositions(level))
        {
            var index = level.Index(position);
            var prop = props[index];
            if (prop.Type != EPropType.Consumer)
                continue;

            if (prop.SwitchState == EPropSwitchState.Disabled)
            {
                props[index] = prop with { ServiceState = EConsumerServiceState.Disabled };
                continue;
            }

            var underground = level.GetUnderground(position, prop.Carrier);
            var supplied = underground.Amount >= Balancing.Current.ConsumerRequiredAmount && underground.Intensity >= Balancing.Current.ConsumerRequiredIntensity;
            props[index] = prop with { ServiceState = supplied ? EConsumerServiceState.Producing : EConsumerServiceState.Starved };
        }

        return level with { Props = props };
    }
}