package com.inspect.analysis.domain;

import com.inspect.partrolresult.domain.AnalyseReqItem;
import com.inspect.partrolresult.domain.AnalyseRequest;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
@Slf4j
@ToString
public class AnalyseResult implements Serializable {
    private String requestId;
    private List<AnalyseResItem> resultList;
    private List<AnalyseResItem> resultsList;
    private String taskPatrolId;
    private String filter = "0";
    private String result = "1";
    private boolean isTest = false;
    private int totalNumber;
    private boolean isCompensate = false; // false-normal; true-compensate

    public void setResultsList(List<AnalyseResItem> resultsList) {
        this.resultsList = resultsList;
        if (resultsList != null) {
            this.resultList = resultsList;
        }
    }

    public void reloadReq(AnalyseRequest analyseRequest) {
        this.totalNumber = analyseRequest.getTotalNumber();
        AnalyseResItem analyseResItem = this.resultList.get(0);
        List<AnalyseResItem> resultList = new ArrayList<>();
        this.setResultsList(resultList);

        // 根据请求时的点位循环
        for (int i = 0; i < analyseRequest.getObjectList().size(); ++i) {
            AnalyseResItem resItem = analyseResItem.clone();
            resultList.add(resItem);
            AnalyseReqItem analyseReqItem = analyseRequest.getObjectList().get(i);
            List<AnalyseResPoint> results = new ArrayList<>();
            // 根据请求时的点位算法类型循环
            for (String type : analyseReqItem.getTypeList()) {
                AnalyseResPoint resPoint = analyseResItem.getResults().get(0);
                List<AnalyseResPoint> resPointList = analyseResItem.getResults().stream().filter(res -> Arrays.asList(res.getType().split(",")).contains(type)).collect(Collectors.toList());
                if (!resPointList.isEmpty()) {
                    resPoint = resPointList.get(0);
                    // 合并同一算法的value
                    AnalyseResPoint finalResPoint = resPoint;
                    String value = resPointList.stream().map(res ->
                            "-9999".equals(finalResPoint.getValue()) ? "" : res.getValue()
                    ).collect(Collectors.joining(","));
                    resPoint.setValue(value);
                    // 合并同一算法的pos
                    List<Pos> pos = resPointList.stream().map(res -> res.getPos()).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());
                    resPoint.setPos(pos);
                }
                AnalyseResPoint clone = resPoint.clone();
                clone.setType(type);
                results.add(clone);
            }
            resItem.setResults(results);
        }
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public void setResultList(List<AnalyseResItem> resultList) {
        this.resultList = resultList;
    }

    public void setTaskPatrolId(String taskPatrolId) {
        this.taskPatrolId = taskPatrolId;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public void setTest(boolean test) {
        this.isTest = test;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;
        AnalyseResult that = (AnalyseResult) object;
        return Objects.equals(requestId, that.requestId) && Objects.equals(resultList, that.resultList) && Objects.equals(resultsList, that.resultsList) && Objects.equals(taskPatrolId, that.taskPatrolId) && Objects.equals(filter, that.filter) && Objects.equals(result, that.result);
    }

    @Override
    public int hashCode() {
        return Objects.hash(requestId, resultList, resultsList, taskPatrolId, filter, result);
    }
}
