/*
 * 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 de.steamwar.bausystem.region.FlagOptional;
import de.steamwar.bausystem.region.RegionHistory;
import de.steamwar.bausystem.region.dynamic.*;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.TestblockMode;
import lombok.Getter;
import lombok.NonNull;

public abstract class WorkRegion extends DynamicRegion {

    @Getter
    private DefaultFlagStorage flags = new NormalFlagStorage();

    @Getter
    protected NormalArea area = NormalArea.EMPTY;
    protected NormalArea northArea = NormalArea.EMPTY;
    protected NormalArea southArea = NormalArea.EMPTY;

    protected VariantSelector frame;
    protected VariantSelector testblock;

    @Getter
    private final RegionHistory history = new RegionHistory.Impl(20);

    protected WorkRegion(int minX, int minZ) {
        super(minX, minZ);
        RegionDataRepository.loadFlagStorage(this, flags);
    }

    protected WorkRegion(RegionConstructorData regionConstructorData) {
        super(regionConstructorData);
        RegionDataRepository.loadFlagStorage(this, flags);
    }

    @Override
    public void update(DynamicRegion updateFrom) {
    }

    @Override
    public void setFlags(DefaultFlagStorage flags) {
        this.flags = flags;
    }

    @Override
    public @NonNull Area getBuildArea() {
        FlagOptional<TestblockMode> testblock = flags.get(Flag.TESTBLOCK);
        if (testblock.isWithDefault(TestblockMode.NO_VALUE)) {
            return Area.EMPTY;
        }
        if (testblock.isWithDefault(TestblockMode.SOUTH)) {
            return northArea.setResetFile(this.frame);
        } else {
            return southArea.setResetFile(this.frame);
        }
    }

    @Override
    public @NonNull Area getTestblockArea() {
        FlagOptional<TestblockMode> testblock = flags.get(Flag.TESTBLOCK);
        if (testblock.isWithDefault(TestblockMode.NO_VALUE)) {
            return Area.EMPTY;
        }
        if (testblock.isWithDefault(TestblockMode.SOUTH)) {
            return southArea.setResetFile(this.testblock);
        } else {
            return northArea.setResetFile(this.testblock);
        }
    }
}
