package com.pennapps.labs.pennmobile;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class MenuFragment extends Fragment {

    private DiningAPI mAPI;
    private DiningHall mDiningHall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAPI = new DiningAPI(((MainActivity) getActivity()).getAPIClient());
        mDiningHall = getArguments().getParcelable("DiningHall");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_menu, container, false);
        v.setBackgroundColor(Color.WHITE);
        TextView diningHallNameTV = (TextView) v.findViewById(R.id.dining_hall_name);
        diningHallNameTV.setText(mDiningHall.getName());
        diningHallNameTV.setGravity(Gravity.LEFT);
        fillDescriptions(v);
        try {
            if (mDiningHall.isResidential() && mDiningHall.hasMenu()) {
                JSONObject resultObj = mAPI.getDailyMenu(mDiningHall.getId());

                JSONArray meals = resultObj.getJSONObject("Document")
                        .getJSONObject("tblMenu")
                        .getJSONArray("tblDayPart");

                for (int i = 0; i < meals.length(); i++) {
                    JSONObject meal = meals.getJSONObject(i);
                    parseMeal(meal, mDiningHall);
                }
            }
        } catch (JSONException ignored) {
        }
        return v;
    }

    public static void parseMeal(JSONObject meal, DiningHall diningHall) {
        try {
            String mealName = meal.getString("txtDayPartDescription");

            JSONArray stations = new JSONArray();
            try {
                stations = meal.getJSONArray("tblStation");
            } catch (JSONException e) {
                JSONObject stationsObject = meal.getJSONObject("tblStation");
                stations.put(stationsObject);
            }
            HashMap<String, HashSet<String>> currentMenu = new HashMap<>();
            for (int j = 0; j < stations.length(); j++) {
                JSONObject station = stations.getJSONObject(j);
                parseStation(station, currentMenu);
            }

            if (mealName != null) {
                diningHall.menus.put(mealName, currentMenu);
            }
        } catch (JSONException ignored) {

        }
    }

    public static void parseStation(JSONObject station, HashMap<String, HashSet<String>> menu) {
        try {
            String stationName = station.getString("txtStationDescription");
            JSONArray stationItems = new JSONArray();
            try {
                stationItems = station.getJSONArray("tblItem");
            } catch (JSONException e) {
                JSONObject stationItem = station.getJSONObject("tblItem");
                stationItems.put(stationItem);
            }
            for (int k = 0; k < stationItems.length(); k++) {
                JSONObject foodItem = stationItems.getJSONObject(k);
                String foodName = foodItem.getString("txtTitle");
                foodName = StringEscapeUtils.unescapeHtml4(foodName);
                if (menu.containsKey(stationName)) {
                    HashSet<String> items = menu.get(stationName);
                    items.add(foodName);
                    menu.put(stationName, items);
                } else {
                    HashSet<String> items = new HashSet<>();
                    items.add(foodName);
                    menu.put(stationName, items);
                }
            }
        } catch (JSONException ignored) {

        }
    }

    public void fillDescriptions(View view) {
        DiningHall diningHall = mDiningHall;
        TextView hallNameTV = (TextView) view.findViewById(R.id.dining_hall_name);
        TextView hallStatus = (TextView) view.findViewById(R.id.dining_hall_status);
        LinearLayout menuParent = (LinearLayout) view.findViewById(R.id.menu_parent);

        hallNameTV.setText(WordUtils.capitalizeFully(diningHall.getName()));
        view.setTag(diningHall);
        if (diningHall.isOpen()) {
            hallStatus.setText("Open");
            hallStatus.setBackground(ContextCompat.getDrawable(view.getContext(), R.drawable.label_green));
        } else {
            hallStatus.setText("Closed");
            hallStatus.setBackground(ContextCompat.getDrawable(view.getContext(), R.drawable.label_red));
        }

        StringBuilder menuText = new StringBuilder();
        for (Map.Entry<String, HashMap<String, HashSet<String>>> menu : diningHall.menus.entrySet()) {
            menuText.setLength(0);
            String mealName = StringUtils.capitalize(menu.getKey());
            for (Map.Entry<String, HashSet<String>> menuItem : menu.getValue().entrySet()) {
                String key = StringUtils.capitalize(menuItem.getKey());
                HashSet<String> items = menuItem.getValue();
                String tab = "&nbsp&nbsp&nbsp ";
                menuText.append("<b>");
                menuText.append(key);
                menuText.append("</b> <br>");
                for (String item : items) {
                    menuText.append(tab);
                    menuText.append(item);
                    menuText.append("<br>");
                }
            }
            // Meal name
            TextView mealNameTV = new TextView(view.getContext(), null, R.style.MealName);
            mealNameTV.setVisibility(View.VISIBLE);
            mealNameTV.setText(mealName);
            menuParent.addView(mealNameTV);
            // Menu
            TextView menuTV = new TextView(view.getContext(), null, R.style.Menu);
            menuTV.setVisibility(View.VISIBLE);
            menuTV.setText(Html.fromHtml(menuText.toString()));
            menuParent.addView(menuTV);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.dining);
    }
}

@@ -1676,603 +1676,8 @@
 v);%0A
-        try %7B%0A            if (mDiningHall.isResidential() && mDiningHall.hasMenu()) %7B%0A                JSONObject resultObj = mAPI.getDailyMenu(mDiningHall.getId());%0A%0A                JSONArray meals = resultObj.getJSONObject(%22Document%22)%0A                        .getJSONObject(%22tblMenu%22)%0A                        .getJSONArray(%22tblDayPart%22);%0A%0A                for (int i = 0; i %3C meals.length(); i++) %7B%0A                    JSONObject meal = meals.getJSONObject(i);%0A                    parseMeal(meal, mDiningHall);%0A                %7D%0A            %7D%0A        %7D catch (JSONException ignored) %7B%0A        %7D%0A
     

Remove unused API calls