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.JSONArray;
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.getVenues();
                JSONArray venues = resultObj.getJSONObject("document").getJSONArray("venue");
                for (int i = 0; i < venues.length(); i++) {
                    JSONObject venue = venues.getJSONObject(i);
                    int id = venue.getInt("id");
                    String name = venue.getString("name");
                    boolean isResidential = venue.getString("venueType").equals("residential");
                    mDiningHalls.add(new DiningHall(id, name, isResidential));
                }
            } 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);
        }
    }
}

@@ -2661,32 +2661,34 @@
 id... params) %7B%0A
+//
             try 
@@ -2681,32 +2681,34 @@
           try %7B%0A
+//
                 
@@ -2749,24 +2749,26 @@
 ingHalls) %7B%0A
+//
             
@@ -2853,17 +2853,21 @@
 ame());%0A
-%0A
+//%0A//
         
@@ -2905,24 +2905,26 @@
 dinner%22)) %7B%0A
+//
             
@@ -2992,24 +2992,26 @@
 (%22dinner%22);%0A
+//
             
@@ -3083,32 +3083,34 @@
 ing, String%3E();%0A
+//
                 
@@ -3151,32 +3151,34 @@
 nnerObj.keys();%0A
+//
                 
@@ -3202,32 +3202,34 @@
 ys.hasNext()) %7B%0A
+//
                 
@@ -3258,32 +3258,34 @@
  = keys.next();%0A
+//
                 
@@ -3347,16 +3347,18 @@
 tring()%0A
+//
         
@@ -3436,24 +3436,26 @@
 %22%5C%22%22, %22%22));%0A
+//
             
@@ -3460,33 +3460,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -3529,32 +3529,34 @@
 u(currentMenu);%0A
+//
                 
@@ -3553,33 +3553,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -3612,24 +3612,26 @@
 %22lunch%22)) %7B%0A
+//
             
@@ -3697,24 +3697,26 @@
 t(%22lunch%22);%0A
+//
             
@@ -3792,24 +3792,26 @@
  String%3E();%0A
+//
             
@@ -3859,24 +3859,26 @@
 Obj.keys();%0A
+//
             
@@ -3910,24 +3910,26 @@
 asNext()) %7B%0A
+//
             
@@ -3966,24 +3966,26 @@
 eys.next();%0A
+//
             
@@ -4048,24 +4048,26 @@
 oString());%0A
+//
             
@@ -4072,33 +4072,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -4148,16 +4148,18 @@
 tMenu);%0A
+//
         
@@ -4164,32 +4164,34 @@
               %7D%0A
+//
                 
@@ -4184,32 +4184,34 @@
               %7D%0A
+//
             %7D ca
@@ -4226,33 +4226,37 @@
 NException e) %7B%0A
-%0A
+//%0A//
             %7D%0A  

Comment out menus for now