package me.rutgersdirect.rudirect.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

import me.rutgersdirect.rudirect.R;
import me.rutgersdirect.rudirect.BusConstants;
import me.rutgersdirect.rudirect.helper.ShowBusStopsHelper;
import me.rutgersdirect.rudirect.model.BusStop;

public class BusStopsActivity extends ActionBarActivity {
    public static boolean active;
    private String busTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bus_stops);

        Intent intent = getIntent();
        busTag = intent.getStringExtra(BusConstants.BUS_TAG_MESSAGE);
        String[] busStopTitles = intent.getStringArrayExtra(BusConstants.BUS_STOP_TITLES_MESSAGE);
        String[] busStopTimes = intent.getStringArrayExtra(BusConstants.BUS_STOP_TIMES_MESSAGE);

        setListView(busStopTitles, busStopTimes);

        //ActionBar setup
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

    }

    // Updates the list view with bus stop titles and times
    public void setListView(String[] titles, String[] times) {
        ArrayList<BusStop> buses = new ArrayList<>(titles.length);

        for (int i = 0; i < titles.length; i++) {
            buses.add(new BusStop(null, titles[i], times[i]));
        }

        ListView busTimesList = (ListView) findViewById(android.R.id.list);
        BusStopAdapter adapter = new BusStopAdapter(getApplicationContext(), R.layout.list_bus_stops, buses);
        busTimesList.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        active = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        active = false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_bus_times, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.refresh) {
            new ShowBusStopsHelper().execute(busTag, BusStopsActivity.this);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -385,16 +385,68 @@
 rayList;
+%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;
 %0A%0Aimport
@@ -749,16 +749,46 @@
  busTag;
+%0A    private Timer autoUpdate;
 %0A%0A    @O
@@ -2130,20 +2130,537 @@
    a
-ctive = true
+utoUpdate = new Timer(); //Auto Update Setup%0A        autoUpdate.schedule(new TimerTask()%7B%0A            public void run()%7B%0A                runOnUiThread(new Runnable()%7B%0A                    public void run() %7B%0A                        updateBusTimes();%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D, 0, 60000); //Updates every minute nonetheless%0A        active = true;%0A    %7D%0A%0A    private void updateBusTimes()%7B //Helper Class to update buses%0A        new ShowBusStopsHelper().execute(busTag, BusStopsActivity.this)
 ;%0A  
@@ -3385,17 +3385,16 @@
 emId();%0A
-%0A
         
@@ -3523,33 +3523,32 @@
 true;%0A        %7D%0A
-%0A
         return s

Add Auto Refresh