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

import com.apobates.forum.attention.ImageDirectoryEnum;
import com.apobates.forum.attention.core.UploadImagePathConvert;
import com.apobates.forum.core.ImageIOMeta;
import com.apobates.forum.core.dao.BoardCarouselIndexDao;
import com.apobates.forum.core.dao.TopicCarouselSlideDao;
import com.apobates.forum.core.entity.TopicCarouselSlide;
import com.apobates.forum.core.service.TopicCarouselSlideService;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200614
 */
@Service
public class TopicCarouselSlideServiceImpl implements TopicCarouselSlideService {
    @Autowired
    private TopicCarouselSlideDao topicCarouselSlideDao;
    @Autowired
    private BoardCarouselIndexDao boardCarouselIndexDao;
    private final Function<ImageIOMeta, UploadImagePathConvert> uipcObj = (imageIOMeta)->new UploadImagePathConvert(imageIOMeta.getImageBucketDomain(), imageIOMeta.getUploadImageDirectName());
    
    @Override
    public Stream<TopicCarouselSlide> getAllUsedByCarousel(int topicCarouselId) {
        return topicCarouselSlideDao.findAllUsed(topicCarouselId);
    }
    
    @Override
    public Set<TopicCarouselSlide> getAllUsedByCarousel(int topicCarouselId, ImageIOMeta imageIOMeta) {
        return getAllUsedByCarousel(topicCarouselId).peek(tcs->decodeImagePath(tcs, uipcObj.apply(imageIOMeta))).collect(Collectors.toSet());
    }
    
    @Override
    public Stream<TopicCarouselSlide> getAll(String topicCarouselTitle, final ImageIOMeta imageIOMeta) {
        return topicCarouselSlideDao.findAllUsed(topicCarouselTitle).peek(tcs->decodeImagePath(tcs, uipcObj.apply(imageIOMeta)));
    }
    
    @Override
    public Stream<TopicCarouselSlide> getAll(int boardGroupId, final ImageIOMeta imageIOMeta) {
        return boardCarouselIndexDao.findAllUsedByBoard(boardGroupId).peek(tcs->decodeImagePath(tcs, uipcObj.apply(imageIOMeta)));
    }
    
    @Override
    public Stream<TopicCarouselSlide> getAll(int boardGroupId, long boardId, ImageIOMeta imageIOMeta) {
        return boardCarouselIndexDao.findAllUsedByBoard(boardGroupId, boardId).peek(tcs->decodeImagePath(tcs, uipcObj.apply(imageIOMeta)));
    }
    
    @Override
    public Stream<TopicCarouselSlide> getAllIgnoreStatus(int topicCarouselId) {
        return topicCarouselSlideDao.findAll(topicCarouselId);
    }
    
    @Override
    public Optional<TopicCarouselSlide> get(int id) {
        if (id > 0) {
            return topicCarouselSlideDao.findOne(id);
        }
        return Optional.empty();
    }
    
    @Override
    public Optional<Boolean> edit(int id, int ranking) {
        return topicCarouselSlideDao.editRanking(id, ranking);
    }
    
    @Override
    public Optional<Boolean> edit(int id, String title, String link, int ranking, boolean status) {
        Optional<TopicCarouselSlide> data = get(id);
        if (!data.isPresent()) {
            return Optional.empty();
        }
        TopicCarouselSlide tcs = data.get();
        tcs.setLink(link);
        tcs.setRanking(ranking);
        tcs.setTitle(title);
        tcs.setStatus(status);
        return topicCarouselSlideDao.edit(tcs);
    }
    
    @Override
    public Optional<Boolean> remove(int id, int topicCarouselId) {
        return topicCarouselSlideDao.editStatus(id, topicCarouselId, false);
    }
    
    @Override
    public int edit(Map<Integer, Integer> rankingMap) {
        return topicCarouselSlideDao.editRanking(rankingMap);
    }
    
    @Override
    public int create(int topicCarouselId, String title, String link, String encodeImageAddr, int ranking, boolean status) {
        //编码图片地址
        TopicCarouselSlide tcs = new TopicCarouselSlide(topicCarouselId, title, link, encodeImageAddr, ranking, status);
        topicCarouselSlideDao.save(tcs);
        if (tcs.getId() < 1) {
            return 0;//("幻灯片创建失败");
        }
        return tcs.getId();
    }
    
    private static void decodeImagePath(TopicCarouselSlide tcs, UploadImagePathConvert uipc){
        Objects.requireNonNull(tcs);
        try{
            String imagePath = uipc.decode(tcs.getImageAddr(), ImageDirectoryEnum.DEFAT, null).map(URL::toString).orElse("/static/img/140x140.png");
            tcs.setImageAddr(imagePath);
        }catch(MalformedURLException e){}
    }
}