package com.zjty.autotest.service;

import com.alibaba.fastjson.JSON;
import com.zjty.autotest.mq.TestTask;
import com.zjty.autotest.pojo.report.ElementDetail;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.sjq.*;
import com.zjty.autotest.pojo.test.Input;
import com.zjty.autotest.pojo.test.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Objects.nonNull;

/**
 * @author C
 */
@SuppressWarnings({"SpringAutowiredFieldsWarningInspection"})
@Service
public class TransHelper {

    @Autowired
    TestTask task;

    public TestReport toTestReport(EvaReport evaReport,String id){
        String out = JSON.toJSONString(evaReport);
        TestReport testReport = new TestReport();
        testReport.setResultId(id);
        testReport.setOutReport(out);
        return testReport;
    }

    public Project toProject(TestChannel testChannel){
        Project project = new Project();
        project.setBrowser(testChannel.getBrowser().toLowerCase());
        project.setName(testChannel.getName());
        project.setUrl(testChannel.getUrl());
        project.setFastMode(testChannel.getFastMode());
        List<RuleSet> ruleSets = testChannel.getRuleSets();
        List<Input> inputs = new ArrayList<>();
        project.setInputs(null);
        if (ruleSets != null) {
            for (RuleSet ruleSet : ruleSets) {
                Input input = new Input();
                input.setKey(ruleSet.getDes());
                input.setAttrName(ruleSet.getCommand());
                input.setAttrValue(ruleSet.getTarget());
                input.setValue(ruleSet.getValue());
                inputs.add(input);
            }
            project.setInputs(inputs);
        }
        return project;
    }

    public EvaReport toEvaReport(Project project, String os, List<Measure> measures) {
        EvaReport evaReport = new EvaReport();
        evaReport.setId(project.getId());
        evaReport.setCreateTime(new Date());
        evaReport.setProjectName(project.getName());
        evaReport.setBrowser(project.getBrowser());
        evaReport.setSystem(os);
        evaReport.setUrl(project.getUrl());
        if (nonNull(measures)) {
            long count = measures.stream().filter(m -> !m.getSuccess()).count();
            evaReport.setFailCount(project.getName() + "系统通过测试，" + "未通过测试点共0个");
            if (count > 0) {
                evaReport.setFailCount(project.getName() + "系统未通过测试，" + "未通过测试点共" + count + "个");
            }
            List<FailMessage> failMessages = measures.stream()
                    .filter(m -> !m.getSuccess())
                    .map(this::toFailMessage)
                    .collect(Collectors.toList());
            evaReport.setFailMessages(failMessages);
            List<PageContentTest> pageContents = measures.stream()
                    .map(this::toPageContentTest)
                    .collect(Collectors.toList());
            evaReport.setPageContents(pageContents);
            List<PageRoute> pageRoutes = measures.stream()
                    .map(this::toPageRoute)
                    .collect(Collectors.toList());
            evaReport.setPageRoutes(pageRoutes);
        }
        return evaReport;
    }

    private FailMessage toFailMessage(Measure measure) {
        FailMessage failMessage = new FailMessage();
        failMessage.setUrl(measure.getUrl());
        failMessage.setMessage(measure.getMessage());
        failMessage.setPicture(measure.getScreenshot());
        return failMessage;
    }

    private PageContentTest toPageContentTest(Measure measure) {
        PageContentTest pageContentTest = new PageContentTest();
        pageContentTest.setUrl(measure.getUrl());
        pageContentTest.setMessage(measure.getMessage());
        pageContentTest.setResponseTime(measure.getResponseTime());
        pageContentTest.setScreenshot(measure.getScreenshot());
        pageContentTest.setSuccess(0);
        if (measure.getSuccess()) {
            pageContentTest.setSuccess(1);
        }
        List<ElementDetail> elementDetails = measure.getElementDetails();
        if (nonNull(elementDetails)) {
            List<PageContent> contents = elementDetails.stream().map(this::toPageContent).collect(Collectors.toList());
            pageContentTest.setContents(contents);
        } else {
            pageContentTest.setContents(Collections.emptyList());
        }
        return pageContentTest;
    }

    private PageContent toPageContent(ElementDetail elementDetail) {
        PageContent pageContent = new PageContent();
        pageContent.setParms(elementDetail.getType());
        pageContent.setMessage(elementDetail.getMessage());
        pageContent.setResponseTime(elementDetail.getResponseTime());
        pageContent.setSuccess(0);
        if (elementDetail.getSuccess()) {
            pageContent.setSuccess(1);
        }
        return pageContent;
    }

    private PageRoute toPageRoute(Measure measure) {
        PageRoute pageRoute = new PageRoute();
        pageRoute.setPageName(measure.getUrl());
        pageRoute.setSuccess(0);
        if (measure.getSuccess()) {
            pageRoute.setSuccess(1);
        }
        pageRoute.setMessage("无/有");
        pageRoute.setPicture(measure.getScreenshot());
        pageRoute.setResponseTime(measure.getResponseTime());
        return pageRoute;
    }

}
