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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.gui.GuiTraceShow;
import de.steamwar.bausystem.tracer.record.RecordStateMachine;
import de.steamwar.bausystem.tracer.show.ShowModeParameter;
import de.steamwar.bausystem.tracer.show.ShowModeParameterType;
import de.steamwar.bausystem.tracer.show.StoredRecords;
import de.steamwar.bausystem.tracer.show.TraceShowManager;
import de.steamwar.bausystem.tracer.show.mode.EntityShowMode;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.command.SWCommand;
import org.bukkit.entity.Player;

public class CommandTrace extends SWCommand {

    public CommandTrace() {
        super("trace");
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§etrace start §8- §7Startet die Aufnahme aller TNT-Positionen");
        p.sendMessage("§8/§etrace stop §8- §7Stoppt den TNT-Tracer");
        p.sendMessage("§8/§etrace toggleauto §8- §7Automatischer Aufnahmenstart");
        p.sendMessage("§8/§etrace show gui §8- §7Zeigt die Trace show gui");
        p.sendMessage("§8/§etrace show §8<§e-water§8|§e-interpolate-xz§8|§e-interpolate-y§8> §8- §7Zeigt alle TNT-Positionen");
        p.sendMessage("§8/§etrace hide §8- §7Versteckt alle TNT-Positionen");
        p.sendMessage("§8/§etrace delete §8- §7Löscht alle TNT-Positionen");
        // p.sendMessage("§8/§etrace list §8<§7FRAME-ID§8> §8- §7Listet alle TNT auf");
        // p.sendMessage("§8/§etrace gui §8- §7Zeigt die Trace Oberfläche an");
        // p.sendMessage("§7Optionale Parameter mit §8<>§7, Benötigte Parameter mit §8[]");
    }

    @Register({"start"})
    public void startCommand(Player p) {
        if (!permissionCheck(p)) return;
        RecordStateMachine.commandStart();
        p.sendMessage(BauSystem.PREFIX + "§aTNT-Tracer gestartet");
    }

    @Register({"stop"})
    public void stopCommand(Player p) {
        if (!permissionCheck(p)) return;
        RecordStateMachine.commandStop();
        p.sendMessage(BauSystem.PREFIX + "§cTNT-Tracer gestoppt");
    }

    @Register({"toggleauto"})
    public void toggleAutoCommand(Player p) {
        autoCommand(p);
    }

    @Register({"auto"})
    public void autoCommand(Player p) {
        if (!permissionCheck(p)) return;
        RecordStateMachine.commandAuto();
        p.sendMessage(BauSystem.PREFIX + RecordStateMachine.getRecordStatus().getAutoMessage());
    }

    @Register({"clear"})
    public void clearCommand(Player p) {
        deleteCommand(p);
    }

    @Register({"delete"})
    public void deleteCommand(Player p) {
        if (!permissionCheck(p)) return;
        StoredRecords.clear();
        p.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen gelöscht");
    }

    @Register({"show"})
    public void showCommand(Player p) {
        if (!permissionCheck(p)) return;
        TraceShowManager.show(p, new EntityShowMode(p, new ShowModeParameter()));
        p.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt");
    }

    @Register({"show"})
    public void showCommand(Player p, ShowModeParameterType... showModeParameterTypes) {
        if (!permissionCheck(p)) return;
        ShowModeParameter showModeParameter = new ShowModeParameter();
        for (ShowModeParameterType showModeParameterType : showModeParameterTypes) {
            showModeParameterType.getShowModeParameterConsumer().accept(showModeParameter);
        }
        TraceShowManager.show(p, new EntityShowMode(p, showModeParameter));
        p.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt");
    }

    @Register({"show", "gui"})
    public void showGuiCommand(Player p) {
        if (!permissionCheck(p)) return;
        GuiTraceShow.openGui(p);
    }

    @Register({"hide"})
    public void hideCommand(Player p) {
        if (!permissionCheck(p)) return;
        TraceShowManager.hide(p);
        p.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen ausgeblendet");
    }


    private boolean permissionCheck(Player player) {
        if (Welt.noPermission(player, Permission.WORLD)) {
            player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TNT-Tracer nutzen");
            return false;
        }
        return true;
    }
}
