using System;
using System.Collections;
using System.Collections.Generic;

public sealed class BuildQueue
{
    public readonly Queue<BuildProject> Queue = new Queue<BuildProject>();
    public BuildProject CurrentProject { get; private set; } = null;
    public float BuildTime => buildTimer / ((CurrentProject == null) ? 1f : CurrentProject.BuildTime);
    private float buildTimer = 0;

    public void Add(BuildProject project)
    {
        Queue.Enqueue(project);
        StartNextConstructionProject();
    }

    private void StartNextConstructionProject()
    {
        if (CurrentProject != null)
            return;
        if (Queue.Count == 0)
            return;
        CurrentProject = Queue.Dequeue();
        buildTimer = CurrentProject.BuildTime;
    }

    public void Build(float deltaTime)
    {
        if (CurrentProject == null)
            return;

        buildTimer -= deltaTime;
        if (buildTimer > 0)
            return;

        var project = CurrentProject;
        CurrentProject = null;
        StartNextConstructionProject();
        project.FinishProject();
    }
}
