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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.team.boundary.BoundaryViewer;
import de.steamwar.bausystem.region.Prototype;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserPerm;
import net.md_5.bungee.api.chat.ClickEvent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import yapion.hierarchy.output.StringOutput;
import yapion.hierarchy.types.YAPIONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Linked
public class SkinCommand extends SWCommand {

    public SkinCommand() {
        super("skin");
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        SteamwarUser user = SteamwarUser.get(p.getUniqueId());

        if (!user.hasPerm(UserPerm.ADMINISTRATION)) {
            return;
        }

        BauSystem.MESSAGE.send("SKIN_HELP", p);
    }

    @Register
    public void createCommand(Player p, @OptionalValue("") @Mapper("force") String force, @Mapper("kuerzel") String typeKuerzel, @Mapper("creator") String creator, String... names) {
        SteamwarUser user = SteamwarUser.get(p.getUniqueId());

        if (!user.hasPerm(UserPerm.ADMINISTRATION)) {
            return;
        }
        Region region = Region.getRegion(p.getLocation());
        if (region.isGlobal()) {
            BauSystem.MESSAGE.send("SKIN_NO_REGION", p);
            return;
        }

        String name = String.join(" ", names);
        File arenaFile = new File("sections19/" + name + "/" + typeKuerzel + "Arena.schem");
        File testblockFile = region.hasType(RegionType.TESTBLOCK) ? new File("sections19/" + name + "/" + typeKuerzel + "Testblock.schem") : null;

        arenaFile.getParentFile().mkdirs();
        if (testblockFile != null) {
            testblockFile.getParentFile().mkdirs();
        }
        if (arenaFile.exists() && force.equals("")) {
            BauSystem.MESSAGE.send("SKIN_ALREADY_EXISTS", p);
            return;
        }
        if (testblockFile != null && testblockFile.exists() && force.equals("")) {
            BauSystem.MESSAGE.send("SKIN_ALREADY_EXISTS", p);
            return;
        }

        Region.copy(region.getMinPoint(), region.getMaxPoint(), arenaFile);
        if (testblockFile != null) {
            Region.copy(region.getMinPointTestblock(), region.getMaxPointTestblock(), testblockFile);
        }

        YAPIONObject yapionObject = new YAPIONObject();
        yapionObject.add("name", name);
        if (!creator.equals("public")) {
            yapionObject.add("creator", creator);
        }
        yapionObject.add("type", typeKuerzel);
        yapionObject.add("schematic", arenaFile.getPath());
        if (testblockFile != null) {
            yapionObject.add("testblockSchematic", testblockFile.getPath());
        }
        BauSystem.MESSAGE.send("SKIN_MESSAGE", p, BauSystem.MESSAGE.parse("SKIN_MESSAGE_HOVER", p), new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, yapionObject.toYAPION(new StringOutput(true)).getResult()));
    }

    @Register("boundary")
    public void showRegionBoundaries(Player p) {
        SteamwarUser user = SteamwarUser.get(p.getUniqueId());

        if (!user.hasPerm(UserPerm.ADMINISTRATION)) {
            return;
        }
        if (BoundaryViewer.viewers.contains(p)) {
            BoundaryViewer.viewers.remove(p);
        } else {
            BoundaryViewer.viewers.add(p);
        }
    }

    @Mapper(value = "kuerzel", local = true)
    public static TypeMapper<String> kurzelMapper() {
        return new TypeMapper<String>() {
            @Override
            public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                List<String> current = new ArrayList<>();
                Prototype.getPrototypes().forEach(p -> {
                    current.add(p.getName().toUpperCase().replace("_", ""));
                });
                return current;
            }

            @Override
            public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                return s;
            }
        };
    }

    @Mapper(value = "force", local = true)
    public static TypeMapper<String> forceMapper() {
        return new TypeMapper<String>() {
            @Override
            public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                List<String> current = new ArrayList<>();
                current.add("-f");
                return current;
            }

            @Override
            public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                if (s.equals("-f")) {
                    return s;
                }
                if (s.equals("")) {
                    return "";
                }
                return null;
            }
        };
    }

    @Mapper(value = "creator", local = true)
    public static TypeMapper<String> creatorMapper() {
        return new TypeMapper<String>() {
            @Override
            public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                List<String> current = new ArrayList<>();
                current.add("public");
                Bukkit.getOnlinePlayers().forEach(player -> {
                    current.add(player.getName());
                });
                return current;
            }

            @Override
            public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                Set<String> current = new HashSet<>();
                current.add("public");
                Bukkit.getOnlinePlayers().forEach(player -> {
                    current.add(player.getName());
                });
                if (current.contains(s)) {
                    return s;
                }
                return null;
            }
        };
    }
}
