package is.genki.bigredapp.android;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

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

import java.util.ArrayList;

/**
 * Fragment containing a ListView of dining halls.
 * Each dining hall name can be tapped to get it's menu for today.
 */
public class DiningListFragment extends Fragment {

    private static final String BASE_URL = "http://redapi-tious.rhcloud.com/dining";
    private static final String[] MEALS_LIST = {"Breakfast", "Lunch", "Dinner"};
    private ListView mListView;
    private ArrayList<String> mDiningList;

    public DiningListFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mListView = (ListView) rootView.findViewById(R.id.listView);

        // http://developer.android.com/guide/topics/ui/declaring-layout.html#HandlingUserSelections
        AdapterView.OnItemClickListener mListViewClickHandler = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                handleMenuResponse(mDiningList.get(position));
            }
        };
        mListView.setOnItemClickListener(mListViewClickHandler);

        getDiningList();

        return rootView;
    }

    /**
     * https://developer.android.com/training/basics/network-ops/connecting.html#connection
     * @return the device has a connection to the internet
     * Makes a Toast if there is no connection
     */
    private boolean isConnected() {
        ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        } else {
            Toast.makeText(getActivity(), "No Internet Connection", Toast.LENGTH_LONG).show();
            return false;
        }
    }

    /**
     * Populates mDiningList with the String list of dining halls.
     * Sets mListView's adapter to this list.
     */
    private void getDiningList() {
        if (isConnected()) {
            // Async Task to get the list of dining halls
            new GetRequest() {
                @Override
                protected void onPostExecute(String result) {
                    try {
                        // Convert result to JSONArray, then get diningHall string list
                        JSONArray jsonArray = new JSONArray(result);
                        mDiningList = new ArrayList<>();
                        int len = jsonArray.length();
                        for (int i=0; i<len; i++){
                            mDiningList.add(jsonArray.getString(i));
                        }

                        // http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
                        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                                android.R.layout.simple_list_item_1, mDiningList);
                        mListView.setAdapter(adapter);
                    } catch (JSONException e) {
                        Toast.makeText(getActivity(), "Data has invalid format", Toast.LENGTH_LONG).show();
                    }
                }
            }.setContext(getActivity()).execute(BASE_URL);
        }
    }

    /**
     * @param diningHall the dining hall's id as a String, as per RedAPI.
     * Displays today's menu for the given dining hall.
     * If there is none, displays a Toast saying so.
     */
    private void handleMenuResponse(final String diningHall) {
        final String mealCsv = MEALS_LIST[0] + "," + MEALS_LIST[1] + "," + MEALS_LIST[2];
        final String url = BASE_URL + "/menu/" + diningHall + "/" + mealCsv + "/MEALS";
        if (isConnected()) {
            // Async Task to get the menu for a dining hall
            new GetRequest() {
                @Override
                protected void onPostExecute(String result) {
                    try {
                        String menu = "";
                        JSONObject jsonResult = new JSONObject(result);
                        for (String meal : MEALS_LIST) {
                            menu = menu + meal + ":\n";
                            JSONArray jsonArray = jsonResult.getJSONObject(meal).getJSONArray(diningHall);
                            int len = jsonArray.length();
                            for (int i=0; i<len; i++) {
                                menu = menu + jsonArray.getJSONObject(i).getString("name")+", ";
                            }
                            menu = menu + "\n\n";
                        }
                        new AlertDialog.Builder(getActivity())
                                .setTitle("Today's Menu").setMessage(menu)
                                .create().show();
                    } catch (JSONException e) {
                        Toast.makeText(getActivity(), "No menu for this location yet!", Toast.LENGTH_LONG).show();
                    }
                }
            }.setContext(getActivity()).execute(url);
        }
    }
}
@@ -424,24 +424,56 @@
 t.ListView;%0A
+import android.widget.TextView;%0A
 import andro
@@ -489,16 +489,16 @@
 .Toast;%0A
-
 %0Aimport 
@@ -604,16 +604,80 @@
 rayList;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -3588,24 +3588,34 @@
 apter = new 
+PrettyName
 ArrayAdapter
@@ -3614,18 +3614,16 @@
 yAdapter
-%3C%3E
 (getActi
@@ -5748,32 +5748,32 @@
               %7D%0A
-
             %7D.se
@@ -5825,10 +5825,998 @@
  %7D%0A    %7D
+%0A%0A    public class PrettyNameArrayAdapter extends ArrayAdapter%3CString%3E %7B%0A        int resource;%0A        Pattern p = Pattern.compile(%22%5C%5Cb(%5Ba-z%5D)%22);%0A%0A        public PrettyNameArrayAdapter(Context context, int res, ArrayList%3CString%3E items) %7B%0A            super(context, res, items);%0A            this.resource = res;%0A        %7D%0A%0A        @Override%0A        public View getView(int position, View convertView, ViewGroup parent) %7B%0A            LayoutInflater inflater = LayoutInflater.from(getContext());%0A            TextView label = (TextView) inflater.inflate(this.resource, parent, false);%0A            String name = getItem(position);%0A%0A            name = name.replace(%22_%22, %22 %22);%0A            Matcher m = p.matcher(name);%0A            StringBuffer sb = new StringBuffer();%0A            while (m.find()) %7B%0A                m.appendReplacement(sb, m.group(1).toUpperCase());%0A            %7D%0A            m.appendTail(sb);%0A%0A            label.setText(sb.toString());%0A%0A            return label;%0A        %7D%0A    %7D
 %0A%7D

Make dining hall titles a little nicer