/*
 * 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.loader.elements.impl;

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.Comparator;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.function.Consumer;

public class LoaderComparator extends LoaderInteractionElement<LoaderComparator.ComparatorSettingsEnum> {

    public LoaderComparator(Location location) {
        super(location, ComparatorSettingsEnum.INTERACT, ComparatorSettingsEnum.NOOP, ComparatorSettingsEnum.values());
    }

    public enum ComparatorSettingsEnum implements LoaderSettingsEnum<Comparator, LoaderComparator, ComparatorSettingsEnum> {
        NOOP {
            @Override
            public int getPos() {
                return 2;
            }

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

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

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

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

            @Override
            public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setMode(blockData.getMode() == Comparator.Mode.COMPARE ? Comparator.Mode.SUBTRACT : Comparator.Mode.COMPARE);
                parent.update(blockData);
            }
        },

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

            @Override
            public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
                return new SWItem(Material.COMPARATOR, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_COMPARE", player));
            }

            @Override
            public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setMode(Comparator.Mode.COMPARE);
                parent.update(blockData);
            }
        },

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

            @Override
            public SWItem menu(Player player, LoaderComparator parent, int power, long ticks) {
                return new SWItem(Material.COMPARATOR, translateItemName("LOADER_BUTTON_COMPARATOR", "LOADER_INTERACTION_SUBTRACT", player));
            }

            @Override
            public void execute(Location location, Comparator blockData, LoaderComparator parent, int power, long ticks, Consumer<Long> delay) {
                blockData.setMode(Comparator.Mode.SUBTRACT);
                parent.update(blockData);
            }
        }
    }

    @Override
    public SWItem menu(Player player) {
        SWItem swItem = new SWItem(Material.COMPARATOR, "§7Comparator");
        swItem.setLore(Arrays.asList("§7Modes§8: §e" + elements.size(), "§8", "§7Click to edit"));
        return swItem;
    }

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