namespace splat;

using Godot;

/// <summary>
/// Physical material a structure is made from. Drives weight, durability, and
/// how fast the structure decays under weather exposure.
/// </summary>
public enum MaterialType
{
    Stone,
    Timber,
    Turf,
    Concrete,
    Steel
}

/// <summary>
/// How a structure meets the ground. Determines how much ground settling is
/// tolerated before structural damage begins.
/// </summary>
public enum FoundationType
{
    Shallow,
    Deep,
    Floating
}

/// <summary>
/// Custom resource describing a building's physical properties. Material
/// density values follow typical Scottish construction materials so that
/// derived mass is physically plausible (e.g. stone ~2400 kg/m³).
/// </summary>
[GlobalClass]
public partial class BuildingData : Resource
{
    [Export] public string BuildingName { get; set; } = "Building";
    [Export] public MaterialType Material { get; set; } = MaterialType.Stone;
    [Export] public FoundationType Foundation { get; set; } = FoundationType.Shallow;

    /// <summary>Structural integrity in [0, 1]; 1 = pristine, 0 = collapsed.</summary>
    [Export(PropertyHint.Range, "0,1,0.01")]
    public float StructuralIntegrity { get; set; } = 1f;

    /// <summary>Approximate volume in m³, used with material density for mass.</summary>
    [Export] public float Volume { get; set; } = 100f;

    /// <summary>Mass in kg, derived from volume × material density.</summary>
    [Export] public float Mass { get; set; }

    /// <summary>Maximum load (kg) the structure can bear before deformation.</summary>
    [Export] public float MaxLoad { get; set; } = 50000f;

    /// <summary>Baseline decay rate per in-game day at full weather exposure.</summary>
    [Export(PropertyHint.Range, "0,0.01,0.0001")]
    public float DecayRate { get; set; } = 0.001f;

    /// <summary>Density (kg/m³) per material, used to compute <see cref="Mass"/>.</summary>
    public static float Density(MaterialType material) => material switch
    {
        MaterialType.Stone => 2400f,    // typical Scottish sandstone / granite
        MaterialType.Timber => 600f,    // softwood timber framing
        MaterialType.Turf => 400f,      // peat / turf, water-retentive
        MaterialType.Concrete => 2400f, // reinforced concrete
        MaterialType.Steel => 7850f,    // structural steel
        _ => 2400f
    };

    public void Init()
    {
        if (Mass <= 0f)
            Mass = Volume * Density(Material);
    }
}
