package org.linagora.linshare.core.facade.webservice.user.impl;

import java.util.Calendar;
import java.util.List;

import org.linagora.linshare.core.domain.entities.AntivirusLogEntry;
import org.linagora.linshare.core.domain.entities.FileLogEntry;
import org.linagora.linshare.core.domain.entities.LogEntry;
import org.linagora.linshare.core.domain.entities.ShareLogEntry;
import org.linagora.linshare.core.domain.entities.User;
import org.linagora.linshare.core.domain.entities.UserLogEntry;
import org.linagora.linshare.core.facade.webservice.user.LogEntryFacade;
import org.linagora.linshare.core.service.AccountService;
import org.linagora.linshare.core.service.LogEntryService;
import org.linagora.linshare.view.tapestry.beans.LogCriteriaBean;
import org.linagora.linshare.webservice.dto.LogCriteriaDto;
import org.linagora.linshare.webservice.dto.LogDto;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class LogEntryFacadeImpl extends GenericFacadeImpl implements
		LogEntryFacade {

	private final LogEntryService logEntryService;

	public LogEntryFacadeImpl(final AccountService accountService,
			final LogEntryService logEntryService) {
		super(accountService);
		this.logEntryService = logEntryService;
	}

	@Override
	public List<LogDto> query(User actor, LogCriteriaDto criteria) {
		Calendar before = Calendar.getInstance();
		Calendar after = Calendar.getInstance();

		before.setTime(criteria.getBeforeDate());
		after.setTime(criteria.getAfterDate());

		LogCriteriaBean crit = new LogCriteriaBean(Lists.newArrayList(actor
				.getMail()), criteria.getActorFirstName(),
				criteria.getActorLastName(), criteria.getActorDomain(),
				criteria.getTargetMails(), criteria.getTargetFirstName(),
				criteria.getTargetLastName(), criteria.getTargetDomain(),
				before, after, criteria.getLogActions(),
				criteria.getFileName(), criteria.getFileExtension());

		return Lists.transform(logEntryService.findByCriteria(actor, crit),
				new Function<LogEntry, LogDto>() {
					public LogDto apply(LogEntry input) {
						if (input instanceof ShareLogEntry)
							return new LogDto((ShareLogEntry) input);
						if (input instanceof FileLogEntry)
							return new LogDto((FileLogEntry) input);
						if (input instanceof UserLogEntry)
							return new LogDto((UserLogEntry) input);
						return new LogDto((AntivirusLogEntry) input);
					}
				});
	}

}
