package com.njcn.gather.tools.comtrade.comparewave.core.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import static com.njcn.gather.tools.comtrade.comparewave.core.Constants.*;


/**
 * 电能质量数据结构
 * <p>对应C代码：pqs_dat_struct</p>
 * <p><b>重要：所有数组维度和计算逻辑必须与C代码完全一致</b></p>
 * 
 * @author hongawen
 * @since 1.0
 */
@Data
@NoArgsConstructor
public class PqsDataStruct {
    
    /** 相电压、电流有效值数组 */
    private float[] rms = new float[9];
    
    /** 正偏差有效值数组 */
    private float[] rmsPos = new float[6];
    
    /** 负偏差有效值数组 */
    private float[] rmsNeg = new float[6];
    
    /** 电压偏差数组 */
    private float[] uDev = new float[6];
    
    /** 电压正偏差数组 */
    private float[] uPosDev = new float[6];
    
    /** 电压负偏差数组 */
    private float[] uNegDev = new float[6];
    
    /** 谐波有效值数组 [通道][谐波次数] (最多127次谐波) */
    private float[][] fuHarm = new float[MAX_CH_NUM][MAX_HARM_TIMES];
    
    /** 谐波相位数组 [通道][谐波次数] */
    private float[][] fuHarmPhase = new float[MAX_CH_NUM][MAX_HARM_TIMES];
    
    /** 谐波含有率数组 [通道][谐波次数] */
    private float[][] fuHarmCON = new float[MAX_CH_NUM][MAX_HARM_TIMES];
    
    /** 间谐波有效值数组 [通道][谐波次数] */
    private float[][] inHarm = new float[MAX_CH_NUM][MAX_HARM_TIMES];
    
    /** 间谐波含有率数组 [通道][谐波次数] */
    private float[][] inHarmCON = new float[MAX_CH_NUM][MAX_HARM_TIMES];
    
    /** 谐波总有效值数组 */
    private float[] harmRMS = new float[MAX_CH_NUM];
    
    /** 间谐波总有效值数组 */
    private float[] iHarmRMS = new float[MAX_CH_NUM];
    
    /** 总谐波畸变率数组 */
    private float[] harmTHD = new float[MAX_CH_NUM];
    
    /** 奇次谐波畸变率数组 */
    private float[] harmOTHD = new float[MAX_CH_NUM];
    
    /** 偶次谐波畸变率数组 */
    private float[] harmETHD = new float[MAX_CH_NUM];
    
    /** 线电压谐波有效值数组 [3相][谐波次数] */
    private float[][] ppvFuHarm = new float[3][MAX_HARM_TIMES];
    
    /** 线电压谐波含有率数组 [3相][谐波次数] */
    private float[][] ppvFuHarmCON = new float[3][MAX_HARM_TIMES];
    
    /** 线电压谐波总有效值数组 */
    private float[] ppvHarmRMS = new float[3];
    
    /** 线电压总谐波畸变率数组 */
    private float[] ppvHarmTHD = new float[3];
    
    /** 线电压奇次谐波畸变率数组 */
    private float[] ppvHarmOTHD = new float[3];
    
    /** 线电压偶次谐波畸变率数组 */
    private float[] ppvHarmETHD = new float[3];
    
    /** 电压电流序分量数组 [电压/电流][序分量类型] */
    private float[][] uiSeq = new float[2][5];
    
    /** 各次谐波有功功率数组 [相别][谐波次数] */
    private float[][] harmP = new float[4][MAX_HARM_TIMES];
    
    /** 各次谐波无功功率数组 [相别][谐波次数] */
    private float[][] harmQ = new float[4][MAX_HARM_TIMES];
    
    /** 各次谐波视在功率数组 [相别][谐波次数] */
    private float[][] harmS = new float[4][MAX_HARM_TIMES];
    
    /** 总有功功率数组 */
    private float[] totalP = new float[4];
    
    /** 总无功功率数组 */
    private float[] totalQ = new float[4];
    
    /** 总视在功率数组 */
    private float[] totalS = new float[4];
    
    /** 功率因数数组 */
    private float[] cosPF = new float[4];
    
    /** 位移功率因数数组 */
    private float[] cosDF = new float[4];
    
    /** 谐波有功功率数组 (有符号) */
    private float[] hwTotalS = new float[4];
    
    /** 谐波有功功率数组 (无符号) */
    private float[] hwTotalU = new float[4];
    
    /** 谐波无功功率数组 (有符号) */
    private float[] hvarTotalS = new float[4];
    
    /** 谐波无功功率数组 (无符号) */
    private float[] hvarTotalU = new float[4];
    
    /** 谐波视在功率数组 (有符号) */
    private float[] hvaTotalS = new float[4];
    
    /** 谐波视在功率数组 (无符号) */
    private float[] hvaTotalU = new float[4];
    
    /** 频率和频率偏差数组 */
    private float[] freq = new float[2];
    
    /** 短时闪变数组 */
    private float[] pst = new float[3];
    
    /** 长时闪变数组 */
    private float[] plt = new float[3];
    
    /** 电压波动数组 */
    private float[] flt = new float[3];
    
    /** 数据时间戳 */
    private ClockStruct clocktime;
    
    /**
     * 初始化数据结构
     * <p>创建时间戳对象，数组已在声明时自动初始化</p>
     */
    public void init() {
        // 创建时间戳对象，数组已在声明时自动初始化为零值
        clocktime = new ClockStruct();
    }
    
    /**
     * 复制构造函数
     * <p>深拷贝所有数组和对象</p>
     * 
     * @param other 要复制的源对象
     */
    public PqsDataStruct(PqsDataStruct other) {
        // 深拷贝一维数组
        this.rms = other.rms.clone();
        this.rmsPos = other.rmsPos.clone();
        this.rmsNeg = other.rmsNeg.clone();
        this.uDev = other.uDev.clone();
        this.uPosDev = other.uPosDev.clone();
        this.uNegDev = other.uNegDev.clone();
        
        // 深拷贝二维数组的每个子数组
        for (int i = 0; i < this.fuHarm.length; i++) {
            this.fuHarm[i] = other.fuHarm[i].clone();
            this.fuHarmPhase[i] = other.fuHarmPhase[i].clone();
            this.fuHarmCON[i] = other.fuHarmCON[i].clone();
            this.inHarm[i] = other.inHarm[i].clone();
            this.inHarmCON[i] = other.inHarmCON[i].clone();
        }
        
        // 复制其他一维数组
        this.harmRMS = other.harmRMS.clone();
        this.iHarmRMS = other.iHarmRMS.clone();
        this.harmTHD = other.harmTHD.clone();
        this.harmOTHD = other.harmOTHD.clone();
        this.harmETHD = other.harmETHD.clone();
        
        // 复制时间戳对象
        if (other.clocktime != null) {
            this.clocktime = new ClockStruct(
                other.clocktime.getYear(),
                other.clocktime.getMonth(),
                other.clocktime.getDay(),
                other.clocktime.getHour(),
                other.clocktime.getMinute(),
                other.clocktime.getSecond(),
                other.clocktime.getMicroSecond()
            );
        }
    }
}