package me.rutgersdirect.rudirect.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import me.rutgersdirect.rudirect.R;
import me.rutgersdirect.rudirect.model.BusStop;
import me.rutgersdirect.rudirect.ui.activity.BusStopsActivity;

public class BusStopAdapter extends ArrayAdapter<BusStop> {
    private Context context;
    private int layout;
    private ArrayList<BusStop> values;

    public BusStopAdapter(Context context, int layout, ArrayList<BusStop> values) {
        super(context, layout, values);
        this.context = context;
        this.layout = layout;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layout, parent, false);
        }

        TextView busStopName = (TextView) convertView.findViewById(R.id.bus_stop_name);
        TextView busStopTimes = (TextView) convertView.findViewById(R.id.bus_stop_times);

        if (BusStopsActivity.expBusStopIndex != -1 && BusStopsActivity.expBusStopIndex == position) {
            setupExpandedItem(busStopName, busStopTimes, values.get(position));
        } else {
            setupNormalItem(busStopName, busStopTimes, values.get(position));
        }

        return convertView;
    }

    // Sets up the expanded view of the bus stop times
    private void setupExpandedItem(TextView busStopName, TextView busStopTimes, BusStop stop) {
        if (BusStopsActivity.expBusStopIndex != BusStopsActivity.lastExpBusStopIndex || !BusStopsActivity.isExpBusStopIndexExpanded) {
            BusStopsActivity.isExpBusStopIndexExpanded = true;
            busStopTimes.setText("Expanded!");
        } else {
            BusStopsActivity.isExpBusStopIndexExpanded = false;
            setupNormalItem(busStopName, busStopTimes, stop);
        }
    }

    // Sets up the normal view of the bus stop times
    private void setupNormalItem(TextView busStopName, TextView busStopTimes, BusStop stop) {
        int[] times = stop.times;
        String timesString = convertTimesToString(times);
        busStopTimes.setText(timesString);
        busStopName.setText(stop.title);
        setTextColor(busStopName, busStopTimes, times);
    }

    // Converts bus times to a string
    private String convertTimesToString(int[] times) {
        // Bus stop is offline
        if (times.length == 1 && times[0] == -1) {
            return "Offline";
        }
        // Format bus stop times
        StringBuilder builder = new StringBuilder();
        builder.append("Arriving in ");
        for (int i = 0; i < times.length; i++) {
            // Append time
            if (times[i] == 0) {
                builder.append("<1");
            } else {
                builder.append(Integer.toString(times[i]));
            }
            // Append comma
            if (i != times.length - 1) {
                builder.append(", ");
            }
        }
        builder.append(" minutes.");
        return builder.toString();
    }

    // Change text color of the listview items
    private void setTextColor(TextView busStopName, TextView busStopTimes, int[] times) {
        busStopName.setTextColor(Color.parseColor("#000000")); // Black
        if (times[0] == -1) {
            busStopName.setTextColor(Color.parseColor("#9E9E9E")); // Grey
            busStopTimes.setTextColor(Color.parseColor("#9E9E9E"));
        } else if (times[0] == 0 || times[0] == 1) {
            busStopTimes.setTextColor(Color.parseColor("#C62828")); // Red
        } else if (times[0] > 1 && times[0] <= 5) {
            busStopTimes.setTextColor(Color.parseColor("#EF6C00")); // Orange
        } else {
            busStopTimes.setTextColor(Color.parseColor("#1565C0")); // Blue
        }
    }

    // Refills the listview with refreshed values
    public void refill(ArrayList<BusStop> newValues) {
        values.clear();
        values.addAll(newValues);
        notifyDataSetChanged();
    }
}
@@ -281,22 +281,74 @@
 ava.
-util.ArrayList
+text.DateFormat;%0Aimport java.util.ArrayList;%0Aimport java.util.Date
 ;%0A%0Ai
@@ -554,16 +554,75 @@
 Stop%3E %7B%0A
+    private static final int MILLIS_IN_ONE_MINUTE = 60000;%0A
     priv
@@ -2050,105 +2050,942 @@
     
-BusStopsActivity.isExpBusStopIndexExpanded = true;%0A            busStopTimes.setText(%22Expanded!%22);
+// Expand to show the times that the bus is arriving at%0A            if (!busStopTimes.getText().equals(%22Offline%22)) %7B%0A                BusStopsActivity.isExpBusStopIndexExpanded = true;%0A                int%5B%5D minutes = stop.times;%0A                long currentTime = new Date().getTime();%0A                StringBuilder builder = new StringBuilder();%0A                builder.append(%22Arriving at %22);%0A                for (int i = 0; i %3C minutes.length; i++) %7B%0A                    Date stopTime = new Date(currentTime + (minutes%5Bi%5D * MILLIS_IN_ONE_MINUTE));%0A                    String time = DateFormat.getTimeInstance(DateFormat.SHORT).format(stopTime);%0A                    builder.append(time);%0A                    if (i != minutes.length - 1) %7B%0A                        builder.append(%22, %22);%0A                    %7D%0A                %7D%0A                builder.append(%22.%22);%0A                busStopTimes.setText(builder.toString());%0A            %7D
 %0A   

Add expanded view for bus stop times