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

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 de.steamwar.bausystem.region.dynamic.spawn.SpawnAreaTile;
import lombok.NonNull;
import org.bukkit.Bukkit;

import java.io.File;

public class PathRegion extends DynamicRegion {

    private static final File SECTIONS_DIR = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "sections");
    private static final File RESET_FILE = new File(SECTIONS_DIR, "path/Path.schem"); // Temp

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

    public PathRegion(int minX, int minZ) {
        super(minX, minZ);
        RegionDataRepository.loadFlagStorage(this, flagStorage);
        if (minX >= -28 && minX <= 10 && minZ >= -28 && minZ <= 10) {
            area = new SpawnAreaTile(minX, minZ, VariantSelector.File(RESET_FILE), this);
        } else {
            area = new PathAreaTile(minX, minZ, this);
        }
    }

    public PathRegion(RegionConstructorData regionConstructorData) {
        super(regionConstructorData);
        RegionDataRepository.loadFlagStorage(this, flagStorage);
        if (minX >= -28 && minX <= 10 && minZ >= -28 && minZ <= 10) {
            area = new SpawnAreaTile(minX, minZ, VariantSelector.File(RESET_FILE), this);
        } else {
            area = new PathAreaTile(minX, minZ, this);
        }
    }

    @Override
    public void update(DynamicRegion updateFrom) {
        if (area instanceof PathAreaTile) {
            area.reset(null, false);
        }
    }

    @Override
    public void setFlags(DefaultFlagStorage flags) {
    }

    @Override
    public @NonNull RegionType getType() {
        if (area instanceof SpawnAreaTile) {
            return RegionType.SPAWN_EXTENSION;
        } else {
            return RegionType.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;
    }
}
