/**
 * Copyright (c) 2016-2017, Mihai Emil Andronache
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  1)Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  2)Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *  3)Neither the name of charles-rest nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.amihaiemil.charles.github;

import java.io.IOException;
import java.util.Arrays;

import org.slf4j.Logger;

import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.LiveWebPage;
import com.amihaiemil.charles.SnapshotWebPage;
import com.amihaiemil.charles.WebPage;
import com.amihaiemil.charles.aws.AmazonEsRepository;

/**
 * Step to index a single page.
 * @author Mihai Andronache (amihaiemil@gmail.com)
 * @version $Id$
 * @since 1.0.0
 *
 */
public class IndexPage extends IndexStep {

    /**
     * Action's logger.
     */
    private Logger logger;

    /**
     * Command.
     */
    private Command com;

    /**
     * Ctor.
     * @param com Given command.
     * @param logger Logger.
     * @param next Next step to take.
     */
    public IndexPage(Command com, Logger logger, Step next) {
        super(next);
        this.com = com;
        this.logger = logger;
    }

    @Override
    public void perform() {
	    String link = this.getLink();
	    logger.info("Indexing page " + link + " ...");
    	try {
    	    logger.info("Crawling the page...");
    	    this.phantomJsDriver().get(link);
            WebPage snapshot = new SnapshotWebPage(
                new LiveWebPage(this.phantomJsDriver())
            );
    	    logger.info("Page crawled. Sending to aws...");
            new AmazonEsRepository(this.com.indexName()).export(Arrays.asList(snapshot));
    	    logger.info("Page successfully sent to aws!");
        } catch (DataExportException | IOException | RuntimeException e) {
            logger.error("Exception while indexing the page " + link, e);
            throw new IllegalStateException("Exception while indexing the page" + link, e);
        }
        this.next().perform();
    }

    /**
     * Get the page's link from the command's text which should be in markdown format, with a
     * link like [this](http://link.com/here/the/ling) .
     * @return String link.
     */
    private String getLink() {
    	String body = this.com.json().getString("body");
        return body.substring(body.indexOf('('),  body.indexOf(')'));
    }

}

@@ -1649,16 +1649,54 @@
 rrays;%0A%0A
+import org.openqa.selenium.WebDriver;%0A
 import o
@@ -2560,17 +2560,21 @@
 orm() %7B%0A
-%09
+     
     Stri
@@ -2599,17 +2599,21 @@
 Link();%0A
-%09
+     
     logg
@@ -2659,17 +2659,21 @@
 %22);%0A    
+  
-%09
+   
 try %7B%0A  
@@ -2670,25 +2670,29 @@
   try %7B%0A    
-%09
+     
     logger.i
@@ -2724,20 +2724,43 @@
 %22);%0A    
+   
-%09
    
+   WebDriver driver =
  this.ph
@@ -2774,16 +2774,37 @@
 Driver()
+;%0A             driver
 .get(lin
@@ -2862,25 +2862,8 @@
 age(
-%0A                
 new 
@@ -2878,31 +2878,17 @@
 age(
-this.phantomJsD
+d
 river
-(
 ))
+;
 %0A   
@@ -2896,27 +2896,16 @@
         
-);%0A    %09   
  logger.
@@ -3009,16 +3009,33 @@
 .export(
+%0A                
 Arrays.a
@@ -3053,16 +3053,33 @@
 hot)
+%0A            
 );%0A    
-%09
+     
     
@@ -3142,16 +3142,35 @@
  catch (
+%0A            final 
 DataExpo
@@ -3215,16 +3215,25 @@
 eption e
+%0A        
 ) %7B%0A    
@@ -3346,16 +3346,33 @@
 ception(
+%0A                
 %22Excepti
@@ -3400,32 +3400,45 @@
  page%22 + link, e
+%0A            
 );%0A        %7D%0A   
@@ -3706,9 +3706,13 @@
     
-%09
+     
 Stri
@@ -3803,16 +3803,20 @@
 xOf('(')
+ + 1
 ,  body.

fix index page