using StaticArrays: SVector

mutable struct Particle
    id::Int64

    c::SVector{2,Float64} # Center
    tmp_c::SVector{2,Float64} # Temporary center

    φ::Float64 # Angle

    function Particle(id::Int64, c::SVector{2,Float64}, φ::Float64)
        return new(id, c, c, φ)
    end
end

function gen_tmp_particle()
    return Particle(0, SVector(0.0, 0.0), 0.0)
end

function restrict_coordinate(value::Float64, half_box_len::Float64)
    if value < -half_box_len
        value += 2 * half_box_len
    elseif value >= half_box_len
        value -= 2 * half_box_len
    end

    return value
end

function restrict_coordinates(v::SVector{2,Float64}, half_box_len::Float64)
    return SVector(
        restrict_coordinate(v[1], half_box_len), restrict_coordinate(v[2], half_box_len)
    )
end

function restrict_coordinates!(p::Particle, half_box_len::Float64)
    p.tmp_c = restrict_coordinates(p.tmp_c, half_box_len)

    return nothing
end

function are_overlapping(
    c1::SVector{2,Float64},
    c2::SVector{2,Float64},
    overlapping_r²::Float64,
    half_box_len::Float64,
)
    r⃗₁₂ = c2 - c1 # 1 -> 2

    r⃗₁₂ = restrict_coordinates(r⃗₁₂, half_box_len)

    distance² = sq_norm2d(r⃗₁₂)

    overlapping = distance² < overlapping_r²

    return (; overlapping, r⃗₁₂, distance²)
end