from AppKit import NSImage
from vanilla import *
from vanilla.dialogs import getFile
from mojo.extensions import ExtensionBundle

from mechanic.helpers import *
from mechanic.models import Extension, GithubRepo, Registry, Updates
from mechanic.windows.base import BaseWindow
from mechanic.tabs import *


class UpdateNotificationWindow(BaseWindow):
    window_title = "Extension Updates"

    explanation = "If you don't want to install now, choose Extensions > Mechanic > Updates when you're ready to install."
    no_updates_title = 'Mechanic'
    no_updates = 'All updateable extensions are up to date.'
    updates_available = "Updates are available for %d of your extensions."

    @classmethod
    def with_new_thread(cls):
        import threading
        threading.Thread(target=cls).start()

    @property
    def title(self):
        return Font.string(text=self.updates_available % len(self.updates),
                           style="bold")

    def __init__(self, force=False):
        super(UpdateNotificationWindow, self).__init__()

        print "Mechanic: checking for updates..."

        self.updater = Updates()
        self.updates = self.updater.all(force, skip_patch_updates=bool(Storage.get('ignore_patch_updates')))

        # TODO: Make this use exceptions
        if self.updater.unreachable:
            print "%s: Couldn't connect to the internet" % self.no_updates_title
            return

        if self.updates:
            self.create_image()

            self.w.title = TextBox((105,20,-20,20), self.title)
            self.w.explanation = TextBox((105,45,-20,50), 
                                         Font.string(text=self.explanation, size=11))

            self.w.updateButton = Button((-150,-40,130,20), "Install Updates",
                                         callback=self.update)
            self.w.cancelButton = Button((-255,-40,90,20), "Not Now",
                                         callback=self.cancel)
            self.w.showDetailsButton = Button((105,-40,110,20), "Show Details",
                                         callback=self.showDetails)
            self.w.setDefaultButton(self.w.updateButton)

            self.w.open()
        else:
            print "%s: %s" % (self.no_updates_title, self.no_updates)

    def cancel(self, sender):
        self.w.close()

    def update(self, sender):            
        ticks = len(self.updates) * Extension.ticks_per_download
        self.progress = self.startProgress('Updating', ticks)

        for extension in self.updates:
            extension.update()

        self.progress.close()

    def showDetails(self, sender):
        self.w.close()
        MechanicWindow("Update")

    def create_image(self):
        image = NSImage.imageNamed_("ExtensionIcon")
        self.w.image = ImageView((15,15,80,80), scale='fit')
        if image:
            self.w.image.setImage(imageObject=image)


class MechanicWindow(BaseWindow):
    window_title = "Mechanic"

    def __init__(self, *args, **kwargs):
        super(MechanicWindow, self).__init__(*args, **kwargs)

        self.toolbar.add_item(InstallTab)
        self.toolbar.add_item(UpdatesTab)
        self.toolbar.add_item(RegisterTab)
        self.toolbar.add_item(SettingsTab)

        self.open()

@@ -422,23 +422,22 @@
 want to 
-install
+update
  now, ch
@@ -512,60 +512,26 @@
     
-no_updates_title = 'Mechanic'%0A    no_up
+up_to_
 date
-s
  = 'All 
 upda
@@ -530,19 +530,8 @@
 All 
-updateable 
 exte
@@ -1287,18 +1287,24 @@
  print %22
-%25s
+Mechanic
 : Couldn
@@ -1334,32 +1334,8 @@
 net%22
- %25 self.no_updates_title
 %0A   
@@ -2163,18 +2163,24 @@
 nt %22
-%25s
+Mechanic
 : %25s%22 %25 
 (sel
@@ -2179,48 +2179,23 @@
 %22 %25 
-(
 self.
-no_updates_title, self.no_up
+up_to_
 date
-s)
 %0A%0A  

update strings