package com.njcn.advance.utils;

import cn.hutool.core.date.TimeInterval;
import com.njcn.advance.pojo.bo.QtIdxArray;
import com.njcn.advance.pojo.bo.QvvrDataStruct;
import com.njcn.advance.pojo.dto.BalanceInfo;
import com.njcn.advance.pojo.dto.relevent.QtIdx;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/6/20
 */
@Component
public class GetBalanceUtils {


    @Data
    public static class Sarifi {
        private Float sarifiValue;
        private Float time;
        private Float pt1;
        private Float pt2;
    }


    public void translateData(List<BalanceInfo> list) {
        QvvrDataStruct qvvrDataStruct = new QvvrDataStruct();

        // 过滤数据
        List<BalanceInfo> newList = new ArrayList<>();
        filterData(list, newList);

        // 如果newList为空则不带入dll计算
        if (!newList.isEmpty()) {
            packageData(newList, qvvrDataStruct);
            JnaCallDllOrSo jnaCallDll = new JnaCallBalance("qvvr_balance");
            System.out.println("路径>>>>>>>"+jnaCallDll.getStrpath());
            jnaCallDll.setPath();
            // 计算暂降综合评估
            try {
                JnaCallBalance.Balancelibrary INSTANTCE = JnaCallBalance.Balancelibrary.INSTANTCE;
                TimeInterval timeInterval = new TimeInterval();
                INSTANTCE.qvvr_fun_cause(qvvrDataStruct);
                System.out.println("dll算法时长:+++++++++++"+timeInterval.interval());
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }

            resultPackage(newList, qvvrDataStruct, list);
        }
    }

    public void resultPackage(List<BalanceInfo> list, QvvrDataStruct qvvrDataStruct, List<BalanceInfo> list2) {
        for (int i = 0; i < list.size(); i++) {
            for (BalanceInfo balanceInfo : list2) {
                if (balanceInfo.getAreaIndex().equals(list.get(i).getAreaIndex())) {
                    balanceInfo.setCi(qvvrDataStruct.sys_res[i].CI);
                    // 已计算
                    balanceInfo.setIsCount(1);
                }
            }

            for (int j = 0; j < list.get(i).getList().size(); j++) {
                // 设置监测点的评估数据
                list.get(i).getList().get(j).setCiv(qvvrDataStruct.sys_res[i].CIV[j]);
                // 设置监测点的评估等级
                list.get(i).getList().get(j).setL(qvvrDataStruct.sys_res[i].L[j]);
            }
        }
    }

    public void packageData(List<BalanceInfo> list, QvvrDataStruct qvvrDataStruct) {
        // 系统数目
        qvvrDataStruct.sys_num = list.size();

        for (int i = 0; i < qvvrDataStruct.sys_num; i++) {
            // 监测点数目
            qvvrDataStruct.line_num[i] = list.get(i).getList().size();
            QtIdxArray qtIdxArray = new QtIdxArray();

            for (int j = 0; j < list.get(i).getList().size(); j++) {
                // 各监测点数据
                qtIdxArray.qtIdxs[j] = list.get(i).getList().get(j).getQtIdx();
            }

            qvvrDataStruct.line_idx[i] = qtIdxArray;
        }
    }

    /**
     * 过滤掉没有监测点，没有暂降事件的数据
     */
    private void filterData(List<BalanceInfo> list, List<BalanceInfo> listResult) {
        for (BalanceInfo balanceInfo : list) {
            BalanceInfo balanceInfo2 = new BalanceInfo();
            List<BalanceInfo.PointInfo> list2 = new ArrayList<>();

            if (balanceInfo.getList() == null) {
                continue;
            }

            for (int i = 0; i < balanceInfo.getList().size(); i++) {
                QtIdx qtIdx = balanceInfo.getList().get(i).getQtIdx();

                if (qtIdx == null || qtIdx.sarfi_90 == 0) {
                    continue;
                }

                list2.add(balanceInfo.getList().get(i));
            }

            // 发生事件的监测点数目少于4组不带入计算
            if (list2.size() > 4) {
                balanceInfo2.setAreaIndex(balanceInfo.getAreaIndex());
                balanceInfo2.setList(list2);
                listResult.add(balanceInfo2);
            }
        }
    }

    private BalanceInfo.PointInfo newObj(BalanceInfo.PointInfo info, double v1, int c1, int c2, double v2, double v3) {
        info.getQtIdx().r_esm = (float) v1;
        info.getQtIdx().sarfi_90 = c1;
        info.getQtIdx().sarifi_50 = c2;
        info.getQtIdx().r_asei = (float) v2;
        info.getQtIdx().r_assi = (float) v3;
        return info;
    }

}
