package com.oneops.search.msg.processor;

import com.google.gson.JsonParser;
import com.oneops.cms.dj.domain.CmsRelease;
import com.oneops.search.msg.index.Indexer;
import org.apache.http.client.fluent.Request;
import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.GregorianCalendar;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.disMaxQuery;

/*******************************************************************************
 *
 *   Copyright 2016 Walmart, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *******************************************************************************/

@Service
public class ReleaseMessageProcessor implements MessageProcessor {
    private static final String SNAPSHOT = "snapshot";
    private static Logger logger = Logger.getLogger(ReleaseMessageProcessor.class);
    private final Indexer indexer;
    private final Client client;
    @Value("${designSnapshotURL}")
    private String designSnapshotURL;
    @Value("${manifestSnapshotURL}")
    private String manifestSnapshotURL;
    private static final String RELEASE = "release";

    @Autowired
    public ReleaseMessageProcessor(Indexer indexer, Client client) {
        this.indexer = indexer;
        this.client = client;
    }

    @Override
    public void processMessage(String message, String msgType, String msgId) {
        CmsRelease release = GSON.fromJson(message, CmsRelease.class);
        String releaseMsg = GSON_ES.toJson(release);
        indexer.indexEvent(RELEASE, releaseMsg);
        indexer.index(String.valueOf(release.getReleaseId()), RELEASE, releaseMsg);
        indexSnapshot(release);
    }

    private void indexSnapshot(CmsRelease release) {
      
        
        try {
            if (!"closed".equalsIgnoreCase(release.getReleaseState())) {
                logger.info("Release is not closed. Won't do snapshot");
                return;
            }
            if (release.getNsPath().endsWith("bom")) return; // ignore bom release


            Calendar cal = new GregorianCalendar();
            cal.add(Calendar.WEEK_OF_YEAR, -2);
            long snapshotTimestamp = cal.getTime().getTime();
            long hits = client.prepareSearch("cms-201*")
                    .setSearchType(SearchType.COUNT)
                    .setTypes(SNAPSHOT)
                    .setQuery( boolQuery()
                            .must(QueryBuilders.rangeQuery("timestamp").from(snapshotTimestamp))
                            .must(QueryBuilders.termQuery("namespace.keyword", release.getNsPath()))
                    )
                    .execute().actionGet().getHits().getTotalHits();
            logger.info("hits:"+ hits);
            if (hits>0) {
                logger.info("there was a snapshot done recently, so won'd do another one. ");
                return;
            }
            
            String url = (release.getNsPath().endsWith("manifest") ? manifestSnapshotURL : designSnapshotURL) + release.getNsPath();  // set snapshot URL based on release type
            logger.info("Retrieving snapshot for:" + url + " expected release:" + release.getReleaseId());
            String message = Request.Get(url).addHeader("Content-Type", "application/json").execute().returnContent().asString();
            long releaseId = new JsonParser().parse(message).getAsJsonObject().get("release").getAsLong();
            if (releaseId > release.getReleaseId()) {
                logger.warn("Snapshot is dirty, so discarding. Was expecting release:" + release.getReleaseId() + " snapshot has rfcs from release:" + releaseId);
            } else {
                indexer.index(String.valueOf(release.getReleaseId()), SNAPSHOT, message);
                logger.info("Snapshot indexed for release ID: " + release.getReleaseId());
            }

        } catch (Exception e) {
            logger.error("Error while retrieving snapshot" + e.getMessage(), e);
        }
        
    }
}

@@ -285,155 +285,52 @@
 arch
-Response;%0Aimport org.elasticsearch.action.search.SearchType;%0Aimport org.elasticsearch.client.Client;%0Aimport org.elasticsearch.common.unit.TimeValue
+Type;%0Aimport org.elasticsearch.client.Client
 ;%0Aim
@@ -685,79 +685,8 @@
 ery;
-%0Aimport static org.elasticsearch.index.query.QueryBuilders.disMaxQuery;
 %0A%0A/*
@@ -2545,31 +2545,17 @@
 ease) %7B%0A
-      %0A        
+%0A
 %0A       
@@ -3172,17 +3172,16 @@
 etQuery(
- 
 boolQuer
@@ -3504,16 +3504,17 @@
 (%22hits:%22
+ 
 + hits);
@@ -3534,17 +3534,19 @@
 if (hits
-%3E
+ %3E 
 0) %7B%0A   
@@ -3674,28 +3674,16 @@
       %7D%0A
-            
 %0A       
@@ -4752,24 +4752,16 @@
       %7D%0A
-        
 %0A    %7D%0A%7D

fix for