# -*- coding: utf-8 -*-
import datetime
import logging
import webbrowser

import gntp.config
import pytz
import requests
import rumps
from icalendar import Calendar

import hatarake
import hatarake.config
import hatarake.shim

logger = logging.getLogger(__name__)


class Hatarake(hatarake.shim.Shim):
    def __init__(self):
        super(Hatarake, self).__init__("Hatarake")
        self.menu = ["Reload", "Debug", "Report Issue"]
        self.label = self.menu["Reload"]
        self.delay = GROWL_INTERVAL

        self.reload(None)

        self.notifier = GrowlNotifier(
            applicationName='Hatarake',
            notifications=['Nag'],
        )
        self.notifier.register()

    def alert(self, fmt, *args, **kwargs):
        self.notifier.notify(
            noteType='Nag',
            title=u"働け".encode('utf8', 'replace'),
            description=fmt.format(*args).encode('utf8', 'replace'),
            sticky=True,
            identifier=__file__,
            **kwargs
        )

        self.delay = hatarake.GROWL_INTERVAL
    @rumps.timer(1)
    def _update_clock(self, sender):
        now = datetime.datetime.now(pytz.utc).replace(microsecond=0)
        delta = now - self.when

        logger.debug('Pomodoro %s %s, %s', self.title, self.when, now)

        if delta.total_seconds() % self.delay == 0:
            self.alert(u'[{0}] was {1} ago', self.zname, delta)

        self.label.title = u'Last pomodoro [{0}] was {1} ago'.format(self.zname, delta)

        # If delta is more than a day ago, show the infinity symbol to avoid
        # having a super long label in our menubar
        if delta.days:
            delta = u'∞'
        self.title = u'働 {0}'.format(delta)

    @rumps.timer(300)
    def _update_calendar(self, sender):
        self.reload(None)

    def get_latest(self, calendar_url):
        result = requests.get(calendar_url, headers={'User-Agent': hatarake.USER_AGENT})
        cal = Calendar.from_ical(result.text)
        recent = None
        for entry in cal.subcomponents:
            if recent is None:
                recent = entry
                continue
            if 'DTEND' not in entry:
                continue
            if entry['DTEND'].dt > recent['DTEND'].dt:
                recent = entry
        return recent['SUMMARY'], recent['DTEND'].dt

    @rumps.clicked("Reload")
    def reload(self, sender):
        self.config = hatarake.config.Config(hatarake.CONFIG_PATH)
        self.zname, self.when = self.get_latest(self.config.config.get('feed', 'nag'))

    @rumps.clicked("Debug")
    def toggledebug(self, sender):
        sender.state = not sender.state
        if sender.state:
            self.delay = 5
            logging.getLogger().setLevel(logging.INFO)
            logging.info('Setting debugging to INFO and delay to %d', self.delay)
        else:
            logging.info('Setting debugging to WARNING and delay to %d', self.delay)
            logging.getLogger().setLevel(logging.WARNING)
            self.delay = hatarake.GROWL_INTERVAL

    @rumps.clicked("Report Issue")
    def issues(self, sender):
        webbrowser.open(hatarake.ISSUES_LINK)

if __name__ == "__main__":
    logging.basicConfig(level=logging.WARNING)
    Hatarake().run()

@@ -269,35 +269,22 @@
 ass 
-Hatarake(hatarake.shim.Shim
+Growler(object
 ):%0A 
@@ -319,235 +319,32 @@
    s
-uper(Hatarake, self).__init__(%22Hatarake%22)%0A        self.menu = %5B%22Reload%22, %22Debug%22, %22Report Issue%22%5D%0A        self.label = self.menu%5B%22Reload%22%5D%0A        self.delay = GROWL_INTERVAL%0A%0A        self.reload(None)%0A%0A        self.notifier = 
+elf.growl = gntp.config.
 Grow
@@ -427,17 +427,16 @@
 =%5B'Nag'%5D
-,
 %0A       
@@ -451,24 +451,21 @@
    self.
-notifier
+growl
 .registe
@@ -529,16 +529,13 @@
 elf.
-notifier
+growl
 .not
@@ -781,52 +781,324 @@
  )%0A%0A
-        self.delay = hatarake.GROWL_INTERVAL
+%0Aclass Hatarake(hatarake.shim.Shim):%0A    def __init__(self):%0A        super(Hatarake, self).__init__(%22Hatarake%22)%0A        self.menu = %5B%22Reload%22, %22Debug%22, %22Report Issue%22%5D%0A        self.label = self.menu%5B%22Reload%22%5D%0A        self.delay = hatarake.GROWL_INTERVAL%0A%0A        self.reload(None)%0A%0A        self.notifier = Growler()%0A
 %0A   
@@ -1394,16 +1394,25 @@
    self.
+notifier.
 alert(u'

Move growl code to common class