package com.libreecm.db.jpa.users;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.OneToMany;
import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
import org.apache.chemistry.opencmis.commons.data.PermissionMapping;
import org.apache.chemistry.opencmis.commons.definitions.PermissionDefinition;
import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
import org.apache.chemistry.opencmis.commons.enums.SupportedPermissions;
import org.hibernate.annotations.GenericGenerator;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.libreecm.db.jpa.CmisExtensionElementImpl;
import com.libreecm.db.jpa.LibreECMObject;

@Entity
public class AclCapabilitiesImpl extends LibreECMObject implements org.apache.chemistry.opencmis.commons.data.AclCapabilities {

	/**
	 *  
	 */
	private static final long serialVersionUID = 3883886621264859670L;
	

	@ElementCollection
    @CollectionTable(name="acl_capabilities_permission_mapping", joinColumns=@JoinColumn(name="permission_mapping_id"))
    @Column(name="mapping_subject_permission")
    @MapKeyJoinColumn
	private Map<String, PermissionMappingImpl> permissionMappings;
	
	@OneToMany
    private List<PermissionDefinitionImpl> permissions;
	
	private AclPropagation aclPropagation;
	private SupportedPermissions supportedPermissions;

	@OneToMany
    private List<CmisExtensionElementImpl> cmisExtensionElements;
	
	@Id
	@GeneratedValue(generator="hilo-gen")
	@GenericGenerator(name="hilo-gen", strategy="hilo")
	private Integer id;
		
	public Map<String, PermissionMappingImpl> getPermissionMappings() {
		return permissionMappings;
	}

	public void setPermissionMappings(Map<String, PermissionMappingImpl> permissionMappings) {
		this.permissionMappings = permissionMappings;
	}

	public String getId() {
		if (id==null){
			return null;
		}
		return id.toString();
	}
	
	public void setId(String id) {
		if (id==null){
			this.id=null;
		} else
		this.id=Integer.parseInt(id);
	}

	public AclCapabilitiesImpl(){
		
	}
	
	public AclCapabilitiesImpl(Map<String, PermissionMappingImpl> permissionMappings, List<PermissionDefinitionImpl> permissions,
			AclPropagation aclPropagation, SupportedPermissions supportedPermissions,
			List<CmisExtensionElementImpl> extensions) {
		super();
		this.permissionMappings=permissionMappings;
		this.permissions = permissions;
		this.aclPropagation = aclPropagation;
		this.supportedPermissions = supportedPermissions;
		this.cmisExtensionElements = extensions;
	}

	public void setPermissionMapping(Map<String, PermissionMapping> permissionMappings) {
		this.permissionMappings=new HashMap<String, PermissionMappingImpl>();
		for (Entry<String, PermissionMapping> entry : permissionMappings.entrySet()){
			this.permissionMappings.put(entry.getKey(), (PermissionMappingImpl) entry.getValue());
		}
	}

	public void setPermissions(List<PermissionDefinition> permissions) {
		this.permissions=new ArrayList<PermissionDefinitionImpl>();
		Iterator<PermissionDefinition> it = permissions.iterator();
		while (it.hasNext()){
			this.permissions.add((PermissionDefinitionImpl) it.next());
		}
	}

	public void setAclPropagation(AclPropagation aclPropagation) {
		this.aclPropagation = aclPropagation;
	}

	public void setSupportedPermissions(SupportedPermissions supportedPermissions) {
		this.supportedPermissions = supportedPermissions;
	}

	@Override
	public SupportedPermissions getSupportedPermissions() {
		return supportedPermissions;
	}

	@Override
	public AclPropagation getAclPropagation() {
		return aclPropagation;
	}

	public List<PermissionDefinition> getPermissions() {
		if (this.permissions==null){
			this.permissions=new ArrayList<>();
		}
		ArrayList<PermissionDefinition> array= new ArrayList<PermissionDefinition>();
		array.addAll(this.permissions);
		return array;
	}
	
	public Map<String, PermissionMapping> getPermissionMapping() {
		if (this.permissionMappings==null){
			this.permissionMappings = new HashMap<String, PermissionMappingImpl>();
		}
		Map<String, PermissionMapping> permissionMappings = new HashMap<String, PermissionMapping>();
		for (Entry<String, PermissionMappingImpl> entry : this.permissionMappings.entrySet()){
			permissionMappings.put(entry.getKey(), (PermissionMapping) entry.getValue());
		}
		return permissionMappings;
	}

	
	public List<CmisExtensionElementImpl> getCmisExtensionElements() {
		if (this.cmisExtensionElements==null){
			this.cmisExtensionElements=new ArrayList<>();
		}
		return this.cmisExtensionElements;
	} 

	public void setCmisExtensionElements(List<CmisExtensionElementImpl> arg0) {
		this.cmisExtensionElements=arg0;		
	}

	@Override
	@Deprecated
	@JsonIgnore
	public List<CmisExtensionElement> getExtensions() {
		ArrayList<CmisExtensionElement> array= new ArrayList<CmisExtensionElement>();
		array.addAll(this.cmisExtensionElements);
		return array;
	} 

	@Override
	@Deprecated
	@JsonIgnore
	public void setExtensions(List<CmisExtensionElement> arg0) {
		this.cmisExtensionElements=new ArrayList<CmisExtensionElementImpl>();

		Iterator<CmisExtensionElement> it = arg0.iterator();
		while (it.hasNext()){
			this.cmisExtensionElements.add((CmisExtensionElementImpl) it.next());
		}		
	}

}
