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

import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;
import org.bukkit.Location;

import javax.annotation.Nullable;
import java.io.File;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.stream.Stream;

public interface Region {

    static Stream<Region> getRegions() {
        return RegionSystem.INSTANCE.getRegions();
    }

    static Region getRegion(Location location) {
        return RegionSystem.INSTANCE.get(location);
    }

    static Region getGlobalRegion() {
        return RegionSystem.INSTANCE.getGlobalRegion();
    }

    @NonNull
    UUID getID();

    @NonNull
    RegionType getType();

    @NonNull
    FlagStorage getFlags();

    @NonNull
    Area getArea();

    @NonNull
    Area getBuildArea();

    @NonNull
    Area getTestblockArea();

    @NonNull
    GameModeConfig getGameModeConfig();

    @NonNull
    RegionHistory getHistory();

    @NonNull
    RegionBackups getBackups();

    @NonNull
    RegionData getRegionData();

    interface Area {

        Area EMPTY = new Area() {
            @Override
            public boolean isEmpty() {
                return true;
            }

            @Override
            public Point getMinPoint(boolean extension) {
                return Point.ZERO;
            }

            @Override
            public Point getMaxPoint(boolean extension) {
                return Point.ZERO;
            }

            @Override
            public Point getCopyPoint() {
                return Point.ZERO;
            }

            @Override
            public boolean inRegion(Location location, boolean extension) {
                return false;
            }

            @Override
            public Clipboard copy(boolean extension) {
                return null;
            }

            @Override
            public File getResetFile() {
                return null;
            }

            @Override
            public void reset(PasteBuilder pasteBuilder, boolean extension) {
            }

            @Override
            public void forEachChunk(BiConsumer<Integer, Integer> executor) {
            }

            @Override
            public boolean isChunkOutside(int chunkX, int chunkZ) {
                return false;
            }
        };

        default boolean isEmpty() {
            return false;
        }

        @NonNull
        Point getMinPoint(boolean extension);

        @NonNull
        Point getMaxPoint(boolean extension);

        @NonNull
        Point getCopyPoint();

        default boolean inRegion(Location location, boolean extension) {
            Point minPoint = getMinPoint(extension);
            Point maxPoint = getMaxPoint(extension);
            if (location.getBlockX() < minPoint.getX() || location.getBlockX() > maxPoint.getX()) return false;
            if (location.getBlockY() < minPoint.getY() || location.getBlockY() > maxPoint.getY()) return false;
            if (location.getBlockZ() < minPoint.getZ() || location.getBlockZ() > maxPoint.getZ()) return false;
            return true;
        }

        @Nullable
        default Clipboard copy(boolean extension) {
            return FlatteningWrapper.impl.copy(getMinPoint(extension), getMaxPoint(extension), getCopyPoint());
        }

        @Nullable
        File getResetFile();

        void reset(PasteBuilder pasteBuilder, boolean extension);

        default void forEachChunk(BiConsumer<Integer, Integer> executor) {
            Point minPoint = getMinPoint(false);
            Point maxPoint = getMaxPoint(false);
            for (int x = (int) Math.floor(minPoint.getX() / 16.0); x <= (int) Math.ceil(maxPoint.getX() / 16.0); x++) {
                for (int z = (int) Math.floor(minPoint.getZ() / 16.0); z <= (int) Math.ceil(maxPoint.getZ() / 16.0); z++) {
                    executor.accept(x, z);
                }
            }
        }

        default boolean isChunkOutside(int chunkX, int chunkZ) {
            Point minPoint = getMinPoint(true);
            Point maxPoint = getMaxPoint(true);
            return Math.floor(minPoint.getX() / 16.0) > chunkX || chunkX >= Math.ceil(maxPoint.getX() / 16.0) ||
                    Math.floor(minPoint.getZ() / 16.0) > chunkZ || chunkZ >= Math.ceil(maxPoint.getZ() / 16.0);
        }
    }
}
