/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  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.features.simulator.data.observer;

import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorBlockAlignedElement;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.data.SimulatorPhase;
import de.steamwar.bausystem.features.simulator.execute.SimulatorAction;
import de.steamwar.bausystem.features.simulator.gui.SimulatorObserverGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;

public final class ObserverElement extends SimulatorBlockAlignedElement<ObserverPhase> {

    public ObserverElement(Vector position) {
        super(Material.OBSERVER, position);
    }

    @Override
    public String getName(Player player) {
        return "Observer";
    }

    @Override
    public Material getWorldMaterial() {
        return Material.OBSERVER;
    }

    @Override
    public Material getWorldDisabledMaterial() {
        return Material.GRAY_STAINED_GLASS;
    }

    public void toSimulatorActions(BiConsumer<Integer, SimulatorAction> tickStart, BiConsumer<Integer, SimulatorAction> tickEnd) {
        if (disabled) return;
        phases.forEach(phase -> {
            phase.toSimulatorActions(position.clone(), tickStart, tickEnd);
        });

        int end = phases.stream().mapToInt(SimulatorPhase::getTickOffset).max().orElse(0) + 4;
        AtomicReference<BlockState> blockState = new AtomicReference<>();
        tickStart.accept(0, new SimulatorAction(-100, 1) {
            @Override
            public void accept(World world) {
                Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
                blockState.set(block.getState());
                block.setType(Material.OBSERVER, false);
            }
        });
        tickEnd.accept(end, new SimulatorAction(0, 1) {
            @Override
            public void accept(World world) {
                BlockState oldState = blockState.get();
                if (oldState != null) {
                    oldState.update(true, true);
                } else {
                    Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
                    block.setType(Material.AIR);
                }
            }
        });
    }

    @Override
    public void open(Player player, Simulator simulator, SimulatorGroup group, SimulatorBaseGui back) {
        new SimulatorObserverGui(player, simulator, group, this, back).open();
    }

    @Override
    public String getType() {
        return "Observer";
    }
}
