/*
 * 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.bausystem.region.dynamic.spawn;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.BlockVector3;
import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.Location;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

@UtilityClass
public class SpawnResetter {

    private static final File SECTIONS_DIR = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "sections");
    private static final File RESET_FILE = new File(SECTIONS_DIR, "spawn/SpawnBig.schem");

    public static final Location BIG_WORLD_SPAWN = new Location(Bukkit.getWorlds().get(0), 0.5, 26, 0.5);
    public static final Location SMALL_WORLD_SPAWN = new Location(Bukkit.getWorlds().get(0), 0.5, 32, 0.5);

    private static final Location LOCATION = new Location(null, 0, 0, 0);

    public boolean isBigSpawn() {
        Region spawnRegion = DynamicRegionSystem.INSTANCE.get(LOCATION);
        List<Region> neighbours = DynamicRegionSystem.INSTANCE.getNeighbours(spawnRegion)
                .filter(r -> r.getType() == RegionType.PATH || r.getType() == RegionType.SPAWN_EXTENSION || r.getType() == RegionType.SPAWN_PATH)
                .collect(Collectors.toList());
        return neighbours.size() == 8;
    }

    public void reset(Region region) {
        Region spawnRegion = DynamicRegionSystem.INSTANCE.get(LOCATION);
        List<Region> neighbours = DynamicRegionSystem.INSTANCE.getNeighbours(spawnRegion)
                .filter(r -> r.getType() == RegionType.PATH || r.getType() == RegionType.SPAWN_EXTENSION || r.getType() == RegionType.SPAWN_PATH)
                .collect(Collectors.toList());

        if (neighbours.size() == 8) {
            Bukkit.getWorlds().get(0).setSpawnLocation(BIG_WORLD_SPAWN);

            Region.Area area = spawnRegion.getArea();
            Point minPoint = area.getMinPoint(false).subtract(19, 0, 19);
            Clipboard clipboard = FlatteningWrapper.impl.loadSchematic(RESET_FILE);
            BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
            BlockVector3 to = minPoint.toBlockVector3().subtract(offset);
            clipboard.paste(BukkitAdapter.adapt(Bukkit.getWorlds().get(0)), to);

            if (spawnRegion != region) {
                RegionUtils.message(spawnRegion, "REGION_RESET_RESETED");
            }
            neighbours.forEach(r -> {
                if (r != region) {
                    RegionUtils.message(r, "REGION_RESET_RESETED");
                }
            });
            return;
        } else {
            Bukkit.getWorlds().get(0).setSpawnLocation(SMALL_WORLD_SPAWN);
        }

        internalReset(spawnRegion, spawnRegion != region);
        DynamicRegionSystem.INSTANCE.getNeighbours(spawnRegion)
                .forEach(r -> internalReset(r, r != region));
    }

    private void internalReset(Region region, boolean message) {
        Region.Area area = region.getArea();
        Point minPoint = area.getMinPoint(false);
        Clipboard clipboard = FlatteningWrapper.impl.loadSchematic(area.getResetFile());
        BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
        BlockVector3 to = minPoint.toBlockVector3().subtract(offset);
        clipboard.paste(BukkitAdapter.adapt(Bukkit.getWorlds().get(0)), to);
        if (message) {
            RegionUtils.message(region, "REGION_RESET_RESETED");
        }
    }
}
