package fuzion24.device.vulnerability.test;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;
import fuzion24.device.vulnerability.vulnerabilities.helper.BinaryAssets;

/**
 * Created by fuzion24 on 11/23/15.
 */
public class VulnerabilityDescriptor {
    String CVEorID;
    String description;
    String impact;
    Double CVSSV2Score;
    String CVEDate;
    List<String> externalLinks;
    List<String> altNames;
    List<String> patches;

    private VulnerabilityDescriptor(String cve,
                                    String desc,
                                    String impact,
                                    Double cvssv2,
                                    String cveDate,
                                    List<String> externLinks,
                                    List<String> altNames,
                                    List<String> patches) {
        this.CVEorID = cve;
        this.description = desc;
        this.impact = impact;
        this.CVSSV2Score = cvssv2;
        this.CVEDate = cveDate;
        this.externalLinks = externLinks;
        this.altNames = altNames;
        this.patches = patches;
    }


    private static List<String> extractStringArray(JSONObject obj, String arrayName) throws Exception {
        JSONArray jsonStringArray = obj.getJSONArray(arrayName);
        List<String> items = new ArrayList<String>();
        for (int i = 0; i < jsonStringArray.length(); i++) {
            items.add(jsonStringArray.getString(i));
        }
        return items;
    }


    public static Map<String, VulnerabilityDescriptor> getParsedVulnMap(Context ctx) throws Exception {
        String jsonVulns = BinaryAssets.extractAsset(ctx, "vuln_map.json");
        JSONObject vulnMap = new JSONObject(jsonVulns);

        Map<String, VulnerabilityDescriptor> descriptorMap = new HashMap<String, VulnerabilityDescriptor>();
        Iterator<String> keys = vulnMap.keys();




        while (keys.hasNext()) {
            JSONObject jobj = null;
            String description = null;
            String impact = null;
            Double cvssV2Score = null;
            String cveDate = null;
            List<String> externalLinks = null;
            List<String> altNames = null;
            List<String> patches = null;

            String key = keys.next();
            jobj = vulnMap.getJSONObject(key);
            String cve = jobj.getString("cve");
            altNames = extractStringArray(jobj, "altnames");
            description = jobj.getString("description");
            impact = jobj.getString("impact");
            externalLinks = extractStringArray(jobj, "external_links");
            cvssV2Score = jobj.getDouble("cvssv2");
            patches = extractStringArray(jobj, "patch");
            cveDate = jobj.getString("cvedate");

            VulnerabilityDescriptor vd = new VulnerabilityDescriptor(
                    cve,
                    description,
                    impact,
                    cvssV2Score,
                    cveDate,
                    externalLinks,
                    altNames,
                    patches);

            descriptorMap.put(cve, vd);
        }

        return descriptorMap;
    }
}

@@ -484,20 +484,36 @@
 iptor %7B%0A
+%0A%0A
     
+private final 
 String C
@@ -520,24 +520,38 @@
 VEorID;%0A    
+private final 
 String descr
@@ -558,24 +558,38 @@
 iption;%0A    
+private final 
 String impac
@@ -591,24 +591,38 @@
 impact;%0A    
+private final 
 Double CVSSV
@@ -629,24 +629,38 @@
 2Score;%0A    
+private final 
 String CVEDa
@@ -663,24 +663,38 @@
 VEDate;%0A    
+private final 
 List%3CString%3E
@@ -709,24 +709,38 @@
 lLinks;%0A    
+private final 
 List%3CString%3E
@@ -750,24 +750,38 @@
 tNames;%0A    
+private final 
 List%3CString%3E
@@ -3549,10 +3549,570 @@
 ;%0A    %7D%0A
+%0A%0A    public String getCVEorID() %7B%0A        return CVEorID;%0A    %7D%0A%0A    public String getDescription() %7B%0A        return description;%0A    %7D%0A%0A    public String getImpact() %7B%0A        return impact;%0A    %7D%0A%0A    public Double getCVSSV2Score() %7B%0A        return CVSSV2Score;%0A    %7D%0A%0A    public String getCVEDate() %7B%0A        return CVEDate;%0A    %7D%0A%0A    public List%3CString%3E getExternalLinks() %7B%0A        return externalLinks;%0A    %7D%0A%0A    public List%3CString%3E getAltNames() %7B%0A        return altNames;%0A    %7D%0A%0A    public List%3CString%3E getPatches() %7B%0A        return patches;%0A    %7D%0A%0A
 %7D%0A

Add getters