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

import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.dynamic.VariantSelector;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;

import javax.annotation.Nullable;
import java.io.File;

public class SpecialAreaTile implements Region.Area {

    private final int minX;
    private final int minZ;
    private final VariantSelector variantSelector;

    private final Point minPoint;
    private final Point maxPoint;
    private final Point copyPoint;

    public SpecialAreaTile(int minX, int minZ, VariantSelector variantSelector) {
        this.minX = minX;
        this.minZ = minZ;
        this.variantSelector = variantSelector;

        minPoint = new Point(minX, 0, minZ);
        maxPoint = new Point(minX + 18, 255, minZ + 18);
        copyPoint = new Point(minX + 9, 0, minZ + 9);
    }

    @Override
    public @NonNull Point getMinPoint(boolean extension) {
        return minPoint;
    }

    @Override
    public @NonNull Point getMaxPoint(boolean extension) {
        return maxPoint;
    }

    @Override
    public @NonNull Point getCopyPoint() {
        return copyPoint;
    }

    @Nullable
    @Override
    public File getResetFile() {
        return variantSelector.selectVariant(minX, minZ).orElseThrow();
    }

    @Override
    public void reset(PasteBuilder pasteBuilder, boolean extension) {
        pasteBuilder.minPoint(minPoint)
                .maxPoint(maxPoint)
                .run();
    }
}
