package fuzion24.device.vulnerability.test.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
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.Iterator;
import java.util.List;
import java.util.Map;

import fuzion24.device.vulnerability.test.VulnerabilityDescriptor;
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> {

    final private List<Pair<VulnerabilityTestResult, VulnerabilityDescriptor>> mResults;
    private Context mContext;

    public RecyclerAdapter(Context context, List<VulnerabilityTestResult> itemList) {
        this.mContext = context;
        Map<String, VulnerabilityDescriptor> vMap = VulnerabilityDescriptor.getParsedVulnMap(context);

        List<Pair<VulnerabilityTestResult, VulnerabilityDescriptor>> res =
                        new ArrayList<Pair<VulnerabilityTestResult, VulnerabilityDescriptor>>();

        for(VulnerabilityTestResult vtr : itemList){
            VulnerabilityDescriptor vd = vMap.get(vtr.getCVEorID());
            res.add(Pair.create(vtr, vd));
        }

        this.mResults = res;

    }

    @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 class VulnObject {

    }

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

        VulnerabilityTestResult vulnRes = item.first;
        VulnerabilityDescriptor vulnDesc = item.second;

        holder.setItemTestName(vulnRes.getCVEorID());


        if (vulnRes.getException() != null) {
            holder.setItemTestResult(mContext.getString(R.string.error_test, item.getException().getMessage()));
            holder.setItemTestResultColor(mContext.getResources().getColor(R.color.orange));
        } else {
            if (vulnRes.getResult()) {
                holder.setItemTestResultColor(mContext.getResources().getColor(R.color.red));
                holder.setItemTestResult(mContext.getString(R.string.test_result_failure));
            } else {
                holder.setItemTestResultColor(mContext.getResources().getColor(R.color.green));
                holder.setItemTestResult(mContext.getString(R.string.test_result_success));
            }
        }

        if (!TextUtils.isEmpty(description)) {
            holder.setItemTestResultDescription(description);
            holder.setButtonShowDetailsClickListner(mContext,
                    item.getCVEorID(),
                    altNames,
                    description,
                    impact,
                    externalLinks,
                    patches,
                    cvssV2Score,
                    cveDate);
        } else {
            holder.setItemTestResultDescription(mContext.getString(R.string.information_not_available));

            if (jobj != null) {
                holder.setButtonShowDetailsClickListner(mContext,
                        item.getCVEorID(),
                        altNames,
                        description,
                        impact,
                        externalLinks,
                        patches,
                        cvssV2Score,
                        cveDate);
            } else {
                holder.setButtonShowDetailsClickListner(mContext, item.getCVEorID());
            }
        }

    }

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

}

@@ -1931,48 +1931,12 @@
   %7D%0A
-%0A%0A%0A
     
-private class VulnObject %7B%0A%0A    %7D
 %0A%0A  
@@ -3103,59 +3103,8 @@
  %7D%0A%0A
-        if (!TextUtils.isEmpty(description)) %7B%0A    
     
@@ -3143,17 +3143,29 @@
 ription(
-d
+vulnDesc.getD
 escripti
@@ -3166,23 +3166,21 @@
 cription
+()
 );%0A
-    
         
@@ -3241,32 +3241,31 @@
             
-    
-item
+vulnRes
 .getCVEorID(
@@ -3287,602 +3287,142 @@
     
-    altNames,%0A                    description,%0A                    impact,%0A                    externalLinks,%0A                    patches,%0A                    cvssV2Score,%0A                    cveDate);%0A        %7D else %7B%0A            holder.setItemTestResultDescription(mContext.getString(R.string.information_not_available));%0A%0A            if (jobj != null) %7B%0A                holder.setButtonShowDetailsClickListner(mContext,%0A                        item.getCVEorID(),%0A                        altNames,%0A                        description,%0A                        impact,%0A                        e
+vulnDesc.getAltNames(),%0A                vulnDesc.getDescription(),%0A                vulnDesc.getImpact(),%0A                vulnDesc.getE
 xter
@@ -3429,16 +3429,18 @@
 nalLinks
+()
 ,%0A      
@@ -3453,23 +3453,29 @@
     
-        p
+vulnDesc.getP
 atches
+()
 ,%0A  
@@ -3492,27 +3492,33 @@
     
-        cvss
+vulnDesc.getCVSS
 V2Score
+()
 ,%0A  
@@ -3535,157 +3535,31 @@
     
-        cveDate);%0A            %7D else %7B%0A                holder.setButtonShowDetailsClickListner(mContext, item.getCVEorID());%0A            %7D%0A        %7D%0A
+vulnDesc.getCVEDate());
 %0A   

Make the description a requirement