package fuzion24.device.vulnerability.test.adapter;

import android.content.Context;
import android.framework.libcore.io.Streams;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nowsecure.android.vts.R;

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

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

import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.test.adapter.viewholder.RecyclerItemViewHolder;
import fuzion24.device.vulnerability.vulnerabilities.helper.BinaryAssets;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VulnerabilityTestResult> mResults;
    private Context mContext;
    private JSONObject mVulnMap;

    public RecyclerAdapter(Context context, List<VulnerabilityTestResult> itemList) {
        this.mContext = context;
        this.mResults = itemList;

        try {
            String jsonVulns = BinaryAssets.extractAsset(context, "vuln_map.json");
            mVulnMap = new JSONObject(jsonVulns);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.vulnerability_layout, parent, false);

        return RecyclerItemViewHolder.newInstance(view);
    }

    private 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;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder;
        VulnerabilityTestResult item = mResults.get(position);

        holder.setItemTestName(item.getCVEorID());

        try{
            JSONObject jobj = mVulnMap.getJSONObject(item.getCVEorID());
            String cve = jobj.getString("cve");
            List<String> altNames = extractStringArray(jobj, "alt_names");
            String description = jobj.getString("description");
            String impact = jobj.getString("impact");
            List<String> externalLinks =  extractStringArray(jobj, "external_links");
            Double cvssV2Score = jobj.getDouble("cvssv2");
            List<String> patches = extractStringArray(jobj, "patch");
            String cveDate = jobj.getString("cvedate");

            

        }catch(Exception e){
            //We dont have an entry or are missing
            e.printStackTrace();
        }

        if (item.getException() != null) {
            holder.setItemTestResult(mContext.getString(R.string.error_test, item.getException().getMessage()));
        } else {

            if (item.getResult()) {
                holder.setItemTestResult(mContext.getString(R.string.test_result_failure));
                holder.setItemTestResultColor(Color.RED);
            } else {
                holder.setItemTestResultColor(Color.GREEN);
                holder.setItemTestResult(mContext.getString(R.string.test_result_success));
            }

        }
    }

    @Override
    public int getItemCount() {
        return mResults == null ? 0 : mResults.size();
    }

}

@@ -2427,17 +2427,16 @@
 bj, %22alt
-_
 names%22);
@@ -2432,24 +2432,24 @@
 altnames%22);%0A
+
             
@@ -2830,22 +2830,8 @@
 );%0A%0A
-            %0A%0A
     
@@ -2905,16 +2905,43 @@
  missing
+ necessary components of it
 %0A       
@@ -3150,17 +3150,16 @@
  else %7B%0A
-%0A
         
@@ -3505,24 +3505,24 @@
 _success));%0A
+
             
@@ -3515,33 +3515,32 @@
 ;%0A            %7D%0A
-%0A
         %7D%0A    %7D%0A

Fix reference to our json key