package com.tofly.analyse.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.supermap.data.*;
import com.supermap.data.conversion.DataImport;
import com.supermap.data.conversion.ImportMode;
import com.supermap.data.conversion.ImportResult;
import com.supermap.data.conversion.ImportSettingDWG;
import com.tofly.analyse.service.ImportTranService;
import com.tofly.analyse.util.analysis.DatasourceConfig;
import com.tofly.analyse.util.analysis.MultipartFileToFileUtils;
import com.tofly.common.core.entity.ResultRespone;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhoux
 */
@AllArgsConstructor
@Service("importTran")
public class ImportTranServiceImpl  implements ImportTranService {
	
	@Autowired
	private DatasourceConfig datasourceConfig;


	@Override
	public ResultRespone getTranData(MultipartFile file,String path) {
		Map<String,Object> map=new HashMap<>();
		if(!file.isEmpty()){
			String s = MultipartFileToFileUtils.saveMultipartFile(file, path);
			if(!s.isEmpty()){
				Workspace workspace = new Workspace();
				Datasource datasource = null;
				try {
					datasource = datasourceConfig.getDatasource(workspace);
					String[] strings = importDWG(datasource, s);
					if(strings!=null && strings.length>0){
						Datasets datasets = datasource.getDatasets();
						for (String layerName : strings) {
							if (datasets.contains(layerName)) {
								List<Map<String,Object>> resultMap=new ArrayList<>();
								DatasetVector datasetVector = (DatasetVector) datasets.get(layerName);
								Recordset recordset = datasetVector.getRecordset(false, CursorType.STATIC);
								recordset.moveFirst();
								FieldInfos fieldInfos = recordset.getFieldInfos();
								FieldInfo[] fieldInfos1 = fieldInfos.toArray();
								while (!recordset.isEOF()) {
									Map<String,Object> dataMap=new HashMap<>();
									if(CollUtil.isEmpty(dataMap)){
										Geometry geometry = recordset.getGeometry();
										dataMap.put("geometry",Toolkit.GeometryToGeoJson(geometry));
									}
									if(!dataMap.get("geometry").toString().equals("null")){
										for(FieldInfo d:fieldInfos1){
											Object fieldValue = recordset.getFieldValue(d.getName());
											dataMap.put(d.getName(),fieldValue);
										}
										resultMap.add(dataMap);
									}
									recordset.moveNext();
								}
								if(!resultMap.isEmpty()){
									map.put(layerName,resultMap);
								}
								recordset.dispose();
							}
						}
					}
				}catch (Exception e) {
					e.printStackTrace();
					return ResultRespone.failed(e.getMessage());
				} finally {
					if (datasource.isOpened())
						datasource.close();
					workspace.close();
					workspace.dispose();
				}
			}
		}else{
			return ResultRespone.failed("文件不能为空");
		}
		return ResultRespone.success(map);
	}

	public String[] importDWG(Datasource datasource,String path) {
		try {
			ImportSettingDWG importSettingDWG = new ImportSettingDWG();
			importSettingDWG.setImportMode(ImportMode.OVERWRITE);
			importSettingDWG.setImportingAsCAD(false);
			importSettingDWG.setTargetDatasource(datasource);
			importSettingDWG.setSourceFilePath(path);
			DataImport dataImport = new DataImport();
			dataImport.getImportSettings().add(importSettingDWG);
			ImportResult run = dataImport.run();
			String[] succeedDatasetNames = run.getSucceedDatasetNames(importSettingDWG);
			return succeedDatasetNames;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  null;
	}
}