/*
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * 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.stratio.qa.aspects;

import cucumber.api.java.Before;
import cucumber.runtime.io.Resource;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Aspect
public class LoopTagAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getCanonicalName());


    @Pointcut("execution (private * cucumber.runtime.FeatureBuilder.read(..)) &&" + "args (resource)")
    protected void addLoopTagPointcutScenario(Resource resource) {
    }

    /**
     * @param resource
     * @throws Throwable
     */
    @Around(value = "addLoopTagPointcutScenario(resource)")
    public String aroundAddLoopTagPointcutScenario(Resource resource) throws Throwable {
        List<String> lines = Files.readAllLines(Paths.get(resource.getPath()), StandardCharsets.UTF_8);
        String listParams;
        String paramReplace;

        for (int s = 0; s < lines.size(); s++) {
            String[] elems;
            if (lines.get(s).contains("@loop")) {
                listParams = lines.get(s).substring((lines.get(s).lastIndexOf("(") + 1), (lines.get(s).length()) - 1).split(",")[0];
                elems = System.getProperty(listParams).split(",");
                paramReplace = lines.get(s).substring((lines.get(s).lastIndexOf("(") + 1), (lines.get(s).length()) - 1).split(",")[1];
                while (!(lines.get(s).toUpperCase().contains("SCENARIO:"))) {
                    s ++;
                }
                lines.set(s,lines.get(s).replaceAll("Scenario", "Scenario Outline"));
                s++;
                while ( s < lines.size()) {
                    if ((lines.get(s).toUpperCase().contains("SCENARIO")) || (lines.get(s).contains("@"))) {
                        break;
                    }
                    s++;
                }
                lines.add(s, "Examples:");
                exampleLines(paramReplace, elems, lines,  s +1);
                s = s + elems.length;
            }
        }
        return String.join("\n", lines);
    }

    public void exampleLines (String name, String[] params, List<String> lines, int num) {
        lines.add(num, "| " + name + " | " + name + ".id |");
        for (int i = 0; i < params.length; i++) {
            num++;
            lines.add(num, "| " + params[i] + " | " + i + " |");
        }
    }
}

@@ -650,41 +650,8 @@
 s;%0A%0A
-import cucumber.api.java.Before;%0A
 impo
@@ -1940,16 +1940,42 @@
 ,%22)%5B0%5D;%0A
+                try %7B%0A    
         
@@ -2029,24 +2029,288 @@
 split(%22,%22);%0A
+                %7D catch (Exception e) %7B%0A                    logger.error(%22-%3E Error while parsing params. %7B%7D is not defined.%22, listParams);%0A                    throw new Exception(%22-%3E Error while parsing params. %7B%7D is not defined.%22 + listParams);%0A                %7D%0A
             
@@ -2432,16 +2432,51 @@
 ,%22)%5B1%5D;%0A
+                lines.set(s, %22 %22);%0A
         
@@ -2817,17 +2817,16 @@
 O%22)) %7C%7C 
-(
 lines.ge
@@ -2834,21 +2834,29 @@
 (s).
-contains(%22@%22)
+matches(%22.*@%5B%5E%5C%5C%7B%5D.*%22
 )) %7B

Fix failure when replace @{} in loop scenario (#202)