package mystikos.pollen;

import android.os.StrictMode;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView JnumberToday;
    private TextView JnumberTomorrow;
    private TextView JnumberDayAfter;
    private JsonElement jelement;
    private JsonObject jobject;
    private JsonElement jelement2;
    private JsonObject locationobject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        JnumberToday = (TextView) findViewById(R.id.numberToday);
        JnumberTomorrow = (TextView) findViewById(R.id.numberTomorrow);
        JnumberDayAfter = (TextView) findViewById(R.id.numberDayAfter);

        run();
    }

    private void run() {
        //getZip();
        getPollenData();
        //getLocationData();
        setPollenText();
        //setLocationText();
        //setTheme(); DOES NOT WORK YET
    }

    private String[] getPollenData() {
        String[] pollen = new String[3];
        try {
            URL url = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=02145&Affiliateid=9642&AppID=2.1.0&uid=6693636764");
            //URL url = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=" + getZip() + "&Affiliateid=9642&AppID=2.1.0&uid=6693636764"); //url with zip code variable
            InputStream in = url.openStream();

            jelement = new JsonParser().parse(new InputStreamReader(in));
            jobject = jelement.getAsJsonObject();

            jobject = jobject.getAsJsonObject("allergyForecast");

            pollen[0] = jobject.get("Day0").toString(); //pollen today
            pollen[1] = jobject.get("Day1").toString(); //pollen tomorrow
            pollen[2] = jobject.get("Day2").toString(); //pollen day after
        } catch (Exception e) {e.printStackTrace();}
        return pollen;
    } //method to parse pollen data and return array of values

    private void setPollenText() {
        JnumberToday.setText(getPollenData()[0]);
        JnumberTomorrow.setText(getPollenData()[1]);
        JnumberDayAfter.setText(getPollenData()[2]);
    } //method to set the textviews for pollen data based on values in array returned by method getPollenData()

    private void getZip() {
        String zip;
        //return zip;
    } //method to get zip code from settings file

    private void setTheme() {
        double pollenToday = Double.parseDouble(getPollenData()[0]);
        if ( pollenToday <= 4.0)
            super.setTheme(R.style.green);
        if ( pollenToday > 4.0 && pollenToday <= 8.0)
            super.setTheme(R.style.yellow);
        else
            super.setTheme(R.style.red);
    } //method to set activity theme based on pollen level

    private void setLocationText() {
        try {
            URL url = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=02145&Affiliateid=9642&AppID=2.1.0&uid=6693636764");
            //URL url = new URL("http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=" + getZip() + "&Affiliateid=9642&AppID=2.1.0&uid=6693636764"); //url with zip code variable
            InputStream in = url.openStream();

            jelement2 = new JsonParser().parse(new InputStreamReader(in));
            locationobject = jelement2.getAsJsonObject();

            locationobject = locationobject.getAsJsonObject("ForecastInfo");
            String city = locationobject.get("City").toString();
            String state = locationobject.get("State").toString();
            String location = city + "," + state;
            setTitle(location);
        } catch (Exception e) {e.printStackTrace();}
    } //method to set title to location, currently does not work
}

@@ -1370,37 +1370,8 @@
 ();%0A
-        //getLocationData();%0A
     
@@ -1399,18 +1399,16 @@
         
-//
 setLocat
@@ -1541,17 +1541,17 @@
  String%5B
-3
+5
 %5D;%0A     
@@ -2104,32 +2104,179 @@
 sJsonObject();%0A%0A
+            pollen%5B3%5D = jobject.get(%22City%22).toString(); //city name%0A            pollen%5B4%5D = jobject.get(%22State%22).toString(); //state abbreviation%0A%0A
             jobj
@@ -2325,17 +2325,16 @@
 cast%22);%0A
-%0A
         
@@ -3546,795 +3546,129 @@
     
+S
 tr
-y %7B%0A            URL url = new URL(%22http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=02145&Affiliateid=9642&AppID=2.1.0&uid=6693636764%22);%0A            //URL url = new URL(%22http://pollenapps.com/AllergyAlertWebSVC/api/1.0/Forecast/ForecastForZipCode?Zipcode=%22 + getZip() + %22&Affiliateid=9642&AppID=2.1.0&uid=6693636764%22); //url with zip code variable%0A            InputStream in = url.openStream();%0A%0A            jelement2 = new JsonParser().parse(new InputStreamReader(in));%0A            locationobject = jelement2.getAsJsonObject();%0A%0A            locationobject = locationobject.getAsJsonObject(%22ForecastInfo%22);%0A            String city = locationobject.get(%22City%22).toString();%0A            String state = locationobject.get(%22State%22).toString();%0A  
+ing city = getPollenData()%5B3%5D.replace(%22%5C%22%22, %22%22);%0A        String state = getPollenData()%5B4%5D.replace(%22%5C%22%22, %22%22);%0A
         
   St
@@ -3659,26 +3659,24 @@
 %22);%0A        
-  
 String locat
@@ -3690,16 +3690,17 @@
 ity + %22,
+ 
 %22 + stat
@@ -3702,20 +3702,16 @@
  state;%0A
-    
         
@@ -3738,120 +3738,8 @@
     
-    %7D catch (Exception e) %7Be.printStackTrace();%7D%0A    %7D //method to set title to location, currently does not work
+%7D
 %0A%7D%0A

fix logic to set title text with parsed location