package io.github.aquerr.eaglefactions.common.managers;

import com.google.inject.Singleton;
import io.github.aquerr.eaglefactions.api.entities.Faction;
import io.github.aquerr.eaglefactions.api.entities.FactionMemberType;
import io.github.aquerr.eaglefactions.api.entities.FactionPermType;
import io.github.aquerr.eaglefactions.api.managers.PermsManager;

import java.util.UUID;

@Singleton
public class PermsManagerImpl implements PermsManager
{
    public PermsManagerImpl()
    {

    }

    @Override
    public boolean canBreakBlock(final UUID playerUUID, final Faction playerFaction, final Faction chunkFaction)
    {
        return checkFlag(playerUUID, playerFaction, chunkFaction, FactionPermType.DESTROY);
    }

    @Override
    public boolean canPlaceBlock(final UUID playerUUID, final Faction playerFaction, final Faction chunkFaction)
    {
        return checkFlag(playerUUID, playerFaction, chunkFaction, FactionPermType.PLACE);
    }

    @Override
    public boolean canInteract(final UUID playerUUID, final Faction playerFaction, final Faction chunkFaction)
    {
        return checkFlag(playerUUID, playerFaction, chunkFaction, FactionPermType.USE);
    }

    @Override
    public boolean canClaim(final UUID playerUUID, final Faction playerFaction)
    {
        return checkFlag(playerUUID, playerFaction, FactionPermType.CLAIM);
    }

    @Override
    public boolean canAttack(final UUID playerUUID, final Faction playerFaction)
    {
        return checkFlag(playerUUID, playerFaction, FactionPermType.CLAIM);
    }

    @Override
    public boolean canInvite(final UUID playerUUID, final Faction playerFaction)
    {
        return checkFlag(playerUUID, playerFaction, FactionPermType.INVITE);
    }

    private boolean checkFlag(final UUID playerUUID, final Faction playerFaction, final FactionPermType flagTypes)
    {
        final FactionMemberType memberType = playerFaction.getPlayerMemberType(playerUUID);
        return playerFaction.getPerms().get(memberType).get(flagTypes);
    }

    private boolean checkFlag(final UUID playerUUID, final Faction playerFaction, final Faction chunkFaction, final FactionPermType flagType)
    {
        if (playerFaction.getName().equals(chunkFaction.getName()))
        {
            final FactionMemberType memberType = chunkFaction.getPlayerMemberType(playerUUID);

            //Leaders has permission for everything.
            if (memberType == FactionMemberType.LEADER)
                return true;
            return chunkFaction.getPerms().get(memberType).get(flagType);
        }
        else if (playerFaction.getAlliances().contains(chunkFaction.getName()))
        {
            return chunkFaction.getPerms().get(FactionMemberType.ALLY).get(flagType);
        }
        else if (playerFaction.getTruces().contains(chunkFaction.getName()))
        {
            return chunkFaction.getPerms().get(FactionMemberType.TRUCE).get(flagType);
        }
        return false;
    }
}

@@ -630,36 +630,42 @@
     return check
-Flag
+Permission
 (playerUUID, pla
@@ -868,36 +868,42 @@
     return check
-Flag
+Permission
 (playerUUID, pla
@@ -1102,36 +1102,42 @@
     return check
-Flag
+Permission
 (playerUUID, pla
@@ -1303,36 +1303,42 @@
     return check
-Flag
+Permission
 (playerUUID, pla
@@ -1493,36 +1493,42 @@
     return check
-Flag
+Permission
 (playerUUID, pla
@@ -1691,20 +1691,26 @@
 rn check
-Flag
+Permission
 (playerU
@@ -1778,36 +1778,42 @@
 te boolean check
-Flag
+Permission
 (final UUID play
@@ -1980,16 +1980,93 @@
 rUUID);%0A
+        if (memberType == FactionMemberType.LEADER)%0A            return true;%0A
         
@@ -2165,12 +2165,18 @@
 heck
-Flag
+Permission
 (fin

Fix checking leader permission in PermsManager.