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

import com.sk89q.worldedit.EditSession;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.dynamic.VariantSelector;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;
import org.bukkit.Location;

import javax.annotation.Nullable;
import java.io.File;
import java.util.function.BiConsumer;

public class NormalArea implements Region.Area {

    public static final NormalArea EMPTY = new NormalArea(0, 0, 0, 0, 0, 0, null, false, null) {
        @Override
        public void reset(PasteBuilder pasteBuilder, boolean extension) {
        }

        @Override
        public boolean isEmpty() {
            return true;
        }

        @Override
        public boolean inRegion(Location location, boolean extension) {
            return false;
        }

        @Override
        public void forEachChunk(BiConsumer<Integer, Integer> executor) {
        }

        @Override
        public boolean isChunkOutside(int chunkX, int chunkZ) {
            return true;
        }
    };

    private final int minX;
    private final int minY;
    private final int minZ;
    private final int widthX;
    private final int widthY;
    private final int widthZ;
    private VariantSelector variantSelector;
    private final Point minPoint;
    private final Point maxPoint;
    private final Point copyPoint;
    private final boolean rotate;
    private final Region region;

    public NormalArea(int minX, int minY, int minZ, int widthX, int widthY, int widthZ, VariantSelector variantSelector, boolean rotate, Region region) {
        this.minX = minX;
        this.minY = minY;
        this.minZ = minZ;
        this.widthX = widthX;
        this.widthY = widthY;
        this.widthZ = widthZ;
        this.variantSelector = variantSelector;
        this.rotate = rotate;
        this.region = region;

        minPoint = new Point(minX, minY, minZ);
        maxPoint = new Point(minX + widthX - 1, minY + widthY - 1, minZ + widthZ - 1);
        copyPoint = minPoint.add(widthX / 2, widthY, widthZ / 2);
    }

    @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;
    }

    public NormalArea setResetFile(VariantSelector variantSelector) {
        this.variantSelector = variantSelector;
        return this;
    }

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

    @Override
    public void reset(PasteBuilder pasteBuilder, boolean extension) {
        EditSession editSession = pasteBuilder.minPoint(minPoint)
                .maxPoint(maxPoint)
                .rotate(rotate)
                .color(region.getFlags().get(Flag.COLOR).getWithDefault())
                .run();
        region.getHistory().remember(editSession);
    }
}
