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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.shieldprinting.BlockDataConfiguration;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class PowerableConfiguration implements BlockDataConfiguration<Powerable> {

    private Material material;
    private State state;

    public PowerableConfiguration(Material material) {
        this.material = material;
        this.state = State.FROM_COPY;
    }

    @Override
    public void previous() {
        switch (state) {
            case FROM_COPY:
                state = State.ALWAYS_OFF;
                break;
            case FROM_ORIGINAL:
                state = State.FROM_COPY;
                break;
            case ALWAYS_ON:
                state = State.FROM_ORIGINAL;
                break;
            case ALWAYS_OFF:
                state = State.ALWAYS_ON;
                break;
        }
    }

    @Override
    public void next() {
        switch (state) {
            case FROM_COPY:
                state = State.FROM_ORIGINAL;
                break;
            case FROM_ORIGINAL:
                state = State.ALWAYS_ON;
                break;
            case ALWAYS_ON:
                state = State.ALWAYS_OFF;
                break;
            case ALWAYS_OFF:
                state = State.FROM_COPY;
                break;
        }
    }

    @Override
    public SWItem get(Player player) {
        SWItem swItem = new SWItem(material, BauSystem.MESSAGE.parse("SHIELD_PRINTING_GUI_STATE_POWERABLE", player, material.name()));
        List<String> lore = new ArrayList<>();
        for (State value : State.values()) {
            lore.add(BauSystem.MESSAGE.parse((value == state) ? "SHIELD_PRINTING_GUI_STATE_ACTIVE" : "SHIELD_PRINTING_GUI_STATE_INACTIVE",
                    player, BauSystem.MESSAGE.parse("SHIELD_PRINTING_GUI_STATE_" + value.name(), player)));
        }
        lore.add("");
        lore.add(BauSystem.MESSAGE.parse("SHIELD_PRINTING_GUI_STATE_PREVIOUS", player));
        lore.add(BauSystem.MESSAGE.parse("SHIELD_PRINTING_GUI_STATE_NEXT", player));
        swItem.setLore(lore);
        return swItem;
    }

    @Override
    public void apply(Powerable copied, Powerable worldOriginal) {
        switch (state) {
            case FROM_COPY:
                break;
            case FROM_ORIGINAL:
                copied.setPowered(worldOriginal.isPowered());
                break;
            case ALWAYS_ON:
                copied.setPowered(true);
                break;
            case ALWAYS_OFF:
                copied.setPowered(false);
                break;
        }
    }

    private enum State {
        FROM_COPY,
        FROM_ORIGINAL,
        ALWAYS_ON,
        ALWAYS_OFF
    }
}
