package net.educoder.ecsonar.services;

import net.educoder.ecsonar.dao.ProjectDao;
import net.educoder.ecsonar.model.Metrics;
import net.educoder.ecsonar.model.Project;
import net.educoder.ecsonar.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class ReportService {

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ProjectDao projectDao;


    /**
     * 获取分析指标数据
     *
     * @param name
     * @return
     */
    public Metrics getMetrics(String name) {
        Project project = projectDao.findByName(name);
        if (project==null){
            return getDefault();
        }
        String uuid = project.getProject_uuid();

        Metrics metrics = projectDao.selectMetricsByProjectUuid(uuid);

        return metrics;
    }

    public Metrics getDefault(){
        Metrics metrics = new Metrics();
        metrics.setComplexity(0f);
        metrics.setLines(0);
        metrics.setBlock_bugs(0);
        metrics.setBlock_code_smells(0);
        metrics.setBlock_violations(0);
        metrics.setCritical_bugs(0);
        metrics.setCritical_code_smells(0);
        metrics.setCritical_violations(0);
        metrics.setMajor_bugs(0);
        metrics.setMajor_code_smells(0);
        metrics.setMajor_violations(0);
        metrics.setMinor_bugs(0);
        metrics.setMinor_code_smells(0);
        metrics.setMinor_violations(0);
        metrics.setBugs("-");
        metrics.setViolations("-");
        metrics.setCode_smells("-");

        return metrics;
    }


    /**
     * 将结果写进excel
     *
     * @param metrics
     */
    public void writeToExcel(String stuId, String name,
                             Metrics metrics, String templatePath, String outPath, int index) throws Exception {
        Workbook workbook = ExcelUtil.getWorkbok(new File(templatePath));
        ExcelUtil.writeMetrics(metrics, stuId, name,workbook , index);
        ExcelUtil.saveExcel(workbook,outPath);
    }

    /**
     * 写入指定模板
     * @param stuId
     * @param name
     * @param metrics
     * @param templatePath
     * @param outPath
     * @throws Exception
     */
    public void writeSpecifyTemplateToExcel(String stuId, String name,
                             Metrics metrics, String templatePath, String outPath) throws Exception {
        Workbook workbook = ExcelUtil.getWorkbok(new File(templatePath));
        ExcelUtil.writeToTemplateOne(metrics, stuId, name,workbook);
        ExcelUtil.saveExcel(workbook,outPath);
    }

    /**
     * 获取复杂度
     *
     * @return
     */
    private String getComplexity(String uuid) {
        Float num = projectDao.getComplexity(uuid);
        if (num == null) {
            return "0";
        }
        return "" + projectDao.getComplexity(uuid);
    }


    /**
     * 获取总行数
     *
     * @return
     */
    private int getLines(String uuid) {
        Integer num = projectDao.getLines(uuid);
        if (num == null) {
            return 0;
        }
        return projectDao.getLines(uuid);
    }

    /**
     * 漏洞-阻断
     *
     * @return
     */
    private int getBlockViolations(String uuid) {
        return projectDao.getIndicator(uuid, 3, "BLOCKER");
    }

    /**
     * 漏洞-严重
     *
     * @return
     */
    private int getCriticalViolations(String uuid) {
        return projectDao.getIndicator(uuid, 3, "CRITICAL");
    }

    /**
     * 漏洞-主要
     *
     * @return
     */
    private int getMajorViolations(String uuid) {
        return projectDao.getIndicator(uuid, 3, "MAJOR");
    }

    /**
     * 漏洞-次要
     *
     * @return
     */
    private int getMinorViolations(String uuid) {
        return projectDao.getIndicator(uuid, 3, "MINOR");

    }

    /**
     * 漏洞-等级
     *
     * @return
     */
    private String getViolations(String uuid) {
        //93
        return projectDao.getLevel(uuid, 93);
    }


    /**
     * 缺陷-阻断
     *
     * @return
     */
    private int getBlockBugs(String uuid) {
        return projectDao.getIndicator(uuid, 2, "BLOCKER");
    }

    /**
     * 缺陷-严重
     *
     * @return
     */
    private int getCriticalBugs(String uuid) {
        return projectDao.getIndicator(uuid, 2, "CRITICAL");
    }

    /**
     * 缺陷-主要
     *
     * @return
     */
    private int getMajorBugs(String uuid) {
        return projectDao.getIndicator(uuid, 2, "MAJOR");
    }

    /**
     * 缺陷-次要
     *
     * @return
     */
    private int getMinorBugs(String uuid) {
        return projectDao.getIndicator(uuid, 2, "MINOR");
    }

    /**
     * 缺陷-等级
     *
     * @return
     */
    private String getBugs(String uuid) {
        //89
        return projectDao.getLevel(uuid, 89);
    }


    /**
     * 缺陷-阻断
     *
     * @return
     */
    private int getBlockCodeSmells(String uuid) {
        return projectDao.getIndicator(uuid, 1, "BLOCKER");
    }

    /**
     * 缺陷-严重
     *
     * @return
     */
    private int getCriticalCodeSmells(String uuid) {
        return projectDao.getIndicator(uuid, 1, "CRITICAL");
    }

    /**
     * 缺陷-主要
     *
     * @return
     */
    private int getMajorCodeSmells(String uuid) {
        return projectDao.getIndicator(uuid, 1, "MAJOR");
    }

    /**
     * 缺陷-次要
     *
     * @return
     */
    private int getMinorCodeSmells(String uuid) {
        return projectDao.getIndicator(uuid, 1, "MINOR");
    }

    /**
     * 缺陷-等级
     *
     * @return
     */
    private String getCodeSmells(String uuid) {
        //80
        return projectDao.getLevel(uuid, 80);
    }



}
