/*
 * 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.*;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.NonNormalFlagStorage;
import de.steamwar.bausystem.region.dynamic.RegionConstructorData;
import de.steamwar.bausystem.region.dynamic.RegionDataRepository;
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 FlagStorage flagStorage = new NonNormalFlagStorage();
    private final Area area;
    private final GameModeConfig config = new GameModeConfig(null);

    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 File getResetFile(int minX, int minZ) {
        if (minX == -28) {
            return RESET_FILE_WEST;
        } else if (minX == 10) {
            return RESET_FILE_EAST;
        } else if (minZ == -28) {
            return RESET_FILE_NORTH;
        } else if (minZ == 10) {
            return RESET_FILE_SOUTH;
        }
        throw new IllegalArgumentException("Invalid minX: " + minX + ", minZ: " + minZ);
    }

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

    @Override
    public void setFlags(FlagStorage flags) {
    }

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

    @Override
    public @NonNull FlagStorage 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 config;
    }

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