package com.pennapps.labs.pennmobile;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.pennapps.labs.pennmobile.adapters.DiningAdapter;
import com.pennapps.labs.pennmobile.api.DiningAPI;
import com.pennapps.labs.pennmobile.classes.DiningHall;

import org.json.JSONException;
import org.json.JSONObject;

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

public class DiningFragment extends ListFragment {

    private DiningAPI mAPI;
    private ListView mListView;
    private ArrayList<DiningHall> mDiningHalls;
    private DiningAdapter mAdapter;
    private Activity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAPI = new DiningAPI();
        mActivity = getActivity();
        mDiningHalls = new ArrayList<DiningHall>();
        new GetOpenTask().execute();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = getListView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dining, container, false);
        return v;
    }

    private class GetOpenTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                JSONObject resultObj = mAPI.getDiningInfo("open");
                Iterator<String> keys = resultObj.keys();
                while(keys.hasNext()) {
                    String key = keys.next();
                    int id = Integer.parseInt(resultObj.get(key).toString());
                    boolean open = resultObj.get(key).toString().equals("true");
                    mDiningHalls.add(new DiningHall(id, key, open));
                }
            } catch (JSONException e) {

            } catch (NullPointerException e) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void params) {
            new GetMenusTask().execute();
        }
    }

    private class GetMenusTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                for (DiningHall mDiningHall : mDiningHalls) {
                    JSONObject resultObj = mAPI.getDiningInfo("scrape/" + mDiningHall.getName());

                    if (resultObj.has("dinner")) {
                        JSONObject dinnerObj = (JSONObject) resultObj.get("dinner");
                        HashMap<String, String> currentMenu = new HashMap<String, String>();
                        Iterator<String> keys = dinnerObj.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            currentMenu.put(key, dinnerObj.get(key).toString()
                                    .replace("[", "").replace("]", "").replace("\"", ""));
                        }

                        mDiningHall.setDinnerMenu(currentMenu);
                    }

                    if (resultObj.has("lunch")) {
                        JSONObject lunchObj = (JSONObject) resultObj.get("lunch");
                        HashMap<String, String> currentMenu = new HashMap<String, String>();
                        Iterator<String> keys = lunchObj.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            currentMenu.put(key, lunchObj.get(key).toString());
                        }

                        mDiningHall.setLunchMenu(currentMenu);
                    }
                }
            } catch (JSONException e) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void params) {
            mAdapter = new DiningAdapter(mActivity, mDiningHalls);
            mListView.setAdapter(mAdapter);
        }
    }
}

@@ -455,16 +455,43 @@
 gHall;%0A%0A
+import org.json.JSONArray;%0A
 import o
@@ -1733,25 +1733,15 @@
 .get
-DiningInfo(%22open%22
+Venues(
 );%0A 
@@ -1759,133 +1759,181 @@
     
-Iterator%3CString%3E keys = resultObj.keys();%0A                while(keys.hasNext()) %7B%0A                    String key = keys.nex
+JSONArray venues = resultObj.getJSONArray(%22venue%22);%0A                for (int i = 0; i %3C venues.length(); i++) %7B%0A                    JSONObject venue = venues.getJSONObjec
 t(
+i
 );%0A 
@@ -1964,54 +1964,84 @@
 d = 
-Integer.parseInt(resultObj.get(key).to
+venue.getInt(%22id%22);%0A                    String name = venue.get
 String(
-)
+%22name%22
 );%0A 
@@ -2071,57 +2071,72 @@
 ean 
-open = resultObj.get(key).toString().equals(%22true
+isResidential = venue.getString(%22venueType%22).equals(%22residential
 %22);%0A
@@ -2195,17 +2195,27 @@
 id, 
-key, open
+name, isResidential
 ));%0A

Update dining hall parsing loop