package ru.dragonestia.kubik.discovery.model;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Objects;

@Getter
public class Server {

    private final String id;
    private final String groupId;
    private final String address;
    private final int port;

    @Setter private boolean canPicked = true;
    @Setter private Instant lastActivity = Instant.now();

    public Server(ServerIdentifierStrategy identifierStrategy, String groupId, String address, int port) {
        this.id = identifierStrategy.of(groupId, address, port);
        this.groupId = groupId;
        this.address = address;
        this.port = port;
    }

    @Override
    public String toString() {
        return "Server(id=%s, groupId=%s, address=%s, port=%s, canPicked=%s)".formatted(id, groupId, address, port, canPicked);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == null || getClass() != other.getClass()) return false;
        var server = (Server) other;
        return Objects.equals(id, server.id);
    }
}
