package io.corbel.resources.rem.eventbus;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.corbel.event.ResourceEvent;
import io.corbel.eventbus.EventHandler;
import io.corbel.resources.rem.service.AclResourcesService;

public class AclConfigurationEventHandler implements EventHandler<ResourceEvent> {

    private AclResourcesService aclResourcesService;
    private final Pattern collectionPattern = Pattern.compile("^(?:.*/)?[\\w-_]+?(?::(?<collection>[\\w-_:]+))?$");
    private final String aclAdminCollection;

    private static final String ALL = "@ALL";

    public AclConfigurationEventHandler(String aclAdminCollection) {
        this.aclAdminCollection = aclAdminCollection;
    }

    public void setAclResourcesService(AclResourcesService aclResourcesService) {
        this.aclResourcesService = aclResourcesService;
    }

    @Override
    public void handle(ResourceEvent event) {

        if (!event.getType().equals(aclAdminCollection)) {
            return;
        }

        if (event.getResourceId().equals(ALL)) {
            aclResourcesService.refreshRegistry();
            return;
        }

        switch (event.getAction()) {
            case CREATE:
                extractUriPattern(event.getResourceId()).ifPresent(aclResourcesService::addAclConfiguration);
                break;
            case DELETE:
                extractUriPattern(event.getResourceId()).ifPresent(aclResourcesService::removeAclConfiguration);
        }

    }

    @Override
    public Class<ResourceEvent> getEventType() {
        return ResourceEvent.class;
    }

    private Optional<String> extractUriPattern(String resourceId) {
        Matcher matcher = collectionPattern.matcher(resourceId);

        if (!matcher.matches()) {
            return Optional.empty();
        }

        return Optional.ofNullable(matcher.group("collection")).filter(c -> !c.isEmpty());
    }

}

@@ -40,100 +40,8 @@
 s;%0A%0A
-import java.util.Optional;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 impo
@@ -175,16 +175,90 @@
 rvice;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0A%0A
 public c
@@ -390,124 +390,8 @@
 ce;%0A
-    private final Pattern collectionPattern = Pattern.compile(%22%5E(?:.*/)?%5B%5C%5Cw-_%5D+?(?::(?%3Ccollection%3E%5B%5C%5Cw-_:%5D+))?$%22);%0A
     
@@ -1026,32 +1026,75 @@
 eturn;%0A        %7D
+%0A        String id = event.getResourceId();
 %0A%0A        switch
@@ -1161,59 +1161,198 @@
     
-extractUriPattern(event.getResourceId()).ifPresent(
+// Why id contains entire url in event? @see DefaultResourcesService line 98%0A                String onlyId = id.substring(id.lastIndexOf(%22/%22) + 1);%0A                try %7B%0A                    
 aclR
@@ -1366,18 +1366,17 @@
 sService
-::
+.
 addAclCo
@@ -1390,126 +1390,259 @@
 tion
-);%0A                break;%0A            case DELETE:%0A                extractUriPattern(event.getResourceId()).ifPresent(
+(URLDecoder.decode(onlyId.substring(onlyId.indexOf(%22:%22) + 1), %22UTF8%22));%0A                %7D catch (UnsupportedEncodingException e) %7B%0A                    // Never happends%0A                %7D%0A                break;%0A            case DELETE:%0A                
 aclR
@@ -1660,10 +1660,9 @@
 vice
-::
+.
 remo
@@ -1679,16 +1679,50 @@
 guration
+(id.substring(id.indexOf(%22:%22) + 1)
 );%0A     
@@ -1844,320 +1844,6 @@
  %7D%0A%0A
-    private Optional%3CString%3E extractUriPattern(String resourceId) %7B%0A        Matcher matcher = collectionPattern.matcher(resourceId);%0A%0A        if (!matcher.matches()) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        return Optional.ofNullable(matcher.group(%22collection%22)).filter(c -%3E !c.isEmpty());%0A    %7D%0A%0A
 %7D%0A

allow regexp with acl management api too