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

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.LinkedList;

abstract class AbstractAutoLoader {

    abstract Player getPlayer();
    abstract boolean setRedstone(Location location, boolean active);
    abstract LinkedList<LoaderAction> getActions();
    abstract void resetLastActivation();
    abstract int getLastActivation();

    void print(String message, boolean withSize){
        if(withSize)
            getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message + " §8" + getActions().size()));
        else
            getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
    }

    abstract static class LoaderAction {

        final Location location;
        final AbstractAutoLoader loader;

        LoaderAction(AbstractAutoLoader loader, Location location){
            this.location = location;
            this.loader = loader;
            loader.getActions().add(this);
            loader.resetLastActivation();
        }

        abstract boolean perform();
        abstract int ticks();
    }

    static class RedstoneActivation extends LoaderAction{

        final boolean active;
        final int length;
        int status;

        RedstoneActivation(AbstractAutoLoader loader, Location location, int ticks, boolean active){
            super(loader, location);
            this.length = ticks;
            this.active = active;
            status = 0;
        }

        @Override
        public boolean perform() {
            status++;
            if(status < length)
                return false;

            if(!loader.setRedstone(location, active))
                return false;
            status = 0;
            return true;
        }

        @Override
        int ticks() {
            return 1;
        }
    }

    static class TemporaryActivation extends LoaderAction{

        final int length;
        int status;

        TemporaryActivation(AbstractAutoLoader loader, Location location, int ticks){
            super(loader, location);
            this.length = ticks;
            status = 0;
        }

        @Override
        public boolean perform() {
            if(status == 0){
                if(!loader.setRedstone(location, true))
                    return false;
            }else if(status == length){
                if(!loader.setRedstone(location, false))
                    return false;
                status = 0;
                return true;
            }
            status++;
            return false;
        }

        @Override
        int ticks() {
            return 1;
        }
    }
}
