/*
 * 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.FlagStorage;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;
import lombok.Setter;

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

public class WorkArea extends NormalArea {

    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 File resetFile;
    private final Point minPoint;
    private final Point maxPoint;
    private final Point copyPoint;
    private final boolean rotate;
    private final Region region;

    public WorkArea(int minX, int minY, int minZ, int widthX, int widthY, int widthZ, File resetFile, boolean rotate, Region region) {
        this.minX = minX;
        this.minY = minY;
        this.minZ = minZ;
        this.widthX = widthX;
        this.widthY = widthY;
        this.widthZ = widthZ;
        this.resetFile = resetFile;
        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;
    }

    @Override
    public NormalArea setResetFile(File resetFile) {
        this.resetFile = resetFile;
        return this;
    }

    @Nullable
    @Override
    public File getResetFile() {
        return resetFile;
    }

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