package vn.azteam.tpf.config;

import java.time.Duration;

import org.ehcache.config.builders.*;
import org.ehcache.jsr107.Eh107Configuration;

import io.github.jhipster.config.jcache.BeanClassLoaderAwareJCacheRegionFactory;
import io.github.jhipster.config.JHipsterProperties;

import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.*;

@Configuration
@EnableCaching
public class CacheConfiguration {

    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

    public CacheConfiguration(JHipsterProperties jHipsterProperties) {
        BeanClassLoaderAwareJCacheRegionFactory.setBeanClassLoader(this.getClass().getClassLoader());
        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 JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            cm.createCache(vn.azteam.tpf.repository.UserRepository.USERS_BY_LOGIN_CACHE, jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.repository.UserRepository.USERS_BY_EMAIL_CACHE, jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.User.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.Authority.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.User.class.getName() + ".authorities", jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBDetailUser.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBActivity.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBAddress.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCity.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCommand.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCountry.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCrop.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCustomer.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBDistrict.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEntity.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEnvironmentUpdate.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEnvironmentalParameter.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEquipment.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEquipmentHistory.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEquipmentOfCustomer.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEquipmentType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBFunction.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBFunctionGroup.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuideline.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuidelineDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBLimits.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBMaintenanceDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBMaintenanceSchedule.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBNurseryDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBObjectParameter.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBObjectUpdate.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBPermission.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBRole.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSetting.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBStockUpdate.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSupplies.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesInWarehouse.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesOrder.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesOrderDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesPriceHistory.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSuppliesUsingDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBVendor.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBWard.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBActivityType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBDetailUser.class.getName() + ".tbCrops", jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCrop.class.getName() + ".tbDetailUsers", jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuideline.class.getName() + ".tbSupplies", jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBSupplies.class.getName() + ".tbGuidelines", jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBNotification.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.Casbin.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCropType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEntityType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuidelinePeriod.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBCustomerHasCropType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBLocationOfCustomer.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBDashboardType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuidelineDetailsObjectUpdate.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBBlockDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBControlType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBActivityExtendType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBExternalTable.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBWaterType.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBProductUnit.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBProductBlockDetails.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBProductStockUpdate.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBGuidelineActivity.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBEntityLocationOfCustomer.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBBlockDetailsReport.class.getName(), jcacheConfiguration);
            cm.createCache(vn.azteam.tpf.domain.TBProductBlockDetailsReport.class.getName(), jcacheConfiguration);
            // jhipster-needle-ehcache-add-entry
        };
    }
}
