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

import de.steamwar.velocitycore.ArenaMode;
import de.steamwar.messages.Message;
import de.steamwar.velocitycore.ServerStarter;
import de.steamwar.velocitycore.inventory.SWItem;
import de.steamwar.velocitycore.inventory.SWListInv;
import de.steamwar.velocitycore.inventory.SWStreamInv;
import de.steamwar.command.SWCommand;
import de.steamwar.messages.PlayerChatter;
import de.steamwar.sql.*;

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

public class ReplayCommand extends SWCommand {

    public ReplayCommand() {
        super("replay");
    }

    @Register
    public void genericCommand(PlayerChatter sender, @OptionalValue("") String optionalMap) {
        if (PunishmentCommand.isPunishedWithMessage(sender, Punishment.PunishmentType.NoFightServer))
            return;

        new SWStreamInv<>(sender, new Message("REPLAY_TITLE"), (click, fight) -> {
            ArenaMode mode = ArenaMode.getBySchemType(fight.getSchemType());
            ServerStarter starter = new ServerStarter().replay(fight.getFightID()).blueLeader(sender.getPlayer());

            String map = mode.getRandomMap();
            if (!optionalMap.equals("")) {
                String tMap = mode.hasMap(optionalMap);
                if (tMap != null) map = tMap;
            }

            if (sender.user().hasPerm(UserPerm.MODERATION) && click.isShiftClick() && fight.replayExists()) {
                starter.test(mode, map, sender.getPlayer()).start();
            } else if(!fight.replayAllowed()) {
                sender.system("REPLAY_UNAVAILABLE");
            } else {
                starter.arena(mode, map).start();
            }
        }, page -> Fight.getPage(page, 45).stream().map(fight -> new SWListInv.SWListEntry<>(getFightItem(fight), fight)).toList()).open();
    }

    private SWItem getFightItem(Fight fight) {
        SchematicType type = fight.getSchemType();
        SWItem item = new SWItem(type != null ? type.getMaterial() : "BARRIER", parseLeader(fight.getBlueLeader(), fight.getBluePlayers().size(), fight.getWin() == 1));

        List<Message> lore = new ArrayList<>();
        lore.add(parseLeader(fight.getRedLeader(), fight.getRedPlayers().size(), fight.getWin() == 2));
        lore.add(new Message("REPLAY_TIME", fight.getStartTime()));
        lore.add(new Message("SPACER"));
        lore.add(new Message("REPLAY_SERVER", fight.getServer()));
        if(!fight.replayAllowed())
            lore.add(new Message("REPLAY_UNAVAILABLE"));
        item.setLore(lore);

        if(fight.replayAllowed())
            item.setEnchanted(true);

        return item;
    }

    private Message parseLeader(SteamwarUser leader, int players, boolean winner) {
        return new Message("REPLAY_" + (players > 1 ? "" : "SOLO_") + (winner ? "WINNER" : "LOSER"), leader.getUserName(), players - 1);
    }
}
