package gluu.scim2.client.factory;

import gluu.scim2.client.TestModeScimClient;
import gluu.scim2.client.rest.ClientSideService;
import gluu.scim2.client.DummyClient;
//import org.xdi.oxauth.model.util.SecurityProviderUtility;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Created by eugeniuparvan on 2/20/17.
 * Updated by jgomer on 2017-07-13
 */
public class ScimClientFactory {

    //TODO: reduce to a single getClient method
    //TODO: installBCProvider()
    public static ClientSideService getClient(String domain, String umaMetaDataUrl, String umaAatClientId,
                                              String umaAatClientJksPath, String umaAatClientJksPassword, String umaAatClientKeyId) {
        //SecurityProviderUtility.installBCProvider();
        //return new UmaScimClient(domain, umaAatClientId, umaAatClientJksPath, umaAatClientJksPassword, umaAatClientKeyId);
        return null;
    }

    /**
     *
     * @param domain See TestModeScimClient constructor
     * @param OIDCMetadataUrl
     * @return An object for using SCIM service in test mode only (not UMA protected)
     * @throws Exception Instantiation abnormality
     * @since 3.1.0
     */
    public static ClientSideService getTestClient(String domain, String OIDCMetadataUrl) throws Exception {
        //SecurityProviderUtility.installBCProvider();
        Class clazz=ClientSideService.class;
        InvocationHandler handler = new TestModeScimClient(domain, OIDCMetadataUrl);
        return (ClientSideService) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, handler);

    }

    public static ClientSideService getDummyClient(String domain) throws Exception{

        Class clazz=ClientSideService.class;
        InvocationHandler handler = new DummyClient(domain);
        return (ClientSideService) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, handler);

    }

}

@@ -103,30 +103,21 @@
 ent.
-rest.ClientSideService
+UmaScimClient
 ;%0Aim
@@ -152,16 +152,74 @@
 Client;%0A
+import org.xdi.oxauth.model.util.SecurityProviderUtility;%0A
 //import
@@ -465,16 +465,19 @@
 tory
+%3CT%3E
  %7B%0A%0A    
 //TO
@@ -476,64 +476,167 @@
     
-//TODO: reduce to a single getClient method%0A    //TODO: 
+private Class%3CT%3E serviceInterface;%0A    private Class%3C?%3E interfaces%5B%5D;%0A%0A    public ScimClientFactory(Class %3CT%3E interfaceClass)%7B%0A        SecurityProviderUtility.
 inst
@@ -654,161 +654,171 @@
 er()
+;
 %0A    
-public static ClientSideService getClient(String domain, String umaMetaDataUrl, String umaAatClientId,%0A                                             
+    serviceInterface=interfaceClass;%0A        interfaces=new Class%3C?%3E%5B%5D%7BserviceInterface%7D;%0A    %7D%0A%0A    public T getClient(String domain, String umaAatClientId,
  Str
@@ -914,71 +914,35 @@
     
-//SecurityProviderUtility.installBCProvider();%0A        //return
+InvocationHandler handler =
  new
@@ -1065,121 +1065,266 @@
 urn 
-null;%0A    %7D%0A%0A    /**%0A     *%0A     * @param domain See TestModeScimClient constructor%0A     * @param OIDCMetadataUrl
+serviceInterface.cast(Proxy.newProxyInstance(serviceInterface.getClassLoader(), interfaces, handler));%0A    %7D%0A%0A    /**%0A     *%0A     * @param domain protocol + host of resource server%0A     * @param OIDCMetadataUrl URL of authorization servers' metadata document
 %0A   
@@ -1500,32 +1500,9 @@
 lic 
-static ClientSideService
+T
  get
@@ -1574,108 +1574,8 @@
 n %7B%0A
-        //SecurityProviderUtility.installBCProvider();%0A        Class clazz=ClientSideService.class;%0A
     
@@ -1670,36 +1670,38 @@
  return 
-(ClientSideService) 
+serviceInterface.cast(
 Proxy.ne
@@ -1707,37 +1707,48 @@
 ewProxyInstance(
-clazz
+serviceInterface
 .getClassLoader(
@@ -1750,37 +1750,26 @@
 ader(), 
-new Class%3C?%3E%5B%5D%7Bclazz%7D
+interfaces
 , handle
@@ -1762,34 +1762,34 @@
 rfaces, handler)
+)
 ;
-%0A
 %0A    %7D%0A%0A    publ
@@ -1795,32 +1795,9 @@
 lic 
-static ClientSideService
+T
  get
@@ -1844,54 +1844,8 @@
 ion%7B
-%0A%0A        Class clazz=ClientSideService.class;
 %0A   
@@ -1921,28 +1921,30 @@
 urn 
-(ClientSideService) 
+serviceInterface.cast(
 Prox
@@ -1962,21 +1962,32 @@
 nstance(
-clazz
+serviceInterface
 .getClas
@@ -2001,29 +2001,18 @@
 (), 
-new Class%3C?%3E%5B%5D%7Bclazz%7D
+interfaces
 , ha
@@ -2017,18 +2017,18 @@
 handler)
+)
 ;
-%0A
 %0A    %7D%0A%0A

Add generics to client factory