package cn.com.taiji.track.config;

import cn.com.taiji.track.dto.BeidouShipLocationDTO;
import cn.com.taiji.track.dto.IGeomesaTrackDTO;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.geomesa.redis.data.RedisDataStore;
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author kok20
 */
@Slf4j
@Configuration
@ConditionalOnProperty(name = "taiji.kafka.consumer.beidou.enable",havingValue = "true")
public class BeidouShipRedisDataStoreConfig extends AbstractRedisDataStoreConfig {

    @Value("${taiji.kafka.consumer.beidou.group}")
    private String group;
    @Value("${taiji.kafka.consumer.beidou.expiry}")
    private Integer expiry;

//    @Override
//    @Bean(name = "sft")
//    public SimpleFeatureType sft(RedisDataStore redisDataStore, IGeomesaTrackDTO geomesaTrackDTO) throws IOException {
//        SimpleFeatureType sft = SimpleFeatureTypes.createType(geomesaTrackDTO.getTypeName(),  geomesaTrackDTO.getSimpleFeatureType().toString());
//        sft.getUserData().put("geomesa.feature.expiry", StrUtil.format("syncTime({} seconds)", expiry));
//
//        try {
//            redisDataStore.getFeatureSource(geomesaTrackDTO.getTypeName()).removeFeatures(Filter.INCLUDE);
//            redisDataStore.removeSchema(geomesaTrackDTO.getTypeName());
//        } catch (Exception e) {
//            log.error("可能属于首次构建，SCHEMA 并不存在");
//            e.printStackTrace();
//        }
//
//        redisDataStore.createSchema(sft);
//        return sft;
//    }

    @Override
    @Bean(name = "sftLocation")
    public SimpleFeatureType sftLocation(RedisDataStore redisDataStore, IGeomesaTrackDTO geomesaLocationDTO) throws IOException {
        SimpleFeatureType sft = SimpleFeatureTypes.createType(geomesaLocationDTO.getTypeName(),  geomesaLocationDTO.getSimpleFeatureType().toString());
        sft.getUserData().put("geomesa.feature.expiry", StrUtil.format("syncTime({} seconds)", expiry));

        try {
            redisDataStore.getFeatureSource(geomesaLocationDTO.getTypeName()).removeFeatures(Filter.INCLUDE);
            redisDataStore.removeSchema(geomesaLocationDTO.getTypeName());
        } catch (Exception e) {
            log.error("可能属于首次构建，SCHEMA 并不存在");
            e.printStackTrace();
        }

        redisDataStore.createSchema(sft);
        return sft;
    }

//    @Bean(name = "geomesaTrackDTO")
//    public BeidouShipTrackDTO geomesaTrackDTO() {
//        BeidouShipTrackDTO dto = new BeidouShipTrackDTO();
//        dto.setKafkaConsumerGroup(group);
//        dto.setExpirySeconds(expiry);
//        return dto;
//    }

    @Bean(name = "geomesaLocationDTO")
    public BeidouShipLocationDTO geomesaLocationDTO() {
        BeidouShipLocationDTO dto = new BeidouShipLocationDTO();
        dto.setKafkaConsumerGroup(group);
        dto.setExpirySeconds(expiry);
        return dto;
    }
}
