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

import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorElement;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.gui.SimulatorTNTGui;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import yapion.hierarchy.types.YAPIONObject;

public final class TNTElement extends SimulatorElement<TNTPhase> {

    public static final double SUB_PIXEL = 0.00999999046326;
    @Getter
    private final Vector alignment = new Vector();

    public TNTElement(Vector position) {
        super(Material.TNT, position);
    }

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

    public void move(double x, double y, double z) {
        position.setX(position.getX() + x);
        position.setY(position.getY() + y);
        position.setZ(position.getZ() + z);
    }

    public void alignX(int direction) {
        position.setX(position.getX() - SUB_PIXEL * alignment.getX());
        alignment.setX(direction);
        position.setX(position.getX() + SUB_PIXEL * alignment.getX());
    }

    public void alignZ(int direction) {
        position.setZ(position.getZ() - SUB_PIXEL * alignment.getZ());
        alignment.setZ(direction);
        position.setZ(position.getZ() + SUB_PIXEL * alignment.getZ());
    }

    @Override
    public SWItem toItem(Player player, InvCallback invCallback) {
        long sum = phases.stream().mapToInt(TNTPhase::getCount).sum();
        SWItem swItem = super.toItem(player, invCallback);
        swItem.getItemStack().setAmount((int) Math.min(64, Math.max(1, sum)));
        swItem.setName("§e" + getName(player) + "§8:§7 " + sum);
        return swItem;
    }

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

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

    @Override
    public boolean canBeInGroup(SimulatorGroup simulatorGroup) {
        return simulatorGroup.getElements().stream().allMatch(TNTElement.class::isInstance);
    }

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

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

    @Override
    public void saveExtra(YAPIONObject elementObject) {
        elementObject.add("alignmentX", alignment.getX());
        elementObject.add("alignmentZ", alignment.getZ());
    }

    @Override
    public void loadExtra(YAPIONObject elementObject) {
        alignment.setX(elementObject.getDoubleOrSetDefault("alignmentX", 0));
        alignment.setZ(elementObject.getDoubleOrSetDefault("alignmentZ", 0));
    }
}
