package com.libreecm.input.auxiliars;

import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import com.libreecm.commonAPI.model.enums.BaseTypeId;
import com.libreecm.commonAPI.model.enums.IncludeRelationships;
import com.libreecm.commonAPI.model.objects.Relationship;
import com.libreecm.commonAPI.model.objects.RenditionData;
import com.libreecm.commonAPI.model.objects.Uri;
import com.libreecm.commonAPI.model.users.Ace;
import com.libreecm.commonAPI.model.users.Acl;
import com.libreecm.commonAPI.model.users.Principal;

public class Mapper { 
	  
	public static GregorianCalendar map(FileTime element) {
		return GregorianCalendar.from(
			ZonedDateTime.ofInstant(element.toInstant(), (new GregorianCalendar()).toZonedDateTime().getZone()));
	} 
 
	/**
	 * Returns a new Uri form a file like ON UNIX:
	 * cmis:folder://repository_first/root_folder/.../parent_folder/object OR IN
	 * WINDOWS:
	 * cmis:folder://repository_first/c:/root_folder/.../parent_folder/object
	 * 
	 * @param file
	 * @param repositoryName
	 * @return
	 */
	public static Uri map(File file, String repositoryName) {
		Uri uri = new Uri();
		if (file.isDirectory()) {
			uri.setType(BaseTypeId.CMIS_FOLDER.value());
		} else
			uri.setType(BaseTypeId.CMIS_DOCUMENT.value());
		for (String folder : file.getParent().split("/")) {
			uri.getFolderTreeList().add(folder);
		}
		uri.setObject(file.getName());
		uri.setRepositoryName(repositoryName);
		return uri;
	}

	public static Acl map(AclFileAttributeView view) throws IOException {
		Acl acl = new Acl();
		if (view != null) {
			for (AclEntry entry : view.getAcl()) {
				Ace ace = new Ace();

				Principal principal = new Principal();
				principal.setId(entry.principal().getName());
				ace.setPrincipal(principal);

				ArrayList<String> permissions = new ArrayList<>();
				for (AclEntryPermission permission : entry.permissions()) {
					permissions.add(permission.name());
				}
				ace.setPermissions(permissions);

				acl.getAces().add(ace);
			}
		}

		return acl;
	}

	public static List<Relationship> filterRelationships(List<Relationship> list, IncludeRelationships type, String id) {
		List<Relationship> result = new ArrayList<>();
		if (type == IncludeRelationships.SOURCE) {
			for (Relationship relationship :list){
				if (relationship.getSourceId().equals(id)){
					result.add(relationship);
				}
			}
		}
		else if (type == IncludeRelationships.TARGET) {
			for (Relationship relationship :list){
				if (relationship.getTargetId().equals(id)){
					result.add(relationship);
				}
			}
		} // IncludeRelationships.BOTH is default
		else result=list;
		
		return result;
	}

	public static List<RenditionData> filterRenditions(List<RenditionData> renditions, String renditionFilter,
			String id) {
		List<RenditionData> result = new ArrayList<>();
		if (renditions==null) return result;
		for (RenditionData rendition: renditions){
			if (rendition.getKind().equals(renditionFilter)){
				result.add(rendition);
			}
		}
		return result;		
	}
}
