package com.mooctest.service;

import com.mooctest.dao.MasterReportDao;
import com.mooctest.data.TaskDTO;
import com.mooctest.model.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class TaskService {
    List<Task> tasks = Arrays.asList(
            new Task(2613, 1489, "途牛旅游", "/static/images/apps/tuniu.png", "9.56.0", 0, "2018-11-12", "2018-12-20"),
            new Task(2613, 1489, "花田小憩", "/static/images/apps/huatianxiaoqi.png", "6.5.0", 1, "2018-10-12", "2018-10-20"),
            new Task(2613, 1489, "小猿搜题", "/static/images/apps/xiaoyuansouti.png", "8.5.0", 1, "2018-10-03", "2018-10-15"),
            new Task(2613, 1489, "途牛旅游", "/static/images/apps/tuniu.png", "9.50.0", 1, "2018-10-02", "2018-10-12"),
            new Task(2613, 1489, "JayMe", "/static/images/apps/JayMe.jpeg", "3.5.8", 1, "2018-10-01", "2018-11-01"),
            new Task(2613, 1489, "和苗智家", "/static/images/apps/hemiaozhijia.png", "1.0.8", 1, "2018-09-22", "2018-09-29"),
            new Task(2613, 1489, "邻里快讯", "/static/images/apps/linlikuaixun.png", "2.1.3", 1, "2018-09-18", "2018-09-29"),
            new Task(2613, 1489, "探记", "/static/images/apps/tanji.png", "4.1.0", 1, "2018-09-13", "2018-09-22"),
            new Task(2973, 1717, "月度赛", "/static/images/apps/tanji.png", "4.1.0", 1, "2018-09-13", "2018-09-22")
    );

    @Autowired
    MasterReportDao masterReportDao;

    @Autowired
    BugReportService bugReportService;

    public List<TaskDTO> getAllTasks() {
        return tasks.stream()
                .map(task -> {
                    TaskDTO taskDTO = new TaskDTO();
                    BeanUtils.copyProperties(task, taskDTO);
                    return taskDTO;
                })
                .map(taskDTO -> {
                    if (taskDTO.getName().equals("途牛旅游") && taskDTO.getVersion().equals("9.56.0")) {
                        long totalBugs = masterReportDao.countByExamIdAndCaseId(taskDTO.getExamId(), taskDTO.getCaseId());
                        long undealBugs = 0;
                        if (totalBugs == 0) {
                            totalBugs = bugReportService.getAllBugs(taskDTO.getExamId(), taskDTO.getCaseId()).size();
                            undealBugs = totalBugs;
                        } else {
                            undealBugs = masterReportDao.countByExamIdAndCaseIdAndStatus(taskDTO.getExamId(), taskDTO.getCaseId(), 0);
                        }
                        taskDTO.setNumOfTotalBug(totalBugs);
                        taskDTO.setNumOfUndeal(undealBugs);
                    } else {
                        Random r = new Random();
                        int n = r.nextInt(100)%(100-20+1) + 20;
                        taskDTO.setNumOfTotalBug(n * 10);
                        taskDTO.setNumOfUndeal(0);
                    }
                    return taskDTO;
                }).sorted(Comparator.comparing(TaskDTO::getStatus))
                .collect(Collectors.toList());

    }

    public TaskDTO getByExamIdAndCaseId(long examId, long caseId) {
        Task t = tasks.stream().filter(task -> task.getExamId() == examId && task.getCaseId() == caseId).findFirst().get();
        TaskDTO taskDTO = new TaskDTO();
        BeanUtils.copyProperties(t, taskDTO);
        long totalBugs = masterReportDao.countByExamIdAndCaseId(taskDTO.getExamId(), taskDTO.getCaseId());
        long undealBugs = 0;
        if (totalBugs == 0) {
            totalBugs = bugReportService.getAllBugs(taskDTO.getExamId(), taskDTO.getCaseId()).size();
            undealBugs = totalBugs;
        } else {
            undealBugs = masterReportDao.countByExamIdAndCaseIdAndStatus(taskDTO.getExamId(), taskDTO.getCaseId(), 0);
        }
        taskDTO.setNumOfTotalBug(totalBugs);
        taskDTO.setNumOfUndeal(undealBugs);
        return taskDTO;
    }
}
