package com.mooctest.controller;

import com.mooctest.cluster.MyClusterAnalyzer;
import com.mooctest.data.BugDTO;
import com.mooctest.data.DiffText;
import com.mooctest.data.ReportDTO;
import com.mooctest.service.BugReportService;
import com.mooctest.service.DiffTextService;
import com.mooctest.service.MasterReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

@RestController
public class TestController {
    @Autowired
    BugReportService bugReportService;

    @Autowired
    MasterReportService masterReportService;

    @Autowired
    DiffTextService diffTextService;

    @GetMapping("/test")
    public String test() {

        List<BugDTO> bugs = bugReportService.getAllBugs(2613, 1489);

        MyClusterAnalyzer<String> analyzer = new MyClusterAnalyzer<String>();
        bugs.forEach(bugDTO -> analyzer.addDocument(bugDTO.getId(), bugDTO.getDescription()));

        Map<String, BugDTO> bugMap = bugs.stream().collect(toMap(BugDTO::getId, Function.identity()));
        bugs = null;

        List<Set<String>> clusters = analyzer.repeatedBisection(1.0);

//        Map<String, Set<String>> masterClusterMap = new HashMap<>();
//        for (Set<String> cluster : clusters) {
//            String masterReport = masterReportService.findMasterReport(cluster, analyzer.getDocuments());
//            masterClusterMap.put(masterReport, cluster);
//        }
//        clusters = null;
//
//        //showClusterResult(masterClusterMap, bugMap);
//
//
//        Map<String, List<DiffText>> masterDiffTextMap = new HashMap<>();
//        masterClusterMap.forEach((masterId, cluster) -> {
//
//            List<DiffText> diffTexts = diffTextService.genDiffText(masterId, cluster, bugMap);
//            masterDiffTextMap.put(masterId, diffTexts);
//        });
//        showDiffResult(masterDiffTextMap, bugMap);
//
//        MyClusterAnalyzer<String> diffTextAnalyzer = new MyClusterAnalyzer<String>();
//
//
//        masterDiffTextMap.forEach((masterId, diffTexts) -> {
//            Map<String, DiffText> diffTextMap = diffTexts.stream()
//                    .collect(toMap(diffText -> diffText.getBugId() + "_" + diffText.getIndex(), Function.identity()));
//
//            diffTexts.forEach(diffText ->
//                    diffTextAnalyzer.addDocument(diffText.getBugId() + "_" + diffText.getIndex(), diffText.getSentence()));
//            List<Set<String>> diffTextClusters = diffTextAnalyzer.repeatedBisection(1.0);
//            List<Set<DiffText>> diffTextSets = new LinkedList<>();
//            diffTextClusters.forEach(cluster -> {
//                Set<DiffText> diffTextSet = cluster.stream()
//                        .map(sentenceId -> diffTextMap.get(sentenceId))
//                        .collect(Collectors.toSet());
//                diffTextSets.add(diffTextSet);
//            });
//            diffTextAnalyzer.clearDocs();
//        });
        return "ok";
    }



    public void showClusterResult(Map<String, Set<String>> masterClusterMap, Map<String, BugDTO> bugMap) {
        masterClusterMap.entrySet().forEach(entry -> {
            System.out.println("[");
            System.out.println("master:");
            System.out.println(bugMap.get(entry.getKey()).getDescription());
            System.out.println();

            entry.getValue().forEach(bugId -> {
                System.out.println(bugMap.get(bugId).getDescription());
            });
            System.out.println("]");
        });

    }


    public void showDiffResult(Map<String, List<DiffText>> masterDiffTextMap, Map<String, BugDTO> bugMap) {
        masterDiffTextMap.entrySet().forEach(entry -> {
            System.out.println("[");
            System.out.println("master:");
            System.out.println(bugMap.get(entry.getKey()).getDescription());
            System.out.println();

            entry.getValue().forEach(diffText -> {
                System.out.println(diffText.getSentence());
            });
            System.out.println("]");
        });

    }

}
