package uk.co.drnaylor.minecraft.quickstart.internal;

import com.google.common.base.Preconditions;
import uk.co.drnaylor.minecraft.quickstart.QuickStart;
import uk.co.drnaylor.minecraft.quickstart.internal.annotations.Permissions;

import java.util.*;

public final class PermissionUtil {

    private final Permissions ps;
    private final String commandAlias;

    PermissionUtil(Permissions ps, String commandAlias) {
        Preconditions.checkNotNull(ps);
        this.ps = ps;
        this.commandAlias = commandAlias;
    }

    public Set<String> getBasePermissions() {
        Set<String> perms = ps.useDefault() ? getPermissionWithSuffix("base") : new HashSet<>();
        perms.addAll(Arrays.asList(ps.value()));
        return perms;
    }

    public Set<String> getWarmupPermissions() {
        Set<String> perms = ps.useDefaultWarmupExempt() ? getPermissionWithSuffix("exempt.warmup") : new HashSet<>();
        perms.addAll(Arrays.asList(ps.warmupExempt()));
        return perms;
    }

    public Set<String> getCooldownPermissions() {
        Set<String> perms = ps.useDefaultCooldownExempt() ? getPermissionWithSuffix("exempt.cooldown") : new HashSet<>();
        perms.addAll(Arrays.asList(ps.cooldownExempt()));
        return perms;
    }

    public Set<String> getCostPermissions() {
        Set<String> perms = ps.useDefaultCostExempt() ? getPermissionWithSuffix("exempt.cost") : new HashSet<>();
        perms.addAll(Arrays.asList(ps.cooldownExempt()));
        return perms;
    }

    public Set<String> getPermissionWithSuffix(String suffix) {
        Set<String> perms = new HashSet<>();
        StringBuilder perm = new StringBuilder(QuickStart.PERMISSIONS_PREFIX);
        if (!ps.root().isEmpty()) {
            perm.append(ps.root()).append(".");
        }

        perm.append(commandAlias).append(".");

        if (!ps.sub().isEmpty()) {
            perm.append(ps.sub()).append(".");
        }

        perms.add(perm.append(suffix).toString());

        if (ps.includeAdmin()) {
            perms.add(QuickStart.PERMISSIONS_ADMIN);
        }

        return perms;
    }
}

@@ -604,26 +604,8 @@
 ms =
- ps.useDefault() ?
  get
@@ -632,34 +632,16 @@
 (%22base%22)
- : new HashSet%3C%3E()
 ;%0A      
@@ -1570,32 +1570,68 @@
 ew HashSet%3C%3E();%0A
+%0A        if (ps.useDefault()) %7B%0A    
         StringBu
@@ -1693,32 +1693,36 @@
 REFIX);%0A        
+   
+ 
 if (!ps.root().i
@@ -1725,32 +1725,36 @@
 ().isEmpty()) %7B%0A
+    
             perm
@@ -1785,32 +1785,36 @@
 d(%22.%22);%0A        
+    
 %7D%0A%0A        perm.
@@ -1800,32 +1800,36 @@
      %7D%0A%0A        
+ 
+   
 perm.append(comm
@@ -1852,32 +1852,36 @@
 (%22.%22);%0A%0A        
+    
 if (!ps.sub().is
@@ -1895,32 +1895,36 @@
 ) %7B%0A            
+   
+ 
 perm.append(ps.s
@@ -1934,32 +1934,36 @@
 )).append(%22.%22);%0A
+    
         %7D%0A%0A     
@@ -1957,32 +1957,36 @@
      %7D%0A%0A        
+    
 perms.add(perm.a
@@ -2011,16 +2011,26 @@
 ring());
+%0A        %7D
 %0A%0A      

Fix code that broke test.