package com.nikolak.weatherapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.nikolak.weatherapp.ForecastIO.Current;
import com.nikolak.weatherapp.ForecastIO.ForecastAPI;

/**
 * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
 */
public class ForecastFragment extends Fragment {// implements LocationListener{

    protected String latitude, longitude;
    private SharedPreferences settings;
    private View rootView;

    public ImageView currentIcon;

    public TextView currentSummary;

    public TextView currentTemperature;
    public TextView currentApparentTemp;
    public TextView currentPerception;
    public TextView currentWindSpeed;
    public TextView currentMinTemp;
    public TextView currentMaxTemp;
//    private ForecastAPI forecastAPI;

    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        settings = getActivity().getSharedPreferences(MainActivity.PREFS_NAME, 0);
        updateForecast();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_forecast, container, false);

        currentIcon = (ImageView) rootView.findViewById(R.id.currentIcon);

        currentSummary = (TextView) rootView.findViewById(R.id.summaryLabel);

        currentTemperature =  (TextView) rootView.findViewById(R.id.currentTemperature);
        currentApparentTemp = (TextView) rootView.findViewById(R.id.apparentTemperature);
        currentPerception = (TextView)  rootView.findViewById(R.id.perception);
        currentWindSpeed = (TextView) rootView.findViewById(R.id.windSpeed);
        currentMinTemp = (TextView) rootView.findViewById(R.id.minTemperature);
        currentMaxTemp = (TextView) rootView.findViewById(R.id.maxTemperature);

        return rootView;
    }

    public String[] getLocation(){
        return null;
    }

    public void saveLocation(){

    }

    public void updateForecast(){
        latitude = settings.getString("latitude", null);
        longitude = settings.getString("longitude", null);
        FetchWeatherTask updater = new FetchWeatherTask();
        updater.execute();
    }

    public void updateForecastUI(Current current){
        String desc = current.getSummary();
        String temp = Math.round(current.getTemperature())+"°";
        String apparent = Math.round(current.getApparentTemperature())+"°";
        String perception = "0.0mm";
        String windSpeed = current.getWindSpeed()+"mph";
        String minTemp = "-5°";
        String maxTemp = "5°";
        Integer icon;
        switch (current.getIcon()){
            case "clear-day":
                icon = R.drawable.clearday;
                break;
            case  "clear-night":
                icon = R.drawable.clearnight;
                break;
            case "rain":
                icon = R.drawable.rain;
                break;
            case "snow":
                icon = R.drawable.snow;
                break;
            case "sleet":
                icon = R.drawable.sleet;
                break;
            case "wind":
                icon = R.drawable.wind;
                break;
            case "fog":
                icon = R.drawable.fog;
                break;
            case "cloudy":
                icon = R.drawable.cloudy;
                break;
            case "partly-cloudy-day":
                icon = R.drawable.partlycloudyday;
                break;
            case "partly-cloudy-night":
                icon = R.drawable.partlycloudynight;
                break;
            default:
                //TODO: Add default icon
                icon = R.drawable.fog;

        }
        currentIcon.setImageResource(icon);
        currentSummary.setText(desc);

        currentTemperature.setText(temp);

        currentApparentTemp.setText("Feels like: "+apparent);
        currentPerception.setText(perception);
        currentWindSpeed.setText(windSpeed);
        currentMinTemp.setText(minTemp);
        currentMaxTemp.setText(maxTemp);
    }


    public class FetchWeatherTask extends AsyncTask<String, Void, String> {

        private Current current;
        @Override
        protected String doInBackground(String... params) {
            ForecastAPI f = new ForecastAPI();
            current = f.getCurrent(latitude,longitude);
            return null;
        }

        @Override
        protected void onPostExecute(String aString) {
            if(current != null){
                updateForecastUI(current);
            }
        }
    }
}
@@ -900,16 +900,69 @@
 .Current
+ly;%0Aimport com.nikolak.weatherapp.ForecastIO.Forecast
 ;%0Aimport
@@ -1010,16 +1010,48 @@
 stAPI;%0A%0A
+import org.json.JSONException;%0A%0A
 /**%0A * E
@@ -1669,16 +1669,65 @@
 stAPI;%0A%0A
+    private Forecast forecast = new Forecast();%0A%0A
     publ
@@ -3491,31 +3491,44 @@
 ecastUI(
+)%7B%0A        
 Current
+ly
  current
 )%7B%0A     
@@ -3519,18 +3519,40 @@
  current
-)%7B
+ly = forecast.currently;
 %0A       
@@ -3569,24 +3569,26 @@
 sc = current
+ly
 .getSummary(
@@ -3626,24 +3626,26 @@
 ound(current
+ly
 .getTemperat
@@ -3696,24 +3696,26 @@
 ound(current
+ly
 .getApparent
@@ -3801,24 +3801,26 @@
 ed = current
+ly
 .getWindSpee
@@ -3938,16 +3938,18 @@
 (current
+ly
 .getIcon
@@ -5470,17 +5470,16 @@
 ring%3E %7B%0A
-%0A
         
@@ -5490,23 +5490,23 @@
 ate 
-Current current
+Boolean updated
 ;%0A  
@@ -5597,76 +5597,91 @@
     
-ForecastAPI f = new ForecastAPI();%0A            current = f.getCurren
+updated = false;%0A            try %7B%0A                updated = forecast.updateForecas
 t(la
@@ -5687,16 +5687,17 @@
 atitude,
+ 
 longitud
@@ -5696,24 +5696,144 @@
 longitude);%0A
+            %7D catch (JSONException e) %7B%0A                e.printStackTrace();%0A                return null;%0A            %7D%0A
             
@@ -5948,23 +5948,15 @@
  if(
-current != null
+updated
 )%7B%0A 
@@ -5987,23 +5987,16 @@
 ecastUI(
-current
 );%0A     

Update forecast fragment to use newer api wrapper, remove passing class instances to functions to update UI