package mindpop.learnpop;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.content.Context;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by montselozanod on 3/27/15.
 */
public class LoadResource extends AsyncTask<String, String, String> {

    private Context _context;
    private int displayType; // 0 for articles and 1 for videos
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    private ArrayList<Resource> resourcesArrayList;
    private static String urlResources = "http://austinartmap.com/CreativeTeach/PHP/getResourcesList.php";
    private String [] subjects;
    private String grade;
    private JSONArray resources;
    private final String TAG_SUCCESS = "success";

    public LoadResource(Context context, String [] subjects, String grade, int type){
        this._context = context;
        this.subjects = subjects;
        this.grade = grade;
        this.displayType = type;
    }
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(_context);
        pDialog.setMessage("Loading resources...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args){

        //parameters
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("GradeLevel[]", grade));
       

        for(int i = 0; i < subjects.length; i++){
            params.add(new BasicNameValuePair("Subject[]", subjects[i]));
        }
        JSONObject json = jsonParser.makeHttpRequest(urlResources, "GET", params);
        Log.d("Resources: ", json.toString());

        try{
            int success = json.getInt(TAG_SUCCESS);

            if(success == 1){
                for (int i = 0; i < resources.length(); i++){
                    JSONObject c = resources.getJSONObject(i);

                    Resource res = new Resource();
                    res.setTitle(c.getString("ResName"));
                    res.setResourceId(c.getInt("ResID"));
                    res.setSubject(c.getString("Subject"));
                    res.setType(c.getString("ResType"));
                    res.setUpVote(c.getInt("Likes"));
                    res.setDownVote(c.getInt("Dislikes"));
                    //date format
                    //2014-02-28
                    String dateString =c.getString("PublishingDate");

                    try{
                        SimpleDateFormat formatter =
                            new SimpleDateFormat("yyyy-MM-dd");
                        Date date = formatter.parse(dateString);
                        res.setPublishDate(date); //change to date
                    }catch(ParseException e){
                        e.printStackTrace();
                    }
                    res.setUrl(c.getString("ResURL"));
                    res.setSummary(c.getString("Summary"));
                }
            }else{
                //if there are no resources

            }

        }catch(JSONException e){
            e.printStackTrace();
        }

        return null;
    }

    protected  void onPostExecute(JSONObject result)
    {
        pDialog.dismiss();
    }

}
@@ -617,23 +617,26 @@
  String,
- String
+JSONObject
 %3E %7B%0A%0A   
@@ -1107,16 +1107,64 @@
 uccess%22;
+%0A    private final String TAG_RES = %22resources%22;
 %0A%0A    pu
@@ -1680,22 +1680,26 @@
 otected 
-String
+JSONObject
  doInBac
@@ -2117,16 +2117,17 @@
 arams);%0A
+%0A
         
@@ -2262,16 +2262,72 @@
  == 1)%7B%0A
+                resources = json.getJSONArray(TAG_RES);%0A
         
@@ -3742,16 +3742,16 @@
 )%0A    %7B%0A
-
         
@@ -3765,20 +3765,21 @@
 .dismiss();%0A
+%0A
     %7D%0A%0A%7D

fix bug in resources to return json... missing to pass that json to articles