/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.loader.elements.impl;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.AnaloguePowerable;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;

import java.util.function.Consumer;

public class LoaderMovement extends LoaderInteractionElement<LoaderMovement.MovementSettingsEnum> {

    private String name;
    private Material material;
    private boolean analogue;

    public LoaderMovement(Location location, String name, Material material) {
        super(location, MovementSettingsEnum.NO_WAIT_FOR, MovementSettingsEnum.NOOP, MovementSettingsEnum.values());
        this.name = name;
        this.material = material;
        this.analogue = location.getBlock().getBlockData() instanceof AnaloguePowerable;
    }

    public void setInitialTicks(long ticks) {
        if (ticks < 1) ticks = 1;
        extraTicks.set(currentShot, ticks);
    }

    public enum MovementSettingsEnum implements LoaderSettingsEnum<BlockData, LoaderMovement, MovementSettingsEnum> {

        NOOP {
            @Override
            public int getPos() {
                return 2;
            }

            @Override
            public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
                return new SWItem(Material.STRUCTURE_VOID, translateItemName(parent.name, "LOADER_INTERACTION_NOOP", player));
            }

            @Override
            public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
            }
        },

        NO_WAIT_FOR {
            @Override
            public int getPos() {
                return 5;
            }

            @Override
            public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
                SWItem swItem = new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_WAIT_FOR", player));
                swItem.getItemStack().setAmount((int) Math.min(ticks, 64));
                return swItem;
            }

            @Override
            public boolean hasPower(LoaderMovement parent) {
                return parent.analogue;
            }

            @Override
            public boolean hasTicks(LoaderMovement parent) {
                return true;
            }

            @Override
            public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
                if (blockData instanceof AnaloguePowerable) {
                    AnaloguePowerable analoguePowerable = (AnaloguePowerable) location.getBlock().getBlockData();
                    analoguePowerable.setPower(power);
                    parent.update(analoguePowerable);
                } else if (blockData instanceof Powerable) {
                    Powerable powerable = (Powerable) location.getBlock().getBlockData();
                    if (ticks < 0) {
                        powerable.setPowered(power > 0);
                    } else {
                        powerable.setPowered(true);
                    }
                    parent.update(powerable);
                }

                BauSystem.runTaskLater(BauSystem.getInstance(), () -> {
                    if (blockData instanceof AnaloguePowerable) {
                        AnaloguePowerable analoguePowerable = (AnaloguePowerable) blockData;
                        analoguePowerable.setPower(0);
                        parent.update(analoguePowerable);
                    } else {
                        Powerable powerable = (Powerable) blockData;
                        powerable.setPowered(false);
                        parent.update(powerable);
                    }
                }, ticks);
            }
        },

        WAIT_FOR {
            @Override
            public int getPos() {
                return 6;
            }

            @Override
            public SWItem menu(Player player, LoaderMovement parent, int power, long ticks) {
                SWItem swItem = new SWItem(parent.material, translateItemName(parent.name, "LOADER_INTERACTION_NO_WAIT_FOR", player));
                swItem.getItemStack().setAmount((int) Math.min(ticks, 64));
                return swItem;
            }

            @Override
            public boolean hasPower(LoaderMovement parent) {
                return parent.analogue;
            }

            @Override
            public boolean hasTicks(LoaderMovement parent) {
                return true;
            }

            @Override
            public void execute(Location location, BlockData blockData, LoaderMovement parent, int power, long ticks, Consumer<Long> delay) {
                NO_WAIT_FOR.execute(location, blockData, parent, power, ticks, delay);
                delay.accept(ticks);
            }
        }
    }

    @Override
    public SWItem menu(Player player) {
        return new SWItem(material, translateItemName(name, player));
    }

    @Override
    public boolean checkBlockInWorld() {
        if (material == Material.STRING && location.getBlock().getType() == Material.TRIPWIRE) return true;
        return location.getBlock().getType() == material;
    }
}
