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

import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.event.permission.PermissionsSetupEvent;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.persistent.Servertype;
import de.steamwar.persistent.Subserver;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserPerm;
import de.steamwar.velocitycore.commands.*;
import de.steamwar.velocitycore.discord.DiscordBot;
import de.steamwar.velocitycore.discord.util.DiscordRanks;
import de.steamwar.velocitycore.mods.ModUtils;
import net.kyori.adventure.text.event.ClickEvent;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class ConnectionListener extends BasicListener {

    private static final Set<UUID> newPlayers = new HashSet<>();

    public static void newPlayer(UUID player){
        newPlayers.add(player);
    }

    private static final Set<String> TEAM_PERMISSIONS = Set.of("velocity.command.glist");

    @Subscribe
    public void onPermissionSetup(PermissionsSetupEvent event) {
        event.setProvider(subject -> {
            if(!(subject instanceof Player player))
                return perm -> Tristate.TRUE;

            Set<UserPerm> perms = SteamwarUser.getOrCreate(player.getUniqueId(), player.getUsername(), ConnectionListener::newPlayer, WebpasswordCommand::changeUsername).perms();
            if(perms.contains(UserPerm.ADMINISTRATION))
                return perm -> Tristate.TRUE;
            else if(perms.contains(UserPerm.TEAM))
                return perm -> Tristate.fromBoolean(TEAM_PERMISSIONS.contains(perm));
            else
                return perm -> Tristate.FALSE;
        });
    }

    @Subscribe
    public void onPostLogin(PostLoginEvent event) {
        Player player = event.getPlayer();
        SteamwarUser user = SteamwarUser.get(player.getUniqueId());
        Chatter chatter = Chatter.of(player);

        if(user.hasPerm(UserPerm.CHECK))
            CheckCommand.sendReminder(chatter);

        for(Subserver subserver : Subserver.getServerList()) {
            if(subserver.getType() == Servertype.ARENA) {
                chatter.system("JOIN_ARENA", new Message("JOIN_ARENA_HOVER"), ClickEvent.runCommand("/arena " + subserver.getServer().getName()), subserver.getServer().getName());
            }
        }

        if(newPlayers.contains(player.getUniqueId())){
            Chatter.broadcast().system("JOIN_FIRST", player);
            newPlayers.remove(player.getUniqueId());
        }

        DiscordBot.withBot(bot -> DiscordRanks.update(user));
    }

    @Subscribe
    public void onDisconnect(DisconnectEvent e){
        ChallengeCommand.remove(e.getPlayer());
        MsgCommand.remove(e.getPlayer());
        ModUtils.getPlayerModMap().remove(e.getPlayer().getUniqueId());
        ModCommand.playerFilterType.remove(e.getPlayer().getUniqueId());
    }
}
