package org.popkit.leap.elpa.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.popkit.leap.elpa.entity.RecipeDo;

import java.io.File;

/**
 * Created by Aborn Jiang
 * Mail aborn.jiang@gmail.com
 * 2016-05-16:12:02
 */
public class RecipeParser {

    public static RecipeDo parsePkgRecipe(String pkgName) {
        String recipe = PelpaUtils.getRecipeFilePath() + pkgName;
        File recipeFile = new File(recipe);
        if (recipeFile.exists() && recipeFile.isFile()) {
            try {
                String content = FileUtils.readFileToString(recipeFile, "UTF-8");
                return parse(content);
            } catch (Exception e) {

            }
        }
        return null;
    }

    public static RecipeDo parse(String origin) {
        String sub = null;
        try {
            sub = origin.substring(origin.indexOf('(') + 1, origin.lastIndexOf(')'));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sub == null) {
            return null;
        }

        String[] suArr = sub.split("\\s+");
        RecipeDo recipeDo = new RecipeDo();
        recipeDo.setPkgName(suArr[0].trim());
        String[] keyValuePair = sub.substring(sub.indexOf(suArr[0]) + suArr[0].length()).trim().split("\\s+");

        for (int i=0; i<keyValuePair.length; i++) {
            try {
                String current = keyValuePair[i].trim();
                if (StringUtils.isNotBlank(current) && current.startsWith(":")
                        && (i+1) < keyValuePair.length) {
                    String key = current.substring(1);
                    String value = keyValuePair[i+1].trim();
                    if ("files".equalsIgnoreCase(key)) {
                        recipeDo.update(key, fileValue(value));
                    } else if ("repo".endsWith(key) || "url".equals(key)) {
                        recipeDo.update(key, trimIt(value));
                    } else {
                        recipeDo.update(key, value);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return recipeDo;
    }

    private static String trimIt(String orgin) {
        return orgin.replaceAll("\"", "").trim();
    }

    private static String fileValue(String origin) {
        return origin.replaceAll("\"","").replaceAll("\\(", "").replaceAll("\\)", "").trim();
    }
}

@@ -1197,24 +1197,25 @@
 0%5D.trim());%0A
+%0A
         Stri
@@ -1231,14 +1231,21 @@
 alue
+String
 Pair =
+ 
  sub
@@ -1316,17 +1316,13 @@
 it(%22
-%5C%5Cs+
+:
 %22);%0A
-%0A
     
@@ -1334,120 +1334,133 @@
 or (
-int i=0; i%3CkeyValuePair.length; i++) %7B%0A            try %7B%0A                String current = keyValuePair%5Bi%5D.trim()
+String keyValueString : keyValueStringPair) %7B%0A            if (StringUtils.isBlank(keyValueString)) %7B%0A                continue
 ;%0A  
@@ -1473,110 +1473,93 @@
     
+%7D%0A%0A
     
-if (StringUtils.isNotBlank(current) && current.startsW
+        String%5B%5D keyValuePair = keyValueString.spl
 it
-h
 (%22
-:%22)%0A                        && (i+1) %3C 
+%5C%5Cs+%22);%0A            if (
 keyV
@@ -1573,16 +1573,21 @@
 r.length
+ %3C= 1
 ) %7B%0A    
@@ -1602,45 +1602,16 @@
     
-    String key = current.substring(1)
+continue
 ;%0A  
@@ -1608,32 +1608,38 @@
 ntinue;%0A        
+    %7D%0A
             Stri
@@ -1641,21 +1641,19 @@
  String 
-value
+key
  = keyVa
@@ -1664,11 +1664,9 @@
 air%5B
-i+1
+0
 %5D.tr
@@ -1687,124 +1687,117 @@
     
-  
-      if (%22files%22.equalsIgnoreCase(key)) %7B%0A                        recipeDo.update(key, fileValue(value));%0A       
+String value = keyValueString.substring(keyValueString.indexOf(keyValuePair%5B0%5D) + keyValuePair%5B0%5D.length());%0A
     
@@ -1804,24 +1804,16 @@
         
- %7D else 
 if (%22rep
@@ -1820,15 +1820,13 @@
 o%22.e
-ndsWith
+quals
 (key
@@ -1863,32 +1863,24 @@
             
-  
-      
 recipeDo.upd
@@ -1920,33 +1920,44 @@
     
-        %7D else %7B%0A        
+%7D else if (%22files%22.endsWith(key)) %7B%0A
     
@@ -1989,22 +1989,33 @@
 te(key, 
+fileValue(
 value)
+)
 ;%0A      
@@ -2024,133 +2024,67 @@
     
-        %7D%0A                %7D%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A                continue
+%7D else %7B%0A                recipeDo.update(key, value.trim())
 ;%0A  

fix recipeparser files