/*
 *   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.type.Repeater;
import org.bukkit.entity.Player;

import java.util.function.Consumer;

public class LoaderRepeater extends LoaderInteractionElement<LoaderRepeater.RepeaterSettingsEnum> {

    public LoaderRepeater(Location location) {
        super(location, RepeaterSettingsEnum.INTERACT, RepeaterSettingsEnum.NOOP, RepeaterSettingsEnum.values());
    }

    public enum RepeaterSettingsEnum implements LoaderSettingsEnum<Repeater, LoaderRepeater, RepeaterSettingsEnum> {

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

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

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

            }
        },

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

            @Override
            public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
                return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_REPEATER", "LOADER_INTERACTION_INTERACT", player));
            }

            @Override
            public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
                int repeaterDelay = blockData.getDelay();
                repeaterDelay++;
                if (repeaterDelay > 4) repeaterDelay = 1;
                blockData.setDelay(repeaterDelay);
                parent.update(blockData);
            }
        },

        DELAY_1 {
            @Override
            public int getPos() {
                return 4;
            }

            @Override
            public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 1));
                swItem.getItemStack().setAmount(1);
                return swItem;
            }

            @Override
            public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setDelay(1);
                parent.update(blockData);
            }
        },

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

            @Override
            public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 2));
                swItem.getItemStack().setAmount(2);
                return swItem;
            }

            @Override
            public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setDelay(2);
                parent.update(blockData);
            }
        },

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

            @Override
            public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 3));
                swItem.getItemStack().setAmount(3);
                return swItem;
            }

            @Override
            public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setDelay(3);
                parent.update(blockData);
            }
        },

        DELAY_4 {
            @Override
            public int getPos() {
                return 7;
            }

            @Override
            public SWItem menu(Player player, LoaderRepeater parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_SETTING_REPEATER", player, 4));
                swItem.getItemStack().setAmount(4);
                return swItem;
            }

            @Override
            public void execute(Location location, Repeater blockData, LoaderRepeater parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setDelay(4);
                parent.update(blockData);
            }
        }
    }

    @Override
    public SWItem menu(Player player) {
        return new SWItem(Material.REPEATER, translateItemName("LOADER_BUTTON_REPEATER", player));
    }

    @Override
    public boolean checkBlockInWorld() {
        return location.getBlock().getType() == Material.REPEATER;
    }
}
