package com.kryptnostic.rhizome.pods;

import java.util.Set;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.CodecRegistry;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TypeCodec;
import com.google.common.annotations.VisibleForTesting;
import com.kryptnostic.rhizome.configuration.RhizomeConfiguration;
import com.kryptnostic.rhizome.configuration.cassandra.CassandraConfiguration;

@Configuration
@Profile( "cassandra" )
public class CassandraPod {
    private static final Logger  logger = LoggerFactory.getLogger( CassandraPod.class );

    @Inject
    private RhizomeConfiguration configuration;

    @Autowired(
        required = false )
    private Set<TypeCodec<?>>    codecs;

    @Bean
    public CassandraConfiguration cassandraConfiguration() {
        if ( !configuration.getCassandraConfiguration().isPresent() ) {
            throw new RuntimeException(
                    "Cassandra configuration is missing. Please add a cassandra configuration to rhizome.yaml" );
        }
        CassandraConfiguration cassandraConfiguration = configuration.getCassandraConfiguration().get();
        if ( cassandraConfiguration == null ) {
            logger.error(
                    "Seed nodes not found in cassandra configuration. Please add seed nodes to cassandra configuration block in rhizome.yaml" );
        }
        return cassandraConfiguration;
    }

    @Bean
    public Session sess() {
        return getCluster().newSession();
    }

    @Bean
    public CodecRegistry getCodecRegistry() {
        return new CodecRegistry().register( codecs );
    }

    @Bean
    public Cluster getCluster() {
        return new Cluster.Builder()
                .withPoolingOptions( getPoolingOptions() )
                .withProtocolVersion( ProtocolVersion.V3 )
                .addContactPoints( cassandraConfiguration().getCassandraSeedNodes() )
                .withCodecRegistry( getCodecRegistry() )
                .build();
    }

    private static PoolingOptions getPoolingOptions() {
        PoolingOptions poolingOptions = new PoolingOptions();
        return poolingOptions;
    }

    @VisibleForTesting
    public void setRhizomeConfig( RhizomeConfiguration config ) {
        this.configuration = config;
    }

    @VisibleForTesting
    public void setCodecs( Set<TypeCodec<?>> codecs ) {
        this.codecs = codecs;
    }
}

@@ -36,31 +36,8 @@
 s;%0A%0A
-import java.util.Set;%0A%0A
 impo
@@ -60,16 +60,16 @@
 Inject;%0A
+
 %0Aimport 
@@ -122,71 +122,8 @@
 ry;%0A
-import org.springframework.beans.factory.annotation.Autowired;%0A
 impo
@@ -332,55 +332,8 @@
 er;%0A
-import com.datastax.driver.core.CodecRegistry;%0A
 impo
@@ -470,51 +470,8 @@
 on;%0A
-import com.datastax.driver.core.TypeCodec;%0A
 impo
@@ -891,93 +891,8 @@
 n;%0A%0A
-    @Autowired(%0A        required = false )%0A    private Set%3CTypeCodec%3C?%3E%3E    codecs;%0A%0A
     
@@ -1665,126 +1665,8 @@
  %7D%0A%0A
-    @Bean%0A    public CodecRegistry getCodecRegistry() %7B%0A        return new CodecRegistry().register( codecs );%0A    %7D%0A%0A
     
@@ -1950,65 +1950,8 @@
 ) )%0A
-                .withCodecRegistry( getCodecRegistry() )%0A
     
@@ -2220,24 +2220,24 @@
  config ) %7B%0A
+
         this
@@ -2270,123 +2270,7 @@
    %7D
-%0A%0A    @VisibleForTesting%0A    public void setCodecs( Set%3CTypeCodec%3C?%3E%3E codecs ) %7B%0A        this.codecs = codecs;%0A    %7D
 %0A%7D%0A

Revert "register codecs"