package cn.edu.whu.boot.collection.service.impl;

import cn.edu.whu.boot.collection.domain.ExportUserRecord;
import cn.edu.whu.boot.collection.service.ExportService;
import cn.edu.whu.boot.collection.service.ExportWriterService;
import cn.edu.whu.boot.collection.service.IMultimediaService;
import cn.edu.whu.boot.user.entities.User;
import cn.edu.whu.boot.xml.service.ITaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ExportServiceImpl implements ExportService {
    private static final Logger logger = LoggerFactory.getLogger(ExportServiceImpl.class);

    private final IMultimediaService multimediaService;

    private final ITaskService taskService;

    private final ExportWriterService writerService;

    public ExportServiceImpl(IMultimediaService multimediaService, ITaskService taskService, ExportWriterService writerService) {
        this.multimediaService = multimediaService;
        this.taskService = taskService;
        this.writerService = writerService;
    }

    @Override
    public Optional<File> exportMediaTable(int taskId, String tableName) {
        return writerService.writeToFile(multimediaService.selectByTaskIdAndTableName(taskId, tableName));
    }

    @Override
    public Optional<File> exportUsers(int taskId) {
        List<User> workers = taskService.getWorkersByTask(taskId);
        return writerService.writeToFile(workers.stream().map(w -> new ExportUserRecord(w.getId(), w.getRealName())).collect(Collectors.toList()));
    }
}
