package com.mooctest.controller;

import com.mooctest.data.BugDTO;
import com.mooctest.data.FinalReportDTO;
import com.mooctest.data.TaskDTO;
import com.mooctest.service.BugReportService;
import com.mooctest.service.FinalReportService;
import com.mooctest.service.TaskService;
import com.mooctest.util.ReportUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class FinalReportController {
    @Autowired
    FinalReportService finalReportService;

    @Autowired
    TaskService taskService;

    @Autowired
    BugReportService bugReportService;

    @PostMapping("final_report")
    @ResponseBody
    public FinalReportDTO createReport(@RequestBody FinalReportDTO dto) {

        return finalReportService.save(dto);
    }

    @GetMapping("final_reports")
    public String getByExamIdAndCaseId(@RequestParam("examId") long examId,
                                       @RequestParam("caseId") long caseId,
                                       Model model) {

        List<FinalReportDTO> finalReports = finalReportService.getByExamIdAndCaseId(examId, caseId);
        TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId);
        model.addAttribute("finalReports", finalReports);
        model.addAttribute("severity2String", ReportUtil.severity2String);
        model.addAttribute("category2String", ReportUtil.category2String);
        model.addAttribute("recurrent2String", ReportUtil.recurrent2String);
        model.addAttribute("examId", examId);
        model.addAttribute("caseId", caseId);
        model.addAttribute("task", task);
        return "final_report_list";
    }

    @DeleteMapping("final_report/{reportId}")
    @ResponseBody
    public long deleteReport(@PathVariable long reportId) {
        finalReportService.delete(reportId);
        return reportId;
    }

    @PutMapping("final_report/{reportId}")
    @ResponseBody
    public FinalReportDTO updateReport(@PathVariable long reportId,
                             @RequestBody FinalReportDTO dto) {
        return finalReportService.update(reportId, dto);
    }

    @GetMapping("final_report/export")
    @ResponseBody
    public String export(@RequestParam("examId") long examId,
                         @RequestParam("caseId") long caseId) {

        TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId);

        return finalReportService.getExportReportAddr(task);
    }

    @RequestMapping(value = "/appendBugToFinalReport")
    @ResponseBody
    public void processRequest(
            @RequestParam("examId") long examId,
            @RequestParam("caseId") long caseId) {
        List<BugDTO> allBugs = bugReportService.getAllBugs(examId, caseId);

        finalReportService.transAllBug2FinalReport(allBugs, examId, caseId);
    }
}
