/*
 * SPDX-License-Identifier: MIT
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2021 <Your name and contributors>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.github.games647.fastlogin.bukkit.task;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.geysermc.floodgate.api.player.FloodgatePlayer;

import com.github.games647.fastlogin.bukkit.BukkitLoginSession;
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
import com.github.games647.fastlogin.core.StoredProfile;
import com.github.games647.fastlogin.core.hooks.AuthPlugin;

public class FloodgateAuthTask implements Runnable {

    private final FastLoginBukkit plugin;
    private final Player player;
    private final FloodgatePlayer floodgatePlayer;

    public FloodgateAuthTask(FastLoginBukkit plugin, Player player, FloodgatePlayer floodgatePlayer) {
        this.plugin = plugin;
        this.player = player;
        this.floodgatePlayer = floodgatePlayer;
    }

    @Override
    public void run() {
        plugin.getLog().info(
                "Player {} is connecting through Geyser Floodgate.",
                player.getName());

        // check if the Bedrock player is linked to a Java account 
        boolean isLinked = floodgatePlayer.getLinkedPlayer() != null;

        AuthPlugin<Player> authPlugin = plugin.getCore().getAuthPluginHook();

        String autoLoginFloodgate = plugin.getCore().getConfig().get("autoLoginFloodgate").toString().toLowerCase();
        boolean autoRegisterFloodgate = plugin.getCore().getConfig().getBoolean("autoRegisterFloodgate");
        
        boolean isRegistered;
        try {
            isRegistered = authPlugin.isRegistered(player.getName());
        } catch (Exception e) {
            plugin.getLog().error(
                    "An error has occured while checking if player {} is registered",
                    player.getName());
            return;
        }
        
        if (!isRegistered && !autoRegisterFloodgate) {
            plugin.getLog().info(
                    "Auto registration is disabled for Floodgate players in config.yml");
            return;
        }
        
        // logging in from bedrock for a second time threw an error with UUID
        StoredProfile profile = plugin.getCore().getStorage().loadProfile(player.getName());
        if (profile == null) {
            profile = new StoredProfile(player.getUniqueId(), player.getName(), true, player.getAddress().toString());
        }

        BukkitLoginSession session = new BukkitLoginSession(player.getName(), isRegistered, profile);
        
        // enable auto login based on the value of 'autoLoginFloodgate' in config.yml
        session.setVerified(autoLoginFloodgate.equals("true")
                || (autoLoginFloodgate.equals("linked") && isLinked));

        // run login task
        Runnable forceLoginTask = new ForceLoginTask(plugin.getCore(), player, session);
        Bukkit.getScheduler().runTaskAsynchronously(plugin, forceLoginTask);
    }

}

@@ -1246,16 +1246,72 @@
 .task;%0A%0A
+import java.io.IOException;%0Aimport java.util.Optional;%0A%0A
 import o
@@ -1324,24 +1324,24 @@
 kit.Bukkit;%0A
-
 import org.b
@@ -1420,16 +1420,132 @@
 layer;%0A%0A
+import com.github.games647.craftapi.model.Profile;%0Aimport com.github.games647.craftapi.resolver.RateLimitException;%0A
 import c
@@ -2694,23 +2694,22 @@
         
-boolean
+String
  autoReg
@@ -2761,15 +2761,8 @@
 .get
-Boolean
 (%22au
@@ -2782,16 +2782,165 @@
 odgate%22)
+.toString().toLowerCase();%0A        String allowNameConflict = plugin.getCore().getConfig().get(%22allowFloodgateNameConflict%22).toString().toLowerCase()
 ;%0A      
@@ -3291,24 +3291,912 @@
    %0A        
+//decide if checks should be made for conflicting Java player names%0A        if (autoLoginFloodgate.equals(%22no-conflict%22)%0A                %7C%7C !isRegistered && autoRegisterFloodgate.equals(%22no-conflict%22)) %7B%0A            // check for conflicting Premium Java name%0A            Optional%3CProfile%3E premiumUUID = Optional.empty();%0A            try %7B%0A                premiumUUID = plugin.getCore().getResolver().findProfile(player.getName());%0A            %7D catch (IOException %7C RateLimitException e) %7B%0A                plugin.getLog().error(%0A                        %22Could not check wether Floodgate Player %7B%7D's name conflits a premium Java player's name.%22,%0A                        player.getName());%0A                return;%0A            %7D%0A%0A            //stop execution if player's name is conflicting%0A            if (premiumUUID.isPresent()) %7B%0A                return;%0A            %7D%0A        %7D%0A%0A        
 if (!isRegis
@@ -4204,17 +4204,16 @@
 ered && 
-!
 autoRegi
@@ -4225,16 +4225,32 @@
 loodgate
+.equals(%22false%22)
 ) %7B%0A    

Add 'no-conflict' option to some Floodgate config entries