package com.mooctest.controller;

import com.alibaba.fastjson.JSONObject;
import com.mooctest.data.BugDTO;
import com.mooctest.data.SupplementDTO;
import com.mooctest.model.MasterReport;
import com.mooctest.service.BugReportService;
import com.mooctest.service.GraphService;
import com.mooctest.service.MasterReportService;
import com.mooctest.service.SupplementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
public class GraphController {
    @Autowired
    GraphService graphService;

    @Autowired
    ViewResolver viewResolver;

    @Autowired
    BugReportService bugReportService;

    @Autowired
    SupplementService supplementService;

    @Autowired
    MasterReportService masterReportService;

    @GetMapping(value = "/graph")
    public JSONObject getGraphData(@RequestParam("masterId") String masterId) {

        return graphService.buildAggReportGraph(masterId);
    }
    @GetMapping(value = "/tree")
    public JSONObject getTreeData(@RequestParam("masterId") String masterId) {

        return graphService.buildAggReportTree(masterId);
    }

    @GetMapping(value = "/graphDetail/rawReport")
    public void renderRawReport(@RequestParam("bugId") String bugId,
                                  @RequestParam("masterId") String masterId,
                                  @RequestParam(value = "isMaster", required = false, defaultValue = "false") boolean isMaster,
                                  Model model,
                                  HttpServletRequest req,
                                  HttpServletResponse resp) throws Exception {

        long[] ids = masterReportService.getExamIdAndCaseIdByMasterId(masterId);
        long examId = ids[0];
        long caseId = ids[1];
        BugDTO bug = bugReportService.getBugById(bugId, examId, caseId);
        model.addAttribute("bugReport", bug);
        model.addAttribute("isMaster", isMaster);

        View rawReportView = viewResolver.resolveViewName("thumbnail/raw_report", Locale.CHINESE);

        rawReportView.render(model.asMap(), req, resp);

    }

    @GetMapping(value = "/graphDetail/supReport")
    public void renderSupReport(@RequestParam("masterId") String masterId,
                                @RequestParam("supId") String supId,
                                HttpServletRequest req,
                                HttpServletResponse resp,
                                Model model) throws Exception {

        long[] ids = masterReportService.getExamIdAndCaseIdByMasterId(masterId);
        long examId = ids[0];
        long caseId = ids[1];
        Map<String, BugDTO> bugMap = bugReportService.getAllBugsMap(examId, caseId);
        SupplementDTO supplementDTO = supplementService.findBySupId(supId, bugMap);
        model.addAttribute("supId", supplementDTO.getSupplementId());
        model.addAttribute("supplement", supplementDTO);

        View rawReportView = viewResolver.resolveViewName("thumbnail/sup_report", Locale.CHINESE);

        rawReportView.render(model.asMap(), req, resp);
    }

    @GetMapping(value = "/graphDetail/aggReport")
    public void renderAggReport(@RequestParam("masterId") String masterId,
                                HttpServletRequest req,
                                HttpServletResponse resp,
                                Model model) throws Exception {

        List<SupplementDTO> supplements = supplementService.getSupplementTopInfoByMasterId(masterId);
        long[] ids = masterReportService.getExamIdAndCaseIdByMasterId(masterId);
        long examId = ids[0];
        long caseId = ids[1];

        BugDTO masterReport = bugReportService.getBugById(masterId, examId, caseId);
        model.addAttribute("aggReportId", "ML-AG-" + masterId.substring(10));
        model.addAttribute("supplements", supplements);
        model.addAttribute("masterReport", masterReport);

        View rawReportView = viewResolver.resolveViewName("thumbnail/agg_report", Locale.CHINESE);
        rawReportView.render(model.asMap(), req, resp);
    }
}
