package com.oguerreiro.resilient.security.saml2;

import java.util.HashMap;
import java.util.Map;

public class Saml2AuthenticationAttributes {
  public static final String ATTR_NAME = "name";
  public static final String ATTR_USERNAME = "username";
  public static final String ATTR_EMAIL = "email";
  public static final String ATTR_ORGANIZATION_CODE = "organization-code";
  public static final String ATTR_SECURITY_GROUP_CODE = "security-group-code";
  public static final String ATTR_ROLE = "role";

  private String name;
  private String username;
  private String email;
  private String organizationCode;
  private String securityGroupCode;
  private String role;
  private Map<String, String> additional = new HashMap<String, String>();

  public String getName() {
    return name;
  }

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

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getOrganizationCode() {
    return organizationCode;
  }

  public void setOrganizationCode(String organizationCode) {
    this.organizationCode = organizationCode;
  }

  public String getSecurityGroupCode() {
    return securityGroupCode;
  }

  public void setSecurityGroupCode(String securityGroupCode) {
    this.securityGroupCode = securityGroupCode;
  }

  public String getRole() {
    return role;
  }

  public void setRole(String role) {
    this.role = role;
  }

  public void addAttribute(String key, String value, String defaultValue) {
    // Apply default value, if needed an present
    if ((value == null || value.isBlank()) && (defaultValue != null && !defaultValue.isBlank())) {
      value = defaultValue;
    }

    switch (key) {
    case ATTR_NAME: {
      this.setName(value);
      break;
    }
    case ATTR_USERNAME: {
      this.setUsername(value);
      break;
    }
    case ATTR_EMAIL: {
      this.setEmail(value);
      break;
    }
    case ATTR_ORGANIZATION_CODE: {
      this.setOrganizationCode(value);
      break;
    }
    case ATTR_SECURITY_GROUP_CODE: {
      this.setSecurityGroupCode(value);
      break;
    }
    case ATTR_ROLE: {
      this.setRole(value);
      break;
    }
    }

    // Always add. Its a sugar code when using getAttribute(String key)
    this.additional.put(key, value);
  }

  public String getAttribute(String key) {
    return this.additional.get(key);
  }
}
