package incheon.ags.mrb.upload.service;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.geotools.api.feature.simple.SimpleFeatureType;
import incheon.ags.mrb.upload.mapper.ProcessFileMapper;

@Service
public class TableCreationService {

    private final static Logger logger = LoggerFactory.getLogger(TableCreationService.class);
    
    private final ProcessFileMapper processFileMapper;
    private final FileProcessUtilService fileProcessUtilService;
    
    @Autowired
    public TableCreationService(ProcessFileMapper processFileMapper,
                                FileProcessUtilService fileProcessUtilService) {
        this.processFileMapper = processFileMapper;
        this.fileProcessUtilService = fileProcessUtilService;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Map<String, Object> createTable(String tableName, SimpleFeatureType schema, int srid) {
        String geomType = fileProcessUtilService.getGeometryType(schema.getGeometryDescriptor());
        String columnsSql = fileProcessUtilService.generateColumnsSqlFromSchema(schema);

        processFileMapper.createShapefileTable(tableName, geomType, srid, columnsSql);

        Map<String, Object> result = new HashMap<>();
        result.put("tableName", tableName);
        result.put("srid", srid);
        result.put("success", true);

        return result;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Map<String, Object> createCsvTable(String tableName, int srid, String columnsSql) {
        processFileMapper.createTable(tableName, srid, columnsSql);

        Map<String, Object> result = new HashMap<>();
        result.put("tableName", tableName);
        result.put("srid", srid);
        result.put("success", true);

        return result;
    }
    
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Map<String, Object> createCsvwktTable(String tableName, int srid, String columnsSql) {
        processFileMapper.createTableWithGeometry(tableName, srid, columnsSql);

        Map<String, Object> result = new HashMap<>();
        result.put("tableName", tableName);
        result.put("srid", srid);
        result.put("success", true);

        return result;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Map<String, Object> createDxfTable(String tableName, String geomType, int srid, String columnsSql) {
        processFileMapper.createDxfTable(tableName, geomType, srid, columnsSql);

        Map<String, Object> result = new HashMap<>();
        result.put("tableName", tableName);
        result.put("srid", srid);
        result.put("geomType", geomType);
        result.put("success", true);
        return result;
    }
}
