package it.smartcommunitylab.aac.apim;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import it.smartcommunitylab.aac.model.ClientAppBasic;

//ignore data from APIM for paramers, we can't properly deserialize it 
@JsonIgnoreProperties(value = { "parameters" }, allowGetters = true)
public class APIMClient {

    // TODO check with apim
    public static final String SEPARATOR = ",";

    private String clientId;
    private String clientSecret;
    private String clientSecretMobile;
    private String name;
    private String displayName;
    private String redirectUris;
    private Collection<String> grantedTypes;

    private boolean nativeAppsAccess;
    private Map<String, Map<String, Object>> providerConfigurations;
    private String mobileAppSchema;

    private Map<String, Boolean> identityProviders;
    private Map<String, Boolean> identityProviderApproval;

    private String userName;
    private String scope;

    private Map<String, String> parameters;

    public APIMClient() {
        this.parameters = Collections.emptyMap();
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getClientSecretMobile() {
        return clientSecretMobile;
    }

    public void setClientSecretMobile(String clientSecretMobile) {
        this.clientSecretMobile = clientSecretMobile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getRedirectUris() {
        return redirectUris;
    }

    public void setRedirectUris(String redirectUris) {
        this.redirectUris = redirectUris;
    }

    public Collection<String> getGrantedTypes() {
        return grantedTypes;
    }

    public void setGrantedTypes(Collection<String> grantedTypes) {
        this.grantedTypes = grantedTypes;
    }

    public boolean isNativeAppsAccess() {
        return nativeAppsAccess;
    }

    public void setNativeAppsAccess(boolean nativeAppsAccess) {
        this.nativeAppsAccess = nativeAppsAccess;
    }

    public Map<String, Map<String, Object>> getProviderConfigurations() {
        return providerConfigurations;
    }

    public void setProviderConfigurations(Map<String, Map<String, Object>> providerConfigurations) {
        this.providerConfigurations = providerConfigurations;
    }

    public String getMobileAppSchema() {
        return mobileAppSchema;
    }

    public void setMobileAppSchema(String mobileAppSchema) {
        this.mobileAppSchema = mobileAppSchema;
    }

    public Map<String, Boolean> getIdentityProviders() {
        return identityProviders;
    }

    public void setIdentityProviders(Map<String, Boolean> identityProviders) {
        this.identityProviders = identityProviders;
    }

    public Map<String, Boolean> getIdentityProviderApproval() {
        return identityProviderApproval;
    }

    public void setIdentityProviderApproval(Map<String, Boolean> identityProviderApproval) {
        this.identityProviderApproval = identityProviderApproval;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    /*
     * Builders
     */

    public Map<String, String> getParameters() {
        return parameters;
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    public static APIMClient from(ClientAppBasic app) {
        APIMClient client = new APIMClient();
        // base
        client.name = app.getName();
        client.userName = app.getUserName();
        client.displayName = app.getDisplayName();
        // oauth
        client.clientId = app.getClientId();
        client.clientSecret = app.getClientSecret();
        client.grantedTypes = app.getGrantedTypes();
        client.scope = CollectionUtils.isEmpty(app.getScope()) ? ""
                : String.join(SEPARATOR, app.getScope());
        client.redirectUris = CollectionUtils.isEmpty(app.getRedirectUris()) ? ""
                : String.join(SEPARATOR, app.getRedirectUris());
        // deprecated
        client.clientSecretMobile = "";
        client.nativeAppsAccess = false;
        client.mobileAppSchema = app.getMobileAppSchema();

        // apim expects grantTypes in parameters as a string
        Map<String, String> parameters = new HashMap<>();
        parameters.put("grant_types", String.join(SEPARATOR, app.getGrantedTypes()));

        client.parameters = parameters;
        return client;
    }

}

@@ -187,24 +187,76 @@
 tionUtils;%0A%0A
+import com.fasterxml.jackson.annotation.JsonIgnore;%0A
 import com.f
@@ -360,17 +360,55 @@
 pBasic;%0A
-%0A
+import net.minidev.json.JSONObject;%0A%0A//
 //ignore
@@ -471,16 +471,18 @@
 ize it %0A
+//
 @JsonIgn
@@ -1195,16 +1195,32 @@
 scope;%0A%0A
+    @JsonIgnore%0A
     priv
@@ -1234,32 +1234,66 @@
 %3CString, String%3E
+ parametersMap;%0A    private String
  parameters;%0A%0A  
@@ -1339,16 +1339,19 @@
 rameters
+Map
  = Colle
@@ -1365,24 +1365,54 @@
 emptyMap();%0A
+        this.parameters = %22%22;%0A
     %7D%0A%0A    p
@@ -4131,70 +4131,249 @@
  %7D%0A%0A
+//
     
-/*%0A     * Builders%0A     */%0A%0A    public Map%3CString, String%3E
+public Map%3CString, String%3E getParametersMap() %7B%0A//        return parametersMap;%0A//    %7D%0A//%0A//    public void setParametersMap(Map%3CString, String%3E parametersMap) %7B%0A//        this.parametersMap = parametersMap;%0A//    %7D%0A%0A    public String
  get
@@ -4451,35 +4451,22 @@
 ameters(
-Map%3C
 String
-, String%3E
  paramet
@@ -4509,32 +4509,64 @@
 ameters;%0A    %7D%0A%0A
+    /*%0A     * Builders%0A     */%0A%0A
     public stati
@@ -5627,32 +5627,35 @@
 lient.parameters
+Map
  = parameters;%0A 
@@ -5649,24 +5649,150 @@
 parameters;%0A
+        // serialize as JSON for apim to understand values..%0A        client.parameters = JSONObject.toJSONString(parameters);%0A
         retu

fix apim client properties