#ifndef ELECTRIC_LINK_HPP_
#define ELECTRIC_LINK_HPP_

#include <vector>
#include <memory>

#include <WorldComponent/Wiring/Wiring.hpp>
#include <Machine/Machine.hpp>
#include <Side/Side.hpp>

/**
 * @brief Represents a link between a machine port and other ports.
 */
class ElectricLink
{
private:
    struct Endpoint
    {
        std::weak_ptr<Machine> machine;
        Side side;
    };

    std::vector<Endpoint> endpoints;

public:
    static std::shared_ptr<ElectricLink> dummyLink;
    const glm::ivec3 pos = glm::ivec3(0, 0, 0);
    const Side side = Side(0);

    ElectricLink() = default;

    /**
     * @brief Construct a new electric link from the specified data.
     */
    ElectricLink(LocalWorld &world, std::shared_ptr<WorldGridNode> node, std::vector<Wiring::Port> &ports, size_t startingPort);

    /**
     * @brief Get the electric link from the specified machine's side.
     * 
     * @param machine The machine requesting a link.
     * @param side The side of the machine where we are searching from.
     */
    static std::shared_ptr<ElectricLink> get(Machine &machine, Side side);

    /**
     * @brief Try to drain the specified amount of energy (J) from the connected machines.
     * 
     * @param amount The maximum amount to drain.
     * @return The amount of energy actually drained (J).
     */
    int drainEnergy(int amount);
};

#endif