/*
 * #%L
 * %%
 * Copyright (C) 2015 Trustsystems Desenvolvimento de Sistemas, LTDA.
 * %%
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the Trustsystems Desenvolvimento de Sistemas, LTDA. nor the names of its contributors
 *    may be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */
package com.castle.elfinder;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.castle.web.ServletSupport;

import br.com.trustsystems.elfinder.command.CommandFactory;

/**
 * Elfinder Spring Root Configuration.
 *
 * @author Thiago Gutenberg Carvalho da Costa
 */
@Configuration
@ComponentScan(basePackages = { "br.com.trustsystems.elfinder" }, excludeFilters = {
		@Filter(type = FilterType.ANNOTATION, value = Configuration.class), @Filter(value = Controller.class, type = FilterType.ANNOTATION),
		@Filter(value = EnableWebMvc.class, type = FilterType.ANNOTATION), @Filter(value = ServletSupport.class, type = FilterType.ANNOTATION) })
public class ElfinderRootConfig {

	@Bean(name = "commandFactory")
	public CommandFactory getCommandFactory() {
		CommandFactory commandFactory = new CommandFactory();
		commandFactory.setClassNamePattern("br.com.trustsystems.elfinder.command.%sCommand");
		return commandFactory;
	}

//	@Bean(name = "elfinderConfiguration")
//	public ElfinderConfigurationWrapper getElfinderConfigurationWrapper() {
//		return new ElfinderConfigurationWrapper();
//	}
//
//	@Bean(name = "elfinderStorageFactory")
//	public ElfinderStorageFactory getElfinderStorageFactory() {
//		DefaultElfinderStorageFactory elfinderStorageFactory = new DefaultElfinderStorageFactory();
//		elfinderStorageFactory.setElfinderStorage(getElfinderStorage());
//		return elfinderStorageFactory;
//	}
//
//	@Bean(name = "elfinderStorage")
//	public ElfinderStorage getElfinderStorage() {
//		ElfinderConfigurationWrapper elfinderConfiguration = getElfinderConfigurationWrapper();
//		DefaultElfinderStorage defaultElfinderStorage = new DefaultElfinderStorage();
//
//		// creates thumbnail
//		DefaultThumbnailWidth defaultThumbnailWidth = new DefaultThumbnailWidth();
//		defaultThumbnailWidth.setThumbnailWidth(elfinderConfiguration.getThumbnailWidth());
//
//		// creates volumes, volumeIds, volumeLocale and volumeSecurities
//		Character defaultVolumeId = 'A';
//		List<ElfinderConfiguration.Volume> elfinderConfigurationVolumes = elfinderConfiguration.getVolumes();
//		List<Volume> elfinderVolumes = new ArrayList<>(elfinderConfigurationVolumes.size());
//		Map<Volume, String> elfinderVolumeIds = new HashMap<>(elfinderConfigurationVolumes.size());
//		Map<Volume, Locale> elfinderVolumeLocales = new HashMap<>(elfinderConfigurationVolumes.size());
//		List<VolumeSecurity> elfinderVolumeSecurities = new ArrayList<>();
//
//		// creates volumes
//		for (ElfinderConfiguration.Volume elfinderConfigurationVolume : elfinderConfigurationVolumes) {
//
//			final String alias = elfinderConfigurationVolume.getAlias();
//			final String path = elfinderConfigurationVolume.getPath();
//			final String source = elfinderConfigurationVolume.getSource();
//			final String locale = elfinderConfigurationVolume.getLocale();
//			final boolean isLocked = elfinderConfigurationVolume.getConstraint().isLocked();
//			final boolean isReadable = elfinderConfigurationVolume.getConstraint().isReadable();
//			final boolean isWritable = elfinderConfigurationVolume.getConstraint().isWritable();
//
//			// creates new volume
//			Volume volume = VolumeSources.of(source).newInstance(alias, path);
//
//			elfinderVolumes.add(volume);
//			elfinderVolumeIds.put(volume, Character.toString(defaultVolumeId));
//			elfinderVolumeLocales.put(volume, LocaleUtils.toLocale(locale));
//
//			// creates security constraint
//			SecurityConstraint securityConstraint = new SecurityConstraint();
//			securityConstraint.setLocked(isLocked);
//			securityConstraint.setReadable(isReadable);
//			securityConstraint.setWritable(isWritable);
//
//			// creates volume pattern and volume security
//			final String volumePattern = Character.toString(defaultVolumeId) + ElFinderConstants.ELFINDER_VOLUME_SERCURITY_REGEX;
//			elfinderVolumeSecurities.add(new DefaultVolumeSecurity(volumePattern, securityConstraint));
//
//			// prepare next volumeId character
//			defaultVolumeId++;
//		}
//
//		defaultElfinderStorage.setThumbnailWidth(defaultThumbnailWidth);
//		defaultElfinderStorage.setVolumes(elfinderVolumes);
//		defaultElfinderStorage.setVolumeIds(elfinderVolumeIds);
//		defaultElfinderStorage.setVolumeLocales(elfinderVolumeLocales);
//		defaultElfinderStorage.setVolumeSecurities(elfinderVolumeSecurities);
//
//		return defaultElfinderStorage;
//	}

}
