/*
 * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author GeyserMC
 * @link https://github.com/GeyserMC/Geyser
 */

package org.geysermc.geyser.level;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.erosion.util.BlockPositionIterator;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.data.game.setting.Difficulty;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

/**
 * Class that manages or retrieves various information
 * from the world. Everything in this class should be
 * safe to return null or an empty value in the event
 * that chunk caching or anything of the sort is disabled
 * on the standalone version of Geyser.
 */
public abstract class WorldManager {

    @NonNull
    public final BlockState blockAt(GeyserSession session, Vector3i vector) {
        return this.blockAt(session, vector.getX(), vector.getY(), vector.getZ());
    }

    @NonNull
    public BlockState blockAt(GeyserSession session, int x, int y, int z) {
        return BlockState.of(this.getBlockAt(session, x, y, z));
    }

    /**
     * Gets the Java block state at the specified location
     *
     * @param session the session
     * @param vector the position
     * @return the block state at the specified location
     */
    public final int getBlockAt(GeyserSession session, Vector3i vector) {
        return this.getBlockAt(session, vector.getX(), vector.getY(), vector.getZ());
    }

    /**
     * Gets the Java block state at the specified location
     *
     * @param session the session
     * @param x the x coordinate to get the block at
     * @param y the y coordinate to get the block at
     * @param z the z coordinate to get the block at
     * @return the block state at the specified location
     */
    public abstract int getBlockAt(GeyserSession session, int x, int y, int z);

    public final CompletableFuture<Integer> getBlockAtAsync(GeyserSession session, Vector3i vector) {
        return this.getBlockAtAsync(session, vector.getX(), vector.getY(), vector.getZ());
    }

    public CompletableFuture<Integer> getBlockAtAsync(GeyserSession session, int x, int y, int z) {
        return CompletableFuture.completedFuture(this.getBlockAt(session, x, y, z));
    }

    public int[] getBlocksAt(GeyserSession session, BlockPositionIterator iter) {
        int[] blocks = new int[iter.getMaxIterations()];
        for (; iter.hasNext(); iter.next()) {
            int networkId = this.getBlockAt(session, iter.getX(), iter.getY(), iter.getZ());
            blocks[iter.getIteration()] = networkId;
        }
        return blocks;
    }

    /**
     * Checks whether or not this world manager requires a separate chunk cache/has access to more block data than the chunk cache.
     * <p>
     * Some world managers (e.g. Spigot) can provide access to block data outside of the chunk cache, and even with chunk caching disabled. This
     * method provides a means to check if this manager has this capability.
     *
     * @return whether or not this world manager has access to more block data than the chunk cache
     */
    public abstract boolean hasOwnChunkCache();

    /**
     * Get the default game mode of the server
     *
     * @param session the player requesting the default game mode
     * @return the default game mode of the server, or Survival if unknown.
     */
    public abstract GameMode getDefaultGameMode(GeyserSession session);

    /**
     * Change the default game mode of the session's server
     *
     * @param session the player making the change
     * @param gameMode the new default game mode
     */
    public void setDefaultGameMode(GeyserSession session, GameMode gameMode) {
        session.sendCommandPacket("defaultgamemode " + gameMode.name().toLowerCase(Locale.ROOT));
    }

    /**
     * Change the difficulty of the Java server
     *
     * @param session The session of the user that requested the change
     * @param difficulty The difficulty to change to
     */
    public void setDifficulty(GeyserSession session, Difficulty difficulty) {
        session.sendCommandPacket("difficulty " + difficulty.name().toLowerCase(Locale.ROOT));
    }

    /**
     * Returns a list of biome identifiers available on the server.
     */
    public String @Nullable [] getBiomeIdentifiers(boolean withTags) {
        return null;
    }

    /**
     * Retrieves decorated pot sherds from the server. Used to ensure the data is not erased on animation sent
     * through the BlockEntityDataPacket.
     */
    public void getDecoratedPotData(GeyserSession session, Vector3i pos, Consumer<List<String>> apply) {
    }
}
