/*
 * Licensed to the Technische Universität Darmstadt under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The Technische Universität Darmstadt 
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.
 *  
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.inception.ui.core.docanno.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.tudarmstadt.ukp.clarin.webanno.api.DocumentService;
import de.tudarmstadt.ukp.inception.schema.AnnotationSchemaService;
import de.tudarmstadt.ukp.inception.schema.feature.FeatureSupportRegistry;
import de.tudarmstadt.ukp.inception.schema.layer.LayerSupportRegistry;
import de.tudarmstadt.ukp.inception.schema.layer.LayerType;
import de.tudarmstadt.ukp.inception.ui.core.docanno.event.DocumentMetadataAnnotationActionUndoSupport;
import de.tudarmstadt.ukp.inception.ui.core.docanno.layer.DocumentMetadataLayerSingletonCreatingWatcher;
import de.tudarmstadt.ukp.inception.ui.core.docanno.layer.DocumentMetadataLayerSupport;
import de.tudarmstadt.ukp.inception.ui.core.docanno.sidebar.DocumentMetadataSidebarFactory;

/**
 * Provides support for document-level annotations.
 */
@Configuration
@EnableConfigurationProperties(DocumentMetadataLayerSupportPropertiesImpl.class)
public class DocumentMetadataLayerSupportAutoConfiguration
{
    @Bean
    @ConditionalOnProperty(prefix = "documentmetadata", name = "enabled", havingValue = "true", matchIfMissing = true)
    public DocumentMetadataSidebarFactory documentMetadataSidebarFactory(
            AnnotationSchemaService aSchemaService)
    {
        return new DocumentMetadataSidebarFactory(aSchemaService);
    }

    /**
     * This bean remains enabled so we don't break existing projects when disabling metadata
     * support. Instead we return {@code true} from {@link LayerType#isInternal()} to prevent the
     * use from creating new layers of this type.
     */
    @SuppressWarnings("javadoc")
    @Bean
    public DocumentMetadataLayerSupport documentMetadataLayerSupport(
            FeatureSupportRegistry aFeatureSupportRegistry,
            ApplicationEventPublisher aEventPublisher,
            DocumentMetadataLayerSupportProperties aProperties)
    {
        return new DocumentMetadataLayerSupport(aFeatureSupportRegistry, aEventPublisher,
                aProperties);
    }

    @Bean
    public DocumentMetadataLayerSingletonCreatingWatcher documentMetadataLayerSingletonCreatingWatcher(
            DocumentService aDocumentService, AnnotationSchemaService aAnnotationService,
            LayerSupportRegistry aLayerRegistry)
    {
        return new DocumentMetadataLayerSingletonCreatingWatcher(aDocumentService,
                aAnnotationService, aLayerRegistry);
    }

    @Bean
    @ConditionalOnProperty(prefix = "documentmetadata", name = "enabled", havingValue = "true", matchIfMissing = true)
    public DocumentMetadataAnnotationActionUndoSupport documentMetadataAnnotationActionUndoSupport()
    {
        return new DocumentMetadataAnnotationActionUndoSupport();
    }
}
