/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2021  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.bausystem.features.world;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.core.CRIUWakeupEvent;
import de.steamwar.core.CheckpointUtils;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;

@Linked
public class AFKStopperListener implements Listener {

    private long lastMovementTime = System.currentTimeMillis();

    public AFKStopperListener() {
        Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
            long currentTime = System.currentTimeMillis();
            if(currentTime - lastMovementTime > 10*60000) { // 10 Minutes
                for (Player p : Bukkit.getOnlinePlayers())
                    p.kickPlayer(BauSystem.MESSAGE.parse("AFK_KICK_MESSAGE", p));
            } else if(currentTime - lastMovementTime > 9*60000)
                BauSystem.MESSAGE.broadcast("AFK_WARNING_MESSAGE");
        }, 1200, 1200); //every minute
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Location to = event.getTo();
        if (to == null)
            return;

        Location from = event.getFrom();
        if (from.getPitch() != to.getPitch() || from.getYaw() != to.getYaw())
            lastMovementTime = System.currentTimeMillis();
    }

    @EventHandler(priority = EventPriority.LOWEST) //Potential fix for potential race condition with WE axe spontaneously not working
    public void onPlayerJoin(PlayerJoinEvent event) {
        event.getPlayer().setOp(true);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerQuit(PlayerQuitEvent event) {
        if(Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(event.getPlayer())))
            CheckpointUtils.freeze();
    }

    @EventHandler
    public void onCRIUWakeup(CRIUWakeupEvent event) {
        lastMovementTime = System.currentTimeMillis();
    }
}
