package com.devicehive.auth;

import com.devicehive.vo.DeviceVO;
import com.devicehive.vo.NetworkVO;

import java.net.InetAddress;
import java.util.Set;
import java.util.stream.Collectors;

/**
 */
public class JwtCheckPermissionsHelper {

    public static boolean checkPermissions(
            HivePrincipal hivePrincipal,
            HiveAction action,
            InetAddress clientIP,
            String clientDomain) {
        Set<HiveAction> permittedActions = hivePrincipal.getActions();
        if (!checkActionAllowed(action, permittedActions)) return false;
        if (!checkClientIpAllowed(clientIP, hivePrincipal)) return false;
        if (!checkDomainAllowed(clientDomain, hivePrincipal)) return false;
        if (!checkNetworksAllowed(hivePrincipal)) return false;
        if (!checkDeviceGuidsAllowed(hivePrincipal)) return false;
        return true;
    }

    private static boolean checkActionAllowed(HiveAction hiveAction, Set<HiveAction> permissions) {
        boolean result = false;
        if (permissions != null) result = permissions.contains(hiveAction);
        return result;
    }

    private static boolean checkClientIpAllowed(InetAddress clientIP, HivePrincipal principal) {
        boolean result = true;
        if (principal.getSubnets() != null) result = principal.getSubnets().contains(clientIP.toString());
        return result;
    }

    private static boolean checkDomainAllowed(String clientDomain, HivePrincipal principal) {
        boolean result = false;
        if (principal.getDomains() != null && !principal.getDomains().isEmpty())
            result = principal.getDomains().contains(clientDomain);
        return result;
    }

    private static boolean checkNetworksAllowed(HivePrincipal principal) {
        /*todo - add real check
        boolean result = false;
        Set<Long> networks = principal.getNetworks();
        if (networks != null) result = !networks.stream().map(NetworkVO::getId).collect(Collectors.toSet()).isEmpty();*/
        return true;
    }

    private static boolean checkDeviceGuidsAllowed(HivePrincipal principal) {
        /*todo - add real check
        boolean result = false;
        Set<String> devices = principal.getDevices();
        if (devices != null) result = !devices.stream().map(DeviceVO::getGuid).collect(Collectors.toSet()).isEmpty();*/
        return true;
    }

}

@@ -1218,30 +1218,25 @@
     
-boolean result = true;
+// fixme: subnets
 %0A   
@@ -1232,36 +1232,39 @@
 subnets%0A        
-if (
+return 
 principal.getSub
@@ -1270,33 +1270,62 @@
 bnets() 
-!
+=
 = null
-) result =
+ %7C%7C principal.getSubnets().isEmpty() %7C%7C
  princip
@@ -1361,43 +1361,9 @@
 ntIP
-.toString());%0A        return result
+)
 ;%0A  
@@ -1473,44 +1473,15 @@
     
-boolean result = false;%0A        if (
+return 
 prin
@@ -1503,20 +1503,19 @@
 s() 
-!
+=
 = null 
-&& !
+%7C%7C 
 prin
@@ -1546,30 +1546,11 @@
 ty()
-)%0A            result =
+ %7C%7C
  pri
@@ -1593,39 +1593,16 @@
 omain);%0A
-        return result;%0A
     %7D%0A%0A 

Fix checkClientIpAllowed and checkDomainAllowed