package com.tofly.analyse.util.analysis;

import com.supermap.analyst.networkanalyst.*;
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.Arrays;
import java.util.List;

@Component
public class AnalystConfig {


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

        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 && StringUtils.isNotEmpty(layfiledConfig.getSubtype()) && StringUtils.isNotEmpty(layfiledConfig.getSubtypeValue())) {
                List<Integer> lstBrrierNode = new ArrayList<Integer>();
                DatasetVector vecPNetworkNode = (DatasetVector) networkPipe;
                String param=null;
                if(layfiledConfig.getSubtypeValue().contains(",")){
                    String[] split = layfiledConfig.getSubtypeValue().split(",");
                    List<String> strings = Arrays.asList(split);
                    StringBuffer sb=new StringBuffer();
                    for(int i=0;i<strings.size();i++){
                        if(i!= strings.size()-1){
                            sb.append(layfiledConfig.getSubtype());
                            sb.append("=");
                            sb.append("'");
                            sb.append(strings.get(i));
                            sb.append("'");
                            sb.append(" or ");
                        }else{
                            sb.append(layfiledConfig.getSubtype());
                            sb.append("=");
                            sb.append("'");
                            sb.append(strings.get(i));
                            sb.append("'");
                        }
                    }
                    param=sb.toString();
                }else{
                    param=layfiledConfig.getSubtype()+"= '"+layfiledConfig.getSubtypeValue()+"'";
                }
                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;
    }

    public FacilityAnalyst loadModel(DatasetVector networkPipe, int[] barrId) {
        // 构造一个设施网络分析对象，并设置设施网络分析环境
        FacilityAnalyst facilityAnalyst = new FacilityAnalyst();
        try {
            // 构造一个设施网络分析环境设置对象，并设置相关参数
            FacilityAnalystSetting facilityAnalystSetting = new FacilityAnalystSetting();
            facilityAnalystSetting.setNetworkDataset(networkPipe);
            facilityAnalystSetting.setDirectionField("Direction");
            facilityAnalystSetting.setEdgeIDField("SMEDGEID");
            facilityAnalystSetting.setNodeIDField("SMNODEID");
            facilityAnalystSetting.setFNodeIDField("SMFNODE");
            facilityAnalystSetting.setTNodeIDField("SMTNODE");
            facilityAnalystSetting.setTolerance(0.001);
            facilityAnalystSetting.setBarrierNodes(barrId);

            WeightFieldInfo weightFieldInfo = new WeightFieldInfo();
            weightFieldInfo.setName("Length");
            weightFieldInfo.setFTWeightField("SMLENGTH");
            weightFieldInfo.setTFWeightField("SMLENGTH");
            WeightFieldInfos weightFieldInfos = new WeightFieldInfos();
            weightFieldInfos.add(weightFieldInfo);
            facilityAnalystSetting.setWeightFieldInfos(weightFieldInfos);
            facilityAnalyst.setAnalystSetting(facilityAnalystSetting);

            // 加载网络模型
            Boolean isLoad = facilityAnalyst.load();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return facilityAnalyst;
    }


    /// <summary>
    /// 加载传输网络分析模型
    /// </summary>
    /// <param name="networkPipe">进行分析的管线网络数据集</param>
    public TransportationAnalyst loadModel(DatasetVector networkPipe) {
        TransportationAnalyst m_transAnalyst = new TransportationAnalyst();
        try {
            TransportationAnalystSetting analystSetting = new TransportationAnalystSetting();
            analystSetting.setNetworkDataset(networkPipe);
            analystSetting.setEdgeIDField("SmID"); //SMEDGEID
            analystSetting.setNodeIDField("SmNodeID");
            analystSetting.setFNodeIDField("SmFNode");        //SMFNODE		ssfnode
            analystSetting.setTNodeIDField("SmTNode");         //SMTNODE		sstnode
//	  		analystSetting.setTolerance(0);
            WeightFieldInfo weightFieldInfo = new WeightFieldInfo();
            weightFieldInfo.setName("SmLength");
            weightFieldInfo.setFTWeightField("SmLength");
            weightFieldInfo.setTFWeightField("SmLength");
            WeightFieldInfos weightFieldInfos = new WeightFieldInfos();
            weightFieldInfos.add(weightFieldInfo);
            analystSetting.setWeightFieldInfos(weightFieldInfos);

            m_transAnalyst.setAnalystSetting(analystSetting);

            //加载网络模型
            boolean load = m_transAnalyst.load();
            System.out.println(load);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return m_transAnalyst;
    }
}