package cn.com.taiji.track.config;

import cn.com.taiji.track.dto.IGeomesaTrackDTO;
import org.geotools.data.DataStoreFinder;
import org.locationtech.geomesa.redis.data.RedisDataStore;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractRedisDataStoreConfig {
    @Value("#{'${taiji.geomesa.store.redis.config}'.split('###')}")
    private String[] redisStoreConfig;

    @Bean(name = "redisDataStore")
    public RedisDataStore initRedisDataStore() throws IOException {
        Map<String, Serializable> parameters = new HashMap<>();
        for (int i=0; i<redisStoreConfig.length; i++) {
            String param[] = redisStoreConfig[i].split("===");
            parameters.put(param[0], param[1]);
        }
        return (RedisDataStore) DataStoreFinder.getDataStore(parameters);
    }

    @Bean(name = "redisClient")
    public Jedis redisClient() {
        Jedis client = null;
        for (int i=0; i<redisStoreConfig.length; i++) {
            String param[] = redisStoreConfig[i].split("===");
            if ("redis.url".equals(param[0])) {
                client = new Jedis(param[1]);
                break;
            }
        }
        return client;
    }

//    abstract SimpleFeatureType sft(RedisDataStore redisDataStore, IGeomesaTrackDTO geomesaTrackDTO) throws IOException;

    abstract SimpleFeatureType sftLocation(RedisDataStore redisDataStore, IGeomesaTrackDTO geomesaLocationDTO) throws IOException;
}
