package cat_in_136.github.io.demoweatherprovider;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cyanogenmod.providers.WeatherContract;
import cyanogenmod.weather.RequestInfo;
import cyanogenmod.weather.WeatherInfo;
import cyanogenmod.weather.WeatherLocation;
import cyanogenmod.weather.util.WeatherUtils;
import cyanogenmod.weatherservice.ServiceRequest;
import cyanogenmod.weatherservice.ServiceRequestResult;
import cyanogenmod.weatherservice.WeatherProviderService;

public class DemoWeatherProviderService extends WeatherProviderService {
    private static final String TAG = DemoWeatherProviderService.class.getSimpleName();
    private static final int SERVICE_REQUEST_CANCELLED = -1;
    private static final int SERVICE_REQUEST_SUBMITTED = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate()");
    }

    @Override
    protected void onRequestSubmitted(ServiceRequest serviceRequest) {
        RequestInfo requestInfo = serviceRequest.getRequestInfo();
        int requestType = requestInfo.getRequestType();
        Log.d(TAG, "Received request type " + requestType);

        switch (requestType) {
            case RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ:
            case RequestInfo.TYPE_WEATHER_BY_WEATHER_LOCATION_REQ:
                handleWeatherRequest(serviceRequest);
                break;
            case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ:
                handleLookupRequest(serviceRequest);
                break;
            default:
                serviceRequest.fail();
                break;
        }
    }

    @Override
    protected void onRequestCancelled(ServiceRequest serviceRequest) {

    }

    private void handleWeatherRequest(final ServiceRequest serviceRequest) {
        final RequestInfo requestInfo = serviceRequest.getRequestInfo();
        Log.d(TAG, "Received weather request info: " + requestInfo.toString());

        String cityName = "?";
        if (requestInfo.getCityName() != null) {
            cityName = requestInfo.getCityName();
        } else if ((requestInfo.getWeatherLocation() != null) && (requestInfo.getWeatherLocation().getCity() != null)) {
            cityName = requestInfo.getWeatherLocation().getCity();
        } else {
            // cityName = "?";
        }

        // Setup dummy weather status
        WeatherInfo.Builder weatherInfoBuilder = new WeatherInfo.Builder(cityName, 0, WeatherContract.WeatherColumns.TempUnit.CELSIUS);
        weatherInfoBuilder.setWeatherCondition(WeatherContract.WeatherColumns.WeatherCode.COLD);
        weatherInfoBuilder.setHumidity(0);
        weatherInfoBuilder.setWind(0, 0, WeatherContract.WeatherColumns.WindSpeedUnit.KPH);
        weatherInfoBuilder.setTodaysHigh(0);
        weatherInfoBuilder.setTodaysLow(0);

        List<WeatherInfo.DayForecast> forecast = new ArrayList<WeatherInfo.DayForecast>();
        for (int i = 0; i < 5; i++) {
            WeatherInfo.DayForecast.Builder dayForecastBuilder = new WeatherInfo.DayForecast.Builder(WeatherContract.WeatherColumns.WeatherCode.SUNNY);
            dayForecastBuilder.setHigh(0);
            dayForecastBuilder.setLow(0);
            forecast.add(dayForecastBuilder.build());
        }
        weatherInfoBuilder.setForecast(forecast);

        ServiceRequestResult serviceRequestResult = new ServiceRequestResult.Builder(weatherInfoBuilder.build()).build();
        serviceRequest.complete(serviceRequestResult);
    }

    private void handleLookupRequest(final ServiceRequest serviceRequest) {
        final RequestInfo requestInfo = serviceRequest.getRequestInfo();
        Log.d(TAG, "Received lookup request info: " + requestInfo.toString());

        String cityName = "?";
        if (requestInfo.getCityName() != null) {
            cityName = requestInfo.getCityName();
        } else if (requestInfo.getWeatherLocation() != null && requestInfo.getWeatherLocation().getCity() != null) {
            cityName = requestInfo.getWeatherLocation().getCity();
        } else {
            //cityName = "?";
        }

        // Setup dummy location
        List<WeatherLocation> weatherLocation = new ArrayList<WeatherLocation>();
        WeatherLocation.Builder weatherLocationBuilder;
        weatherLocationBuilder = new WeatherLocation.Builder(cityName, cityName);
        weatherLocationBuilder.setState(cityName);
        weatherLocationBuilder.setPostalCode(cityName);
        weatherLocationBuilder.setCountry(cityName);
        weatherLocationBuilder.setCountryId(cityName);
        weatherLocation.add(weatherLocationBuilder.build());

        ServiceRequestResult serviceRequestResult = new ServiceRequestResult.Builder(weatherLocation).build();
        serviceRequest.complete(serviceRequestResult);
    }
}

@@ -122,45 +122,132 @@
 oid.
-os.IBinder;%0Aimport android.os.Message
+content.SharedPreferences;%0Aimport android.os.IBinder;%0Aimport android.os.Message;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -1047,16 +1047,66 @@
 D = 0;%0A%0A
+    private SharedPreferences sharedPreferences;%0A%0A
     @Ove
@@ -1199,16 +1199,98 @@
 ate()%22);
+%0A%0A        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 %0A    %7D%0A%0A
@@ -2382,357 +2382,42 @@
 e = 
-%22?%22;%0A        if (requestInfo.getCityName() != null) %7B%0A            cityName = requestInfo.getCityName();%0A        %7D else if ((requestInfo.getWeatherLocation() != null) && (requestInfo.getWeatherLocation().getCity() != null)) %7B%0A            cityName = requestInfo.getWeatherLocation().getCity();%0A        %7D else %7B%0A            // cityName = %22?%22;%0A        %7D
+getCityNameOfService(requestInfo);
 %0A%0A  
@@ -3834,352 +3834,42 @@
 e = 
-%22?%22;%0A        if (requestInfo.getCityName() != null) %7B%0A            cityName = requestInfo.getCityName();%0A        %7D else if (requestInfo.getWeatherLocation() != null && requestInfo.getWeatherLocation().getCity() != null) %7B%0A            cityName = requestInfo.getWeatherLocation().getCity();%0A        %7D else %7B%0A            //cityName = %22?%22;%0A        %7D
+getCityNameOfService(requestInfo);
 %0A%0A  
@@ -4566,11 +4566,491 @@
 );%0A    %7D
+%0A%0A    private String getCityNameOfService(final RequestInfo requestInfo) %7B%0A        String cityName = sharedPreferences.getString(%22default_city_name%22, %22?%22);%0A        if (requestInfo.getCityName() != null) %7B%0A            cityName = requestInfo.getCityName();%0A        %7D else if (requestInfo.getWeatherLocation() != null && requestInfo.getWeatherLocation().getCity() != null) %7B%0A            cityName = requestInfo.getWeatherLocation().getCity();%0A        %7D%0A%0A        return cityName;%0A    %7D
 %0A%7D%0A

fix bug on the location (e.g. GPS) on, in which case default city name is loaded from the shared preference.