/*
 * 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;

import de.steamwar.bausystem.features.region.RegionCommand;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.TileUtils;
import de.steamwar.bausystem.region.dynamic.normal.display.MicroWarGear21DisplayRegion;
import de.steamwar.bausystem.region.dynamic.normal.display.MiniWarGear21DisplayRegion;
import de.steamwar.bausystem.region.dynamic.normal.display.WarGear21DisplayRegion;
import de.steamwar.bausystem.region.dynamic.normal.display.WarShip21DisplayRegion;
import de.steamwar.bausystem.region.dynamic.normal.work.MicroWarGear21WorkRegion;
import de.steamwar.bausystem.region.dynamic.normal.work.MiniWarGear21WorkRegion;
import de.steamwar.bausystem.region.dynamic.normal.work.WarGear21WorkRegion;
import de.steamwar.bausystem.region.dynamic.normal.work.WarShip21WorkRegion;
import de.steamwar.bausystem.region.dynamic.path.PathRegion;
import de.steamwar.bausystem.region.dynamic.special.DrySpecialRegion;
import de.steamwar.bausystem.region.dynamic.special.WetSpecialRegion;
import de.steamwar.bausystem.shared.Pair;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.SWCommand;
import lombok.RequiredArgsConstructor;
import org.bukkit.entity.Player;

import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@AbstractSWCommand.PartOf(RegionCommand.class)
public class DynamicRegionCommand extends SWCommand {

    public DynamicRegionCommand() {
        super("");
    }

    @Register({"dynamic", "place"})
    public void placeRegion(Player player, Placement placement) {
        Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
        if (!region.getType().isGlobal()) return;
        Pair<Integer, Integer> tile = TileUtils.fromLocation(player.getLocation());
        Pair<Integer, Integer> placePosition = placePosition(tile, placement);
        if (placePosition == null) return;
        Pair<Integer, Integer> min = TileUtils.toMinLocation(placePosition);

        DynamicRegion dynamicRegion = placement.constructor.apply(min.getKey(), min.getValue());
        dynamicRegion.getArea().reset(new PasteBuilder(new PasteBuilder.FileProvider(dynamicRegion.getArea().getResetFile())), false);
        DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).collect(Collectors.toList())
                .forEach(r -> r.update(dynamicRegion));
    }

    private Pair<Integer, Integer> placePosition(Pair<Integer, Integer> sourceTile, Placement placement) {
        Map<Pair<Integer, Integer>, Region> regionCache = new HashMap<>();
        Set<Pair<Integer, Integer>> seen = new HashSet<>();
        LinkedHashSet<Pair<Integer, Integer>> currentTile = new LinkedHashSet<>();
        currentTile.add(sourceTile);
        while (!currentTile.isEmpty() && currentTile.size() < (placement.widthX * 2 / 19) * (placement.widthZ * 2 / 19)) {
            Pair<Integer, Integer> tile = currentTile.removeFirst();
            if (!seen.add(tile)) continue;
            if (canPlace(tile, placement, regionCache)) {
                return tile;
            }

            for (int dx = -1; dx <= 1; dx++) {
                for (int dz = -1; dz <= 1; dz++) {
                    if (dx == 0 && dz == 0) continue;
                    Pair<Integer, Integer> nextTile = new Pair<>(tile.getKey() + dx, tile.getValue() + dz);
                    Region region = regionCache.computeIfAbsent(nextTile, tilePair -> Region.getRegion(TileUtils.toMinLocation(tilePair.getKey(), tilePair.getValue())));
                    if (region.getType().isGlobal()) currentTile.add(nextTile);
                }
            }
        }
        return null;
    }

    private boolean canPlace(Pair<Integer, Integer> tile, Placement placement, Map<Pair<Integer, Integer>, Region> regionCache) {
        for (int x = tile.getKey(); x < tile.getKey() + placement.widthX / TileUtils.tileSize; x++) {
            for (int z = tile.getValue(); z < tile.getValue() + placement.widthZ / TileUtils.tileSize; z++) {
                Region region = regionCache.computeIfAbsent(new Pair<>(x, z), tilePair -> Region.getRegion(TileUtils.toMinLocation(tilePair.getKey(), tilePair.getValue())));
                if (region.getType().isGlobal()) continue;
                return false;
            }
        }
        return true;
    }


    @RequiredArgsConstructor
    public enum Placement {
        Path(PathRegion::new, 19, 19),
        WetSpecial(WetSpecialRegion::new, 19, 19),
        DrySpecial(DrySpecialRegion::new, 19, 19),
        WarGearWork21(WarGear21WorkRegion::new, WarGear21WorkRegion.widthX, WarGear21WorkRegion.widthZ),
        WarGearDisplay21(WarGear21DisplayRegion::new, WarGear21DisplayRegion.widthX, WarGear21DisplayRegion.widthZ),
        MiniWarGearWork21(MiniWarGear21WorkRegion::new, MiniWarGear21WorkRegion.widthX, MiniWarGear21WorkRegion.widthZ),
        MiniWarGearDisplay21(MiniWarGear21DisplayRegion::new, MiniWarGear21DisplayRegion.widthX, MiniWarGear21DisplayRegion.widthZ),
        WarShipWork21(WarShip21WorkRegion::new, WarShip21WorkRegion.widthX, WarShip21WorkRegion.widthZ),
        WarShipDisplay21(WarShip21DisplayRegion::new, WarShip21DisplayRegion.widthX, WarShip21DisplayRegion.widthZ),
        MicroWarGearWork21(MicroWarGear21WorkRegion::new, MicroWarGear21WorkRegion.widthX, MicroWarGear21WorkRegion.widthZ),
        MicroWarGearDisplay21(MicroWarGear21DisplayRegion::new, MicroWarGear21DisplayRegion.widthX, MicroWarGear21DisplayRegion.widthZ),
        ;

        private final BiFunction<Integer, Integer, DynamicRegion> constructor;
        private final int widthX;
        private final int widthZ;
    }

    @Register({"dynamic", "delete"})
    public void deleteRegion(Player player) {
        Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
        if (region.getType().isCannotDelete()) return;
        ((DynamicRegion) region).delete();
    }
}
