package top.quantic.sentry.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import top.quantic.sentry.config.Constants;
import top.quantic.sentry.config.SentryProperties;
import top.quantic.sentry.domain.Setting;
import top.quantic.sentry.repository.SettingRepository;
import top.quantic.sentry.service.dto.SettingDTO;
import top.quantic.sentry.service.mapper.SettingMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Setting.
 */
@Service
public class SettingService {

    private final Logger log = LoggerFactory.getLogger(SettingService.class);

    private final SettingRepository settingRepository;
    private final SettingMapper settingMapper;
    private final SentryProperties sentryProperties;

    @Autowired
    public SettingService(SettingRepository settingRepository, SettingMapper settingMapper,
                          SentryProperties sentryProperties) {
        this.settingRepository = settingRepository;
        this.settingMapper = settingMapper;
        this.sentryProperties = sentryProperties;
    }

    @Cacheable("prefixes")
    public List<String> getPrefixes(String guild) {
        List<Setting> settings = settingRepository.findByGuildAndKey(guild, Constants.KEY_PREFIX);
        if (settings.isEmpty()) {
            settings = settingRepository.findByGuildAndKey(Constants.ANY, Constants.KEY_PREFIX);
            if (settings.isEmpty()) {
                return sentryProperties.getDiscord().getDefaultPrefixes();
            }
        }
        return extractValues(settings);
    }

    /**
     * Save a setting.
     *
     * @param settingDTO the entity to save
     * @return the persisted entity
     */
    @CacheEvict(cacheNames = "prefixes", allEntries = true)
    public SettingDTO save(SettingDTO settingDTO) {
        log.debug("Request to save Setting : {}", settingDTO);
        Setting setting = settingMapper.settingDTOToSetting(settingDTO);
        setting = settingRepository.save(setting);
        SettingDTO result = settingMapper.settingToSettingDTO(setting);
        return result;
    }

    /**
     * Get all the settings.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    public Page<SettingDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Settings");
        Page<Setting> result = settingRepository.findAll(pageable);
        return result.map(setting -> settingMapper.settingToSettingDTO(setting));
    }

    /**
     * Get one setting by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    public SettingDTO findOne(String id) {
        log.debug("Request to get Setting : {}", id);
        Setting setting = settingRepository.findOne(id);
        SettingDTO settingDTO = settingMapper.settingToSettingDTO(setting);
        return settingDTO;
    }

    /**
     * Delete the  setting by id.
     *
     * @param id the id of the entity
     */
    @CacheEvict(cacheNames = "prefixes", allEntries = true)
    public void delete(String id) {
        log.debug("Request to delete Setting : {}", id);
        settingRepository.delete(id);
    }

    private List<String> extractValues(List<Setting> settingList) {
        return settingList.stream().map(Setting::getValue).collect(Collectors.toList());
    }
}

@@ -30,16 +30,55 @@
 rvice;%0A%0A
+import com.google.common.collect.Sets;%0A
 import o
@@ -441,16 +441,60 @@
 ervice;%0A
+import sx.blah.discord.handle.obj.IMessage;%0A
 import t
@@ -808,16 +808,38 @@
 l.List;%0A
+import java.util.Set;%0A
 import j
@@ -1517,24 +1517,258 @@
 ies;%0A    %7D%0A%0A
+    public Set%3CString%3E getPrefixes(IMessage message) %7B%0A        if (message.getChannel().isPrivate()) %7B%0A            return getPrefixes(%22*%22);%0A        %7D else %7B%0A            return getPrefixes(message.getGuild().getID());%0A        %7D%0A    %7D%0A%0A
     @Cacheab
@@ -1793,19 +1793,18 @@
  public 
-Lis
+Se
 t%3CString
@@ -2124,16 +2124,32 @@
  return 
+Sets.newHashSet(
 sentryPr
@@ -2190,16 +2190,17 @@
 efixes()
+)
 ;%0A      
@@ -3879,19 +3879,18 @@
 private 
-Lis
+Se
 t%3CString
@@ -4014,19 +4014,18 @@
 ctors.to
-Lis
+Se
 t());%0A  

Use Set to handle prefixes