package net.educoder.ecsonar;

import net.educoder.ecsonar.dao.ProjectDao;
import net.educoder.ecsonar.model.Metrics;
import net.educoder.ecsonar.model.Project;
import net.educoder.ecsonar.model.api.Person;
import net.educoder.ecsonar.services.ReportService;
import net.educoder.ecsonar.services.SonarService;
import net.educoder.ecsonar.utils.ExcelUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EcsonarApplicationTests {

    @Autowired
    ProjectDao projectDao;

    @Autowired
    SonarService sonarService;

    @Autowired
    ReportService reportService;

    @Test
    public void contextLoads() {
        Project demo_maven_webapp = projectDao.findByName("demo Maven Webapp");
        System.out.println(demo_maven_webapp.getProject_uuid());
    }

    @Test
    public void testScan() throws Exception {
        sonarService.scan(
                "",
                "http://office.cn-gd.ufileos.com/2015962121-%E6%AF%9B%E8%83%A4%E7%81%BF-%E6%9C%AC%E7%A7%91%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1%E4%BB%A3%E7%A0%81.zip",
                1111,
                "1234",
                1);
    }

    @Test
    public void testReport() throws Exception {

        String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
        String outPath = "/Users/youyongsheng/Desktop/222.xlsx";
        Metrics hello = reportService.getMetrics("202017-201705820610-1");
        reportService.writeToExcel("201705820610",
                "李金全",
                hello,
                templatePath,
                outPath,
                1);
    }

    @Test
    public void scanExcel(){
        CountDownLatch countDownLatch = new CountDownLatch(1);

        String excelPath = "/Users/youyongsheng/Desktop/res.xls";
//        String excelPath = "/Users/youyongsheng/Desktop/res-test.xlsx";
        sonarService.scanExcel(excelPath);

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void writeExcel() throws Exception {
        int homeworkId = 20210101;
        String directory = "/tmp/20210101/";
        File file = new File(directory);
        File[] files = file.listFiles();
        System.out.println("学生数：" + files.length);

        List<Person> personList = ExcelUtil.readExcel("/Users/youyongsheng/Desktop/res.xls");

        Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(Person::getUid));


        int i=1;
        for (File file1 : files) {
            String uid = file1.getName();
            String projectName = String.format("%d-%s", homeworkId, uid);

            Metrics metrics = reportService.getMetrics(projectName);

            String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
            String outPath = "/Users/youyongsheng/Desktop/202100000004.xlsx";

            System.out.println("第"+(i++)+"个学生," + uid);
            if (CollectionUtils.isEmpty(collect.get(uid))) {

                System.out.println(uid+"为空");
                continue;
            }


            reportService.writeSpecifyTemplateToExcel(uid,
                    collect.get(uid).get(0).getName(),
                    metrics,
                    i == 2? templatePath : outPath,
                    outPath);


        }

        System.out.println("---------end------------");

    }
}
