package com.oguerreiro.resilient.config;

import java.time.Duration;
import org.ehcache.config.builders.*;
import org.ehcache.jsr107.Eh107Configuration;
import org.hibernate.cache.jcache.ConfigSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.*;
import tech.jhipster.config.JHipsterProperties;
import tech.jhipster.config.cache.PrefixedKeyGenerator;

@Configuration
@EnableCaching
public class CacheConfiguration {

    private GitProperties gitProperties;
    private BuildProperties buildProperties;
    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

    public CacheConfiguration(JHipsterProperties jHipsterProperties) {
        JHipsterProperties.Cache.Ehcache ehcache = jHipsterProperties.getCache().getEhcache();

        jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
            CacheConfigurationBuilder.newCacheConfigurationBuilder(
                Object.class,
                Object.class,
                ResourcePoolsBuilder.heap(ehcache.getMaxEntries())
            )
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
                .build()
        );
    }

    @Bean
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(javax.cache.CacheManager cacheManager) {
        return hibernateProperties -> hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cacheManager);
    }

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            createCache(cm, com.oguerreiro.resilient.repository.UserRepository.USERS_BY_LOGIN_CACHE);
            createCache(cm, com.oguerreiro.resilient.repository.UserRepository.USERS_BY_EMAIL_CACHE);
            createCache(cm, com.oguerreiro.resilient.domain.User.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Authority.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.User.class.getName() + ".authorities");
            createCache(cm, com.oguerreiro.resilient.domain.PersistentToken.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.User.class.getName() + ".persistentTokens");
            createCache(cm, com.oguerreiro.resilient.domain.OrganizationType.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.OrganizationType.class.getName() + ".organizations");
            createCache(cm, com.oguerreiro.resilient.domain.OrganizationType.class.getName() + ".metadataProperties");
            createCache(cm, com.oguerreiro.resilient.domain.Organization.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Organization.class.getName() + ".children");
            createCache(cm, com.oguerreiro.resilient.domain.Organization.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.Organization.class.getName() + ".inputDataUploads");
            createCache(cm, com.oguerreiro.resilient.domain.MetadataProperty.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.MetadataProperty.class.getName() + ".organizationTypes");
            createCache(cm, com.oguerreiro.resilient.domain.MetadataValue.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.UnitType.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.UnitType.class.getName() + ".units");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".fromUnits");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".toUnits");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".baseUnits");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".sourceUnits");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".outputData");
            createCache(cm, com.oguerreiro.resilient.domain.Unit.class.getName() + ".variableUnits");
            createCache(cm, com.oguerreiro.resilient.domain.UnitConverter.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.VariableScope.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.VariableScope.class.getName() + ".variableCategories");
            createCache(cm, com.oguerreiro.resilient.domain.VariableScope.class.getName() + ".variables");
            createCache(cm, com.oguerreiro.resilient.domain.VariableCategory.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.VariableCategory.class.getName() + ".variables");
            createCache(cm, com.oguerreiro.resilient.domain.Variable.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Variable.class.getName() + ".variableClasses");
            createCache(cm, com.oguerreiro.resilient.domain.Variable.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.Variable.class.getName() + ".outputData");
            createCache(cm, com.oguerreiro.resilient.domain.Variable.class.getName() + ".variableUnits");
            createCache(cm, com.oguerreiro.resilient.domain.VariableUnits.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.VariableClass.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Period.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.Period.class.getName() + ".periodVersions");
            createCache(cm, com.oguerreiro.resilient.domain.Period.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.Period.class.getName() + ".inputDataUploads");
            createCache(cm, com.oguerreiro.resilient.domain.Period.class.getName() + ".outputData");
            createCache(cm, com.oguerreiro.resilient.domain.PeriodVersion.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.PeriodVersion.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.PeriodVersion.class.getName() + ".inputDataUploads");
            createCache(cm, com.oguerreiro.resilient.domain.PeriodVersion.class.getName() + ".outputData");
            createCache(cm, com.oguerreiro.resilient.domain.InputData.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.InputData.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.InputDataUpload.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.InputDataUpload.class.getName() + ".inputData");
            createCache(cm, com.oguerreiro.resilient.domain.InputDataUpload.class.getName() + ".inputDataUploadLogs");
            createCache(cm, com.oguerreiro.resilient.domain.InputDataUploadLog.class.getName());
            createCache(cm, com.oguerreiro.resilient.domain.OutputData.class.getName());
            // jhipster-needle-ehcache-add-entry
        };
    }

    private void createCache(javax.cache.CacheManager cm, String cacheName) {
        javax.cache.Cache<Object, Object> cache = cm.getCache(cacheName);
        if (cache != null) {
            cache.clear();
        } else {
            cm.createCache(cacheName, jcacheConfiguration);
        }
    }

    @Autowired(required = false)
    public void setGitProperties(GitProperties gitProperties) {
        this.gitProperties = gitProperties;
    }

    @Autowired(required = false)
    public void setBuildProperties(BuildProperties buildProperties) {
        this.buildProperties = buildProperties;
    }

    @Bean
    public KeyGenerator keyGenerator() {
        return new PrefixedKeyGenerator(this.gitProperties, this.buildProperties);
    }
}
