package com.morristaedt.mirror.modules;

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import java.net.MalformedURLException;
import java.lang.StringBuilder;
import java.net.URLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Calendar;

import com.google.gson.JsonElement;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.morristaedt.mirror.configuration.ConfigurationSettings;
import com.morristaedt.mirror.requests.XKCDRequest;
import com.morristaedt.mirror.requests.XKCDResponse;

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import retrofit.RestAdapter;
import retrofit.RetrofitError;

/**
 * Created by Lee on 4/24/2016.
 */
public class SeptaModule {

    private final String TAG = "SeptaModule";
    private static String textAlert;
    private String routeID;                     //TODO Assigned in settings
    private String nextFiveArrivals;            // Next five arrivals

    public interface SeptaListener {
        void onNewAlert(String SeptaAlert);
    }

    public static void getSingleAlert(final SeptaListener septaListener) {
        final String TAG = "SeptaAsyncModule";
        new AsyncTask<Void, Void, String>() {
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                septaListener.onNewAlert(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                try {
                    Document doc = Jsoup.connect("http://www.septa.org/realtime/alert.html").get();
                    textAlert = doc.text();
                    String removeText = "For current updates on all routes go to System Status.";

                    textAlert = textAlert.replace(removeText, "");
                    return textAlert;
                } catch (Exception err) {
                    Log.d(TAG, "Single Alert exception thrown: " + err);
                    return null;
                }
            }
        }.execute();
    }
}
@@ -1118,29 +1118,30 @@
 private 
-final
+static
  String 
 TAG = %22S
@@ -1136,65 +1136,60 @@
 ing 
-TAG = %22SeptaModule%22;%0A    private static String textAlert;
+textAlert;            //Alert information from Septa
 %0A   
@@ -1332,16 +1332,54 @@
 rivals%0A%0A
+    //Updates when new alert received%0A
     publ
@@ -1454,24 +1454,74 @@
 rt);%0A    %7D%0A%0A
+    //Returns travel alert information from septa%0A
     public s
@@ -1534,22 +1534,22 @@
 void get
-Single
+Travel
 Alert(fi
@@ -1629,16 +1629,96 @@
 Module%22;
+ //Use to log error message%0A%0A        //Start UI thread for background operations
 %0A       
@@ -2037,16 +2037,96 @@
   try %7B%0A
+                    //Uses Jsoup to connect to septa and pulls text off webpage%0A
         
@@ -2241,191 +2241,244 @@
     
-textAlert = doc.text();%0A                    String removeText = %22For current updates on all routes go to System Status.%22;%0A%0A                    textAlert = textAlert.replace(removeText
+//Coverts the document to string%0A                    textAlert = doc.text();%0A                    //Removes generic text from text%0A                    textAlert = textAlert.replace(%22For current updates on all routes go to System Status.%22
 , %22%22

Add comments to SeptaModule