package com.apobates.forum.core.impl.service;

import com.apobates.forum.core.dao.TopicCategoryDao;
import com.apobates.forum.core.entity.TopicCategory;
import com.apobates.forum.core.service.TopicCategoryService;
import com.apobates.forum.utils.Commons;
import com.apobates.forum.utils.persistence.Page;
import com.apobates.forum.utils.persistence.Pageable;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200513
 */
@Service
public class TopicCategoryServiceImpl implements TopicCategoryService{
    @Autowired
    private TopicCategoryDao topicCategoryDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicCategoryServiceImpl.class);
    
    @Override
    public Optional<TopicCategory> create(String names, String value, boolean status)throws IllegalStateException {
        if (TopicCategory.isKeywords(value)) {
            throw new IllegalStateException("属性值为保留词.不允许使用");
        }
        TopicCategory tc = new TopicCategory(value, names);
        tc.setStatus(status);
        try {
            topicCategoryDao.save(tc);
            if (tc.getId() > 0) {
                return Optional.of(tc);
            }
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("话题类型创建失败", e);
            }
        }
        return Optional.empty();
    }
    @Override
    public Optional<Boolean> remove(int id)throws IllegalArgumentException {
        TopicCategory data = get(id).orElseThrow(()->new IllegalArgumentException("话题类型不存在"));
        data.setStatus(false);
        return topicCategoryDao.edit(data);
    }
    
    @Override
    public Optional<Boolean> edit(int id, TopicCategory updateCategory)throws IllegalArgumentException {
        TopicCategory data = get(id).orElseThrow(()->new IllegalArgumentException("话题类型不存在"));
        data.setNames(updateCategory.getNames());
        if (!TopicCategory.isKeywords(updateCategory.getValue())) {
            data.setValue(updateCategory.getValue());
        }
        data.setStatus(updateCategory.isStatus());
        return topicCategoryDao.edit(data);
    }
    
    @Override
    public Page<TopicCategory> getAll(Pageable pageable) {
        return topicCategoryDao.findAll(pageable);
    }
    
    @Override
    public Stream<TopicCategory> getAllUsed() {
        return topicCategoryDao.findUsed();
    }
    
    @Override
    public Stream<TopicCategory> getAssociates() {
        Stream<TopicCategory> usedCategories = topicCategoryDao.findUsed();
        return Stream.concat(usedCategories, Arrays.asList(TopicCategory.feedback(), TopicCategory.report()).stream());
    }
    
    @Override
    public Optional<TopicCategory> get(int id) {
        if (id > 0) {
            return topicCategoryDao.findOne(id);
        }
        return Optional.empty();//("参数不合法或不被接受");
    }
    
    @Override
    public Optional<TopicCategory> get(String value) {
        if (Commons.isNotBlank(value) || !TopicCategory.isKeywords(value)) {
            return topicCategoryDao.findOneByValue(value);
        }
        return Optional.empty();//("参数不合法或不被接受");
    }
}