/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.velocitycore;

import de.steamwar.linkage.EventMode;
import de.steamwar.linkage.Linked;
import de.steamwar.messages.Chatter;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Linked
@EventMode(false)
public class Broadcaster {

    private final List<String> broadcasts = VelocityCore.get().getConfig().getBroadcasts();
    private int lastBroadCast = 0;

    public Broadcaster() {
        if (!broadcasts.isEmpty()) {
            VelocityCore.schedule(this::broadcast).repeat(10, TimeUnit.MINUTES).schedule();
        }
    }

    private void broadcast() {
        if (!VelocityCore.getProxy().getAllPlayers().isEmpty()) {
            Chatter.broadcast().system("PLAIN_STRING", broadcasts.get(lastBroadCast++));
        }

        if (lastBroadCast == broadcasts.size()) lastBroadCast = 0;
    }
}
