/*
 * 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 de.steamwar.bausystem.region.GameModeConfig;
import de.steamwar.bausystem.region.RegionHistory;
import de.steamwar.bausystem.region.RegionType;
import de.steamwar.bausystem.region.dynamic.*;
import lombok.NonNull;
import org.bukkit.Bukkit;

import java.io.File;

public class SpawnPathRegion extends DynamicRegion {

    private static final File SECTIONS_DIR = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "sections");
    private static final File RESET_FILE_NORTH = new File(SECTIONS_DIR, "spawn/SpawnNorth.schem");
    private static final File RESET_FILE_SOUTH = new File(SECTIONS_DIR, "spawn/SpawnSouth.schem");
    private static final File RESET_FILE_WEST = new File(SECTIONS_DIR, "spawn/SpawnWest.schem");
    private static final File RESET_FILE_EAST = new File(SECTIONS_DIR, "spawn/SpawnEast.schem");

    private DefaultFlagStorage flagStorage = new NonNormalFlagStorage();
    private final Area area;

    public SpawnPathRegion(int minX, int minZ) {
        super(minX, minZ);
        RegionDataRepository.loadFlagStorage(this, flagStorage);
        area = new SpawnAreaTile(minX, minZ, getResetFile(minX, minZ), this);
    }

    public SpawnPathRegion(RegionConstructorData regionConstructorData) {
        super(regionConstructorData);
        RegionDataRepository.loadFlagStorage(this, flagStorage);
        area = new SpawnAreaTile(minX, minZ, getResetFile(minX, minZ), this);
    }

    private static VariantSelector getResetFile(int minX, int minZ) {
        if (minX == -28) {
            return VariantSelector.File(RESET_FILE_WEST);
        } else if (minX == 10) {
            return VariantSelector.File(RESET_FILE_EAST);
        } else if (minZ == -28) {
            return VariantSelector.File(RESET_FILE_NORTH);
        } else if (minZ == 10) {
            return VariantSelector.File(RESET_FILE_SOUTH);
        }
        throw new IllegalArgumentException("Invalid minX: " + minX + ", minZ: " + minZ);
    }

    @Override
    public void update(DynamicRegion updateFrom) {
        // TODO: Implement
    }

    @Override
    public void setFlags(DefaultFlagStorage flags) {
    }

    @Override
    public @NonNull RegionType getType() {
        return RegionType.SPAWN_PATH;
    }

    @Override
    public @NonNull DefaultFlagStorage getFlags() {
        return flagStorage;
    }

    @Override
    public @NonNull Area getArea() {
        return area;
    }

    @Override
    public @NonNull Area getBuildArea() {
        return Area.EMPTY;
    }

    @Override
    public @NonNull Area getTestblockArea() {
        return Area.EMPTY;
    }

    @Override
    public @NonNull GameModeConfig getGameModeConfig() {
        return GameModeConfig.EMPTY;
    }

    @Override
    public @NonNull RegionHistory getHistory() {
        return RegionHistory.EMPTY;
    }
}
