package ru.soune.nocopy.configuration;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.vrt.AudioFilePathProvider;
import com.vrt.fileprotection.FileProtector;
import com.vrt.fileprotection.audio.AudioLocalSearch;
import com.vrt.fileprotection.documents.DocumentLocalSearch;
import com.vrt.fileprotection.image.ImageLocalSearch;
import com.vrt.fileprotection.image.ImageUniqueCheck;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableAutoConfiguration
@AllArgsConstructor
@EnableCaching
public class ApplicationConfig {

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public com.vrt.
            NoCopyFileService noCopyFileService(
            FileProtector.FileProvider fileProvider,
            FileProtector.ProcessingListener processingListener,
            ImageUniqueCheck imageUniqueCheck,
            ImageLocalSearch imageLocalSearch,
            AudioLocalSearch audioLocalSearch,
            AudioFilePathProvider audioFilePathProvider,
            DocumentLocalSearch documentLocalSearch) {

        return new com.vrt.NoCopyFileService(
                Collections.emptyList(),
                fileProvider,
                processingListener,
                imageUniqueCheck,
                imageLocalSearch,
                audioLocalSearch,
                audioFilePathProvider,
                documentLocalSearch
        );
    }

    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager(
                "reverseImageSearch", "parsedImages");

        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(2_000)
                .expireAfterWrite(1, TimeUnit.HOURS)
                .expireAfterAccess(30, TimeUnit.MINUTES)
                .softValues()
                .recordStats()
        );

        return cacheManager;
    }
}

