// Copyright 2015 Nikola Kovacevic <nikolak@outlook.com>
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package com.nikolak.weatherapp;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class Utils {

    public static final String TAG = "UtilsClass";

    public static Integer getIconFromValue(String iconValue) {
        Integer icon;
        switch (iconValue) {
            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;

        }
        return icon;
    }

    public static String getCity(Double lat, Double lon, Context context) {
        Geocoder geocoder = new Geocoder(context);
        List<Address> address = null;
        try {
            address = geocoder.getFromLocation(
                    lat,
                    lon,
                    1);
        } catch (IOException e) {
            Log.e(TAG, lat.toString() + lon.toString());
            e.printStackTrace();
            return null;
        }

        if (address == null || address.size() <= 0) {
            Log.w(TAG, "Could not find a location name");
            return null;
        } else {
            Address finalAddress = address.get(0);
            return finalAddress.getLocality();
        }
    }

    public static boolean usesAmPm() {
        DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.getDefault());
        return df instanceof SimpleDateFormat && ((SimpleDateFormat) df).toPattern().contains("a");
    }

    public static String getHour(int unixTime) {
        long time = unixTime * (long) 1000;
        Date date = new Date(time);
        SimpleDateFormat hourformat;
        if (usesAmPm()) {
            hourformat = new SimpleDateFormat("ha");
        } else {
            hourformat = new SimpleDateFormat("HH");
        }
        hourformat.setTimeZone(TimeZone.getDefault());
        return hourformat.format(date);
    }
}

@@ -3662,11 +3662,344 @@
 );%0A    %7D
+%0A%0A    public static String getDay(int unixTime)%7B%0A        long time = unixTime * (long) 1000;%0A        Date date = new Date(time);%0A        SimpleDateFormat dayFormat;%0A        dayFormat = new SimpleDateFormat(%22EEE%22, Locale.getDefault());%0A        dayFormat.setTimeZone(TimeZone.getDefault());%0A        return dayFormat.format(date);%0A    %7D
 %0A%7D%0A

Add function to return short day name to utils