/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  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.fightsystem.fight;

import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.listener.Recording;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.utils.CraftbukkitWrapper;
import de.steamwar.fightsystem.utils.FlatteningWrapper;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FightWorld extends StateDependent {

    @Getter
    private static final boolean PAPER = Bukkit.getVersion().contains("git-Paper");

    public FightWorld() {
        super(ArenaMode.Restartable.contains(Config.mode) || Config.replayserver(), FightState.Schem);
        register();
    }

    @Override
    public void enable() {
        //unused
    }

    @Override
    public void disable() {
        Bukkit.getScheduler().runTask(FightSystem.getPlugin(), FightWorld::resetWorld); //Delay to prevent raceconditions with techhider and hullhider
    }

    public static void forceLoad(){
        Config.ArenaRegion.forEachChunk((cX, cZ) -> {
            Config.world.loadChunk(cX, cZ);
            FlatteningWrapper.impl.forceLoadChunk(Config.world, cX, cZ);
        });
    }

    public static void resetWorld(){
        List<Entity> entities = new ArrayList<>();
        Recording.iterateOverEntities(Objects::nonNull, entity -> {
            if(entity.getType() != EntityType.PLAYER && (!Config.ArenaLeaveable || Config.ArenaRegion.inRegion(entity.getLocation())))
                entities.add(entity);
        });
        entities.forEach(Entity::remove);
        entities.clear();

        World backup = new WorldCreator(Config.world.getName() + "/backup").createWorld();
        assert backup != null;
        Config.ArenaRegion.forEachChunk((x, z) -> {
            CraftbukkitWrapper.impl.resetChunk(Config.world, backup, x, z);
            for(Player p : Bukkit.getOnlinePlayers()) {
                de.steamwar.core.CraftbukkitWrapper.impl.sendChunk(p, x, z);
            }
        });
        Bukkit.unloadWorld(backup, false);
    }
}