/*
 * Copyright 2016 Sascha Häberling
 *
 * 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.s13g.truecar;

import com.s13g.truecar.CarRequestConfig.Maker;
import com.s13g.truecar.CarRequestConfig.Model;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Main {
  private static final String START_TOKEN = "window.__INITIAL_STATE__=";
  private static final String END_TOKEN = ";window.__GS_ENV_VARS__";

  private static List<CarRequestConfig> createWatches() {
    List<CarRequestConfig> watches = new ArrayList<>();
    watches.add(new CarRequestConfig(Maker.VW, Model.GOLF_R, 94122, 250));
    return watches;
  }

  public static void main(String[] args) {
    List<CarRequestConfig> watches = createWatches();
    HttpUtil httpUtil = new HttpUtil();

    for (CarRequestConfig watch : watches) {
      String carRequestUrl = watch.buildRequestUrl();

      System.out.println("Loading URL: " + carRequestUrl);
      Optional<String> optHtml = httpUtil.loadUrl(carRequestUrl);
      if (!optHtml.isPresent()) {
        System.err.println("Cannot load URL " + carRequestUrl);
        return;
      }

      String html = optHtml.get();
      System.out.println("Parsing loaded HTML: " + html.length());
      int start = html.indexOf(START_TOKEN);
      if (start < 0) {
        System.err.println("Cannot find start token");
        return;
      }

      int end = html.indexOf(END_TOKEN, start);
      if (end < 0) {
        System.err.println("Cannot find end token");
        return;
      }

      String json = html.substring(start + START_TOKEN.length(), end);
      System.out.println(json);

      JSONObject carJson = new JSONObject(json);
      System.out.println("Vehice Count: " + carJson.getInt("vehicleCount"));

      // TODO: Continue here.
    }
  }
}

@@ -1706,39 +1706,420 @@
     
-String html = optHtml.get();%0A  
+Optional%3CJSONObject%3E optCarJson = parseJson(optHtml.get());%0A      if (!optCarJson.isPresent()) %7B%0A        return;%0A      %7D%0A      JSONObject carJson = optCarJson.get();%0A%0A      if (!checkApiStatus(carJson)) %7B%0A        return;%0A      %7D%0A%0A      System.out.println(%22Vehice Count: %22 + carJson.getInt(%22vehicleCount%22));%0A%0A      // TODO: Continue here.%0A    %7D%0A  %7D%0A%0A  private static Optional%3CJSONObject%3E parseJson(String html) %7B%0A
     
@@ -2175,26 +2175,24 @@
 .length());%0A
-  
     int star
@@ -2222,26 +2222,24 @@
 TOKEN);%0A    
-  
 if (start %3C 
@@ -2241,34 +2241,32 @@
 rt %3C 0) %7B%0A      
- 
- 
 System.err.print
@@ -2302,37 +2302,48 @@
 ;%0A      
-  
 return
+ Optional.empty()
 ;%0A
-  
     %7D%0A%0A
-  
     int 
@@ -2380,18 +2380,16 @@
 start);%0A
-  
     if (
@@ -2397,34 +2397,32 @@
 nd %3C 0) %7B%0A      
-  
 System.err.print
@@ -2456,37 +2456,47 @@
 ;%0A      
-  
 return
+ Optional.empty()
 ;%0A
-  
     %7D%0A
-%0A  
     Stri
@@ -2564,85 +2564,208 @@
     
-  System.out.println
+return Optional.of(new JSONObject
 (json)
+)
 ;%0A
-%0A
   
-    JSONObject carJson = new JSONObject(json);
+%7D%0A%0A  private static boolean checkApiStatus(JSONObject carJson) %7B%0A    JSONObject apiStatus = carJson.getJSONObject(%22apiStatus%22);%0A    if (apiStatus == null) %7B
 %0A   
@@ -2766,35 +2766,35 @@
  %7B%0A      System.
-out
+err
 .println(%22Vehice
@@ -2791,95 +2791,246 @@
 ln(%22
-Vehice Count: %22 + carJson.getInt(%22vehicleCount%22));%0A%0A      // TODO: Continue here.%0A    %7D
+Cannot find apiStatus%22);%0A      return false;%0A    %7D%0A    int apiStatusCode = apiStatus.getInt(%22code%22);%0A    if (apiStatusCode != 200) %7B%0A      System.err.println(%22API Status Code: %22 + apiStatusCode);%0A      return false;%0A    %7D%0A    return true;
 %0A  %7D

Split phases into methods and add apiStatus code check.