package com.njcn.advance.event.cause.model;

/**
 * 电压暂降分析结果类
 * 包含处理状态和详细分析信息
 */
public class AnalysisResult {
    
    /**
     * 处理结果状态码
     */
    private int status;
    
    /**
     * 电压暂降原因代码
     */
    private int cause;
    
    /**
     * 原因描述字符串
     */
    private String causeDescription;
    
    /**
     * 判断状态码（算法内部路径）
     */
    private int judgeStatus;
    
    /**
     * 处理时间（毫秒）
     */
    private long processingTime;
    
    /**
     * 错误信息（失败时）
     */
    private String errorMessage;
    
    /**
     * 详细特征信息
     */
    private DataFeature features;
    
    /**
     * 输入数据基本信息
     */
    private String inputInfo;
    
    /**
     * 构造函数 - 成功结果
     */
    public AnalysisResult(int cause, String causeDescription, int judgeStatus,
                          long processingTime, DataFeature features, String inputInfo) {
        this.status = 0;  // 成功
        this.cause = cause;
        this.causeDescription = causeDescription;
        this.judgeStatus = judgeStatus;
        this.processingTime = processingTime;
        this.features = features;
        this.inputInfo = inputInfo;
        this.errorMessage = null;
    }
    
    /**
     * 构造函数 - 失败结果
     */
    public AnalysisResult(int status, String errorMessage, String inputInfo) {
        this.status = status;
        this.cause = 0;  // 未知原因
        this.causeDescription = "未知原因";
        this.judgeStatus = -1;
        this.processingTime = 0;
        this.features = null;
        this.inputInfo = inputInfo;
        this.errorMessage = errorMessage;
    }
    
    // Getters
    public int getStatus() { return status; }
    public int getCause() { return cause; }
    public String getCauseDescription() { return causeDescription; }
    public int getJudgeStatus() { return judgeStatus; }
    public long getProcessingTime() { return processingTime; }
    public String getErrorMessage() { return errorMessage; }
    public DataFeature getFeatures() { return features; }
    public String getInputInfo() { return inputInfo; }
    
    /**
     * 是否成功
     */
    public boolean isSuccess() {
        return status == 0;
    }
    
    /**
     * 获取判断路径描述
     */
    public String getJudgeStatusDescription() {
        switch (judgeStatus) {
            case 1: return "暂降前不平衡异常";
            case 2: return "短时浅暂降判断为电压跌落";
            case 3: return "单相接地短路特征";
            case 4: return "不对称故障特征";
            case 5: return "感应电机启动特征（长时间+低SVD）";
            case 6: return "三相对称短路故障";
            case 7: return "电压调节器特征（谐波超标）";
            case 8: return "其他情况归类为短路故障";
            case 9: return "电压严重超限判断为短路故障";
            case 10: return "短时严重暂降判断为短路故障";
            default: return "未知判断路径";
        }
    }
    
    /**
     * 获取完整的分析摘要
     */
    public String getSummary() {
        if (!isSuccess()) {
            return String.format("分析失败: %s\n输入信息: %s", errorMessage, inputInfo);
        }
        
        StringBuilder sb = new StringBuilder();
        sb.append("=== 电压暂降分析结果 ===\n");
        sb.append(String.format("输入信息: %s\n", inputInfo));
        sb.append(String.format("分析结果: %s (代码: %d)\n", causeDescription, cause));
        sb.append(String.format("判断路径: %s (状态码: %d)\n", getJudgeStatusDescription(), judgeStatus));
        sb.append(String.format("处理时间: %d ms\n", processingTime));
        
        if (features != null) {
            sb.append(String.format("事件时段: TS=%d, TE=%d, 持续时间=%d个采样点\n", 
                                   features.getTS(), features.getTE(), features.getHoldTime()));
            sb.append(String.format("电压特征: 最小值=%.3f, 最大值=%.3f\n", 
                                   features.getU3Min(), features.getU3Max()));
            sb.append(String.format("相序特征: 负序=%.3f, 零序=%.3f, 不平衡度=%.3f\n", 
                                   features.getU2Avg(), features.getU0Avg(), features.getBphAvg()));
            sb.append(String.format("椭圆特征: bi1=%.3f, bi2=%.3f\n", 
                                   features.getBi1(), features.getBi2()));
            sb.append(String.format("SVD特征: %.6f\n", features.getSvd()));
        }
        
        return sb.toString();
    }
    
    @Override
    public String toString() {
        return getSummary();
    }
}