/*
 * Copyright 2018 Netflix, 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.
 */

package com.netflix.kayenta.judge;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.kayenta.canary.CanaryClassifierThresholdsConfig;
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.CanaryJudge;
import com.netflix.kayenta.canary.results.CanaryJudgeResult;
import com.netflix.kayenta.judge.config.RemoteJudgeConfigurationProperties;
import com.netflix.kayenta.judge.model.RemoteJudgeRequest;
import com.netflix.kayenta.judge.service.RemoteJudgeService;
import com.netflix.kayenta.metrics.MetricSetPair;
import com.netflix.kayenta.retrofit.config.RemoteService;
import com.netflix.kayenta.retrofit.config.RetrofitClientFactory;
import com.squareup.okhttp.OkHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import retrofit.converter.JacksonConverter;

import java.util.List;

@Component
@ConditionalOnProperty("kayenta.remoteJudge.enabled")
@Slf4j
public class RemoteJudge extends CanaryJudge {

  private final RetrofitClientFactory retrofitClientFactory;
  private final ObjectMapper kayentaObjectMapper;
  private final OkHttpClient httpClient;
  private final RemoteService endpoint;

  private final String JUDGE_NAME = "RemoteJudge-v1.0";

  public RemoteJudge(RetrofitClientFactory retrofitClientFactory,
                     ObjectMapper kayentaObjectMapper,
                     OkHttpClient httpClient,
                     RemoteJudgeConfigurationProperties config) {
    this.retrofitClientFactory = retrofitClientFactory;
    this.kayentaObjectMapper = kayentaObjectMapper;
    this.httpClient = httpClient;
    this.endpoint = config.getEndpoint();

    log.info("Configured " + JUDGE_NAME + " with base URI " + endpoint.getBaseUrl());
  }

  @Override
  public String getName() {
    return JUDGE_NAME;
  }

  @Override
  public boolean isVisible() {
    return false;
  }

  @Override
  public CanaryJudgeResult judge(CanaryConfig canaryConfig,
                                 CanaryClassifierThresholdsConfig scoreThresholds,
                                 List<MetricSetPair> metricSetPairList) {

    RemoteJudgeService remoteJudge = retrofitClientFactory.createClient(
      RemoteJudgeService.class,
      new JacksonConverter(kayentaObjectMapper),
      endpoint,
      httpClient);

    RemoteJudgeRequest judgeRequest = RemoteJudgeRequest.builder()
      .canaryConfig(canaryConfig)
      .metricSetPairList(metricSetPairList)
      .scoreThresholds(scoreThresholds)
      .build();

    return remoteJudge.judge(judgeRequest);
  }
}

@@ -1540,16 +1540,54 @@
 il.List;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A@Compo
@@ -1815,49 +1815,8 @@
 er;%0A
-  private final OkHttpClient httpClient;%0A
   pr
@@ -2034,54 +2034,8 @@
 er,%0A
-                     OkHttpClient httpClient,%0A
     
@@ -2208,42 +2208,8 @@
 er;%0A
-    this.httpClient = httpClient;%0A
     
@@ -2702,16 +2702,182 @@
 ist) %7B%0A%0A
+    OkHttpClient okHttpClient = new OkHttpClient();%0A    okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);%0A    okHttpClient.setReadTimeout(90, TimeUnit.SECONDS);%0A%0A
     Remo
@@ -3044,17 +3044,19 @@
 ,%0A      
-h
+okH
 ttpClien

extend connect and read timeouts (#497)