package io.korobi.mongotoelastic.elasticsearch;

import io.korobi.mongotoelastic.logging.InjectLogger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;

import javax.inject.Inject;

public class IndexInitialiser {

    @InjectLogger
    private Logger logger;
    private final Client esClient;

    @Inject
    public IndexInitialiser(Client esClient) {
        this.esClient = esClient;
    }

    public void initialise() {
        IndicesAdminClient indices = this.esClient.admin().indices();
        DeleteIndexRequestBuilder deleteBuilder = new DeleteIndexRequestBuilder(indices, "chats");
        indices.delete(deleteBuilder.request());
        logger.info("Indexes removed.");

        CreateIndexRequest createBuilder = new CreateIndexRequest("chats");
        try {
            XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
                .startObject()
                    .startObject("chat")
                        .startObject("properties")
                            .startObject("date")
                                .field("type", "long")
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject();
            createBuilder.mapping("chat", mappingBuilder);
        } catch (IOException e) {
            e.printStackTrace();
        }

        logger.info("Created index!");
        indices.create(createBuilder);
    }
}

@@ -100,52 +100,8 @@
 er;%0A
-import org.apache.logging.log4j.LogManager;%0A
 impo
@@ -883,103 +883,151 @@
     
-DeleteIndexRequestBuilder deleteBuilder = new DeleteIndexRequestBuilder(indices, %22chats
+removeIndices(indices);%0A        logger.info(%22Indexes removed.%22);%0A%0A        createIndices(indices);%0A        logger.info(%22Created indices!
 %22);%0A    
     
@@ -1026,94 +1026,215 @@
     
+%7D%0A%0A
     
-indices.delete(deleteBuilder.request()
+private void createIndices(IndicesAdminClient indices) %7B%0A        createChatsIndex(indices);%0A        createChannelsIndex(indices
 );%0A    
+%7D%0A%0A
     
-logger.info(%22Indexes removed.%22);%0A
+private void createChatsIndex(IndicesAdminClient indices) %7B
 %0A   
@@ -1320,16 +1320,46 @@
   try %7B%0A
+            // @formatter:off%0A
         
@@ -1848,24 +1848,53 @@
 ngBuilder);%0A
+            // @formatter:on%0A
         %7D ca
@@ -1962,48 +1962,1431 @@
   %7D%0A
+        indices.create(createBuilder);
 %0A    
+%7D%0A%0A
     
-logger.info(%22Created index!%22
+private void createChannelsIndex(IndicesAdminClient indices) %7B%0A        CreateIndexRequest createBuilder = new CreateIndexRequest(%22channels%22);%0A        try %7B%0A            // @formatter:off%0A            XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()%0A                    .startObject()%0A                        .startObject(%22channel%22)%0A                            .startObject(%22properties%22)%0A                                .startObject(%22topic%22)%0A                                    .field(%22type%22, %22object%22) // we only have one so don't use type 'nested'%0A                                .endObject()%0A                                .startObject(%22topic.time%22)%0A                                    .field(%22type%22, %22long%22)%0A                                .endObject()%0A                                .startObject(%22last_activity%22)%0A                                    .field(%22type%22, %22long%22)%0A                                .endObject()%0A                                .startObject(%22last_valid_content_at%22)%0A                    .field(%22type%22, %22long%22)%0A                                .endObject()%0A                            .endObject()%0A                        .endObject()%0A                    .endObject();%0A            createBuilder.mapping(%22channel%22, mappingBuilder);%0A            // @formatter:on%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace(
 );%0A
+        %7D%0A
     
@@ -3425,11 +3425,227 @@
 );%0A    %7D
+%0A%0A    private void removeIndices(IndicesAdminClient indices) %7B%0A        DeleteIndexRequestBuilder deleteBuilder = new DeleteIndexRequestBuilder(indices, %22chats%22);%0A        indices.delete(deleteBuilder.request());%0A    %7D
 %0A%7D%0A

Prepare for indexing of channel related information.