package com.tofly.analyse.util.analysis;

import com.supermap.analyst.networkanalyst.FacilityAnalyst;
import com.supermap.analyst.networkanalyst.FacilityAnalystSetting;
import com.supermap.analyst.networkanalyst.WeightFieldInfo;
import com.supermap.analyst.networkanalyst.WeightFieldInfos;
import com.supermap.data.CursorType;
import com.supermap.data.DatasetVector;
import com.supermap.data.Recordset;
import com.tofly.analyse.entity.LayerFiledConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

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

@Component
public class PipeTruncConfig {


    /// <summary>
    /// 加载设施网络分析模型
    /// </summary>
    /// <param name="networkPipe">进行分析的管线网络数据集</param>
    /// <param name="useBarrier">是否启用障碍</param>
    public FacilityAnalyst loadModel(DatasetVector networkPipe, boolean useBarrier, LayerFiledConfig layfiledConfig,String type) {

        FacilityAnalyst facilityAnalyst = new FacilityAnalyst();
        try {
            FacilityAnalystSetting facilityAnalystSetting = new FacilityAnalystSetting();
            facilityAnalystSetting.setNetworkDataset((DatasetVector) networkPipe);
            // facilityAnalystSetting.setDirectionField("Flow_D".toLowerCase());
            facilityAnalystSetting.setEdgeIDField("SmEdgeID"); //SMEDGEID
            facilityAnalystSetting.setNodeIDField("SmNodeID");
            facilityAnalystSetting.setFNodeIDField("SmFNode");        //SMFNODE		ssfnode
            facilityAnalystSetting.setTNodeIDField("SmTNode");         //SMTNODE		sstnode
            facilityAnalystSetting.setTolerance(0.001);
            if(StringUtils.isNotEmpty(layfiledConfig.getDir())){
                facilityAnalystSetting.setDirectionField(layfiledConfig.getDir());
            }else{
                facilityAnalystSetting.setDirectionField("Direction");
            }
            WeightFieldInfo weightFieldInfo = new WeightFieldInfo();
            weightFieldInfo.setName("Length");
            weightFieldInfo.setFTWeightField("SmLength");
            weightFieldInfo.setTFWeightField("SmLength");
            WeightFieldInfos weightFieldInfos = new WeightFieldInfos();
            weightFieldInfos.add(weightFieldInfo);
            facilityAnalystSetting.setWeightFieldInfos(weightFieldInfos);


            if (useBarrier) {
                List<Integer> lstBrrierNode = new ArrayList<Integer>();
                DatasetVector vecPNetworkNode = (DatasetVector) networkPipe;
                String param=layfiledConfig.getSubtype()+"= '";
                if(type.equals("0")){
                    param+=layfiledConfig.getSySubtypeValue()+"'";
                }else{
                    param+=layfiledConfig.getXySubtypeValue()+"'";
                }
                Recordset recNodeBarrier = vecPNetworkNode.getChildDataset().query(param, CursorType.STATIC);

                recNodeBarrier.moveFirst();
                while (recNodeBarrier.isEOF() == false) {
                    if (recNodeBarrier.getID() > 0) {
                        lstBrrierNode.add(recNodeBarrier.getID());
                    }
                    recNodeBarrier.moveNext();
                }
                System.out.println(lstBrrierNode.stream().mapToInt(Integer::intValue).toArray().length + "###");
                facilityAnalystSetting.setBarrierNodes(lstBrrierNode.stream().mapToInt(Integer::intValue).toArray());
                recNodeBarrier.close();
                recNodeBarrier.dispose();
            }
            facilityAnalyst.setAnalystSetting(facilityAnalystSetting);


            Boolean blLoadResult = facilityAnalyst.load();
            System.out.println(blLoadResult);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return facilityAnalyst;
    }


}