#    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.

import hexchat

import json
from six.moves.urllib.request import urlopen


__module_name__ = "weather"
__module_version__ = "1.0"
__module_description__ = "weather"


def say(s):
    hexchat.command('SAY %s' % s)


def wuweather_full(word_eol, verbose=True):
    if len(word_eol) < 2:
        return
    loc = word_eol[1]
    pws = loc.startswith('pws:') and '1' or '0'

    #grab weather from api
    try:
        weather = json.loads(urlopen('http://api.wunderground.com/api/587a76efcc30bee2/conditions/almanac/astronomy/forecast/pws:' + pws + '/q/' + loc +'.json').read().decode())
    except Exception as e:
        return
    if not weather:
        return

    #does the response contain an error
    if 'error' in weather['response']:
        say("wuweather: %s" % weather['response']['error']['description'])
        return
    #does the response contain extra 'results'
    elif 'results' in weather['response']:
        output = 'wuweather: Ambiguous Search, options: '
        counter = 0
        for index in weather['response']['results']:
            if counter == 0:
                output += "%s %s %s " % (index['city'], index['state'], index['country'])
            else:
                output += "/ %s %s %s " % (index['city'], index['state'], index['country'])
            counter += 1
        say(output)
        return

    #if we made it here, everything should be good
    try:
        #format the city name
        if pws == '1':
            loc_key = 'observation_location'
        else:
            loc_key = 'display_location'
        city = "[%s, %s, %s]" % \
        (weather['current_observation'][loc_key]['city'],
        weather['current_observation'][loc_key]['state'],
        weather['current_observation'][loc_key]['country'])

        #output the averages, sunrise, moon
        #some locations don't have an almanac
        if weather['almanac']['airport_code'] != "":
            almanac_output = "Avg High: %sF, %sC Low: %sF, %sC " % \
            (weather['almanac']['temp_high']['normal']['F'],
            weather['almanac']['temp_high']['normal']['C'],
            weather['almanac']['temp_low']['normal']['F'],
            weather['almanac']['temp_low']['normal']['C'])
        else:
            almanac_output = ""

        if verbose:
            #output sunrise w/ almanac if available
            say("%s %sSunrise: %s:%s Sunset: %s:%s" % \
            (city,
            almanac_output,
            weather['moon_phase']['sunrise']['hour'],
            weather['moon_phase']['sunrise']['minute'],
            weather['moon_phase']['sunset']['hour'],
            weather['moon_phase']['sunset']['minute']))

        #output the current weather
        say("%s Current: %s %sF, %sC (Feels like: %sF, %sC), Humidity: %s, Wind: %s" % \
        (city,
        weather['current_observation']['weather'],
        weather['current_observation']['temp_f'],
        weather['current_observation']['temp_c'],
        weather['current_observation']['feelslike_f'],
        weather['current_observation']['feelslike_c'],
        weather['current_observation']['relative_humidity'],
        weather['current_observation']['wind_string']))

        if not verbose:
            return

        #output first 3 days of forcast (today, tomorrow, second day)
        counter = 0
        for index in weather['forecast']['simpleforecast']['forecastday']:
            if counter == 0:
                day = 'Today'
            elif counter == 1:
                day = 'Tomorrow'
            else:
                day = index['date']['weekday']

            say("%s %s: %s [POP: %s%%] [High: %sF, %sC Low: %sF, %sC] Humidity: [Max: %s%%, Min: %s%%, Avg: %s%%] Wind: [%smph, %skph]" % \
            (city,
            day,
            index['conditions'],
            index['pop'],
            index['high']['fahrenheit'],
            index['high']['celsius'],
            index['low']['fahrenheit'],
            index['low']['celsius'],
            index['maxhumidity'],
            index['minhumidity'],
            index['avehumidity'],
            index['avewind']['mph'],
            index['avewind']['kph']))
            counter +=1
            if counter >= 3: break
        say("%s URL: %s" % (city, weather['current_observation']['forecast_url']))
    except:
        return


def weather(word, word_eol, userdata):
    wuweather_full(word_eol, verbose=False)
    return hexchat.EAT_ALL
hexchat.hook_command('weather', weather)


def weatherverbose(word, word_eol, userdata):
    wuweather_full(word_eol, verbose=True)
    return hexchat.EAT_ALL
hexchat.hook_command('weatherverbose', weatherverbose)
