# -*- coding: utf-8 -*-

import gtk
import adesk.core as Core

ICON_THEME = Core.ICON_THEME

class Plugin(gtk.EventBox):
    def __init__(self, app, settings):
        gtk.EventBox.__init__(self)
        self.set_visible_window(False)
        self.app = app
        self.cfg = app.cfg
        self.settings = settings
        self.index = None

        self.pixbuf = None
        self.pixbuf_zoom = None
        self.current_icon = None

        self.can_zoom = False
        self.can_show_icon = True

        self.is_visible = False
        self.tooltip = ''
        self.has_tooltip = True
        self.show()

    def set_tooltip(self, tooltip):
        self.tooltip = tooltip
        self.app.tooltip.set_tooltip(tooltip)
        self.app.tooltip.reposition(self)

    def get_tooltip(self, tooltip):
        return self.tooltip

    def set_icon(self, path_icon, is_separator=False):
        #~ print 'set_icon :', path_icon
        if path_icon == '' or self.current_icon == path_icon:
            return

        w = h = self.cfg['icon_size']
        self.pixbuf = None

        if is_separator:
            if self.cfg['position'] == 'bottom' or self.cfg['position'] == 'top':
                w = int(w/4)
            else:
                h = int(h/4)

        if not path_icon:
            self.pixbuf = Core.pixbuf_from_file('images/def_icon.png', w, h)
            self.pixbuf_zoom = Core.pixbuf_from_file('images/def_icon.png', self.app.zoom_size, self.app.zoom_size)
            self.current_icon = 'images/def_icon.png'
            
        ## real path .. load from file
        elif path_icon[0] == '/' or ( len(path_icon) > 7 and path_icon[:7] == 'images/'):
            self.pixbuf = Core.pixbuf_from_file(path_icon, w, h)
            self.pixbuf_zoom = Core.pixbuf_from_file(path_icon, self.app.zoom_size, self.app.zoom_size)
            self.current_icon = path_icon
            
        ## load from icon theme
        elif ICON_THEME.has_icon(path_icon):
            self.pixbuf = ICON_THEME.load_icon(path_icon, self.cfg['icon_size'], gtk.ICON_LOOKUP_USE_BUILTIN)
            self.pixbuf_zoom = ICON_THEME.load_icon(path_icon, int(self.app.zoom_size), gtk.ICON_LOOKUP_USE_BUILTIN)
            self.current_icon = path_icon


        ## check if pixbuf is ok
        if not self.pixbuf:
            self.pixbuf = Core.pixbuf_from_file('images/def_icon.png', w, h)
            self.pixbuf_zoom = Core.pixbuf_from_file('images/def_icon.png', self.app.zoom_size, self.app.zoom_size)
            self.current_icon = 'images/def_icon.png'

        if self.app.init_flag:
            self.app.update()

    def draw(self, cr):
        pass

    def resize(self):
        pass

    def onClick(self, widget, event):
        pass

    def init_position(self):

        x, y, width, height = self.get_allocation()
        x0, y0 = x, y

        size_press = int(self.cfg['icon_size'] * 0.9)

        zoom_size = int(self.cfg['icon_size'] * self.cfg['zoom_factor'])
        zoom_size_press = int(self.cfg['icon_size'] * self.cfg['zoom_factor']*0.9)

        zoom_offset1 =  int((zoom_size - self.cfg['icon_size'])/2.0)
        zoom_offset2 = int((self.cfg['icon_size'] + self.cfg['padding']/2.0) -  zoom_size)

        offset_click = (self.cfg['icon_size'] - self.cfg['icon_size']* 0.9)/2.0

        offset_click1 = int((self.cfg['icon_size'] * self.cfg['zoom_factor'] *0.9 - self.cfg['icon_size'])/2.0)
        offset_click2 = int((self.cfg['icon_size'] + self.cfg['padding']/2.0) -  (self.cfg['icon_size'] * self.cfg['zoom_factor'] *0.9))

        if self.cfg['position'] == "top":
            x1 = x - zoom_offset1
            y1 = self.cfg['padding']/2

            y0 = self.cfg['padding']/2
            x0 = x - offset_click1

        elif self.cfg['position'] == "bottom":
            y1 = y + zoom_offset2  ## zoom_offset2 est négatif !! => pas de soustraction

            y1 = self.app.bar_height - ( self.cfg['padding']/2.0 + self.cfg['icon_size'] * self.cfg['zoom_factor'] )
            x1 = x - zoom_offset1

            y0 = self.app.bar_height - ( self.cfg['padding']/2.0 + self.cfg['icon_size'] * self.cfg['zoom_factor']*0.9 )
            x0 = x - offset_click1

        elif self.cfg['position'] == "left":
            y1 = y - zoom_offset1
            x1 = self.cfg['padding']/2

            x0 = self.cfg['padding']/2 # self.app.bar_width - ( self.cfg['padding']/2.0 + self.cfg['icon_size'] * self.cfg['zoom_factor']*0.9 )
            y0 = y - offset_click1

        elif self.cfg['position'] == "right":
            y1 = y - zoom_offset1
            x1 = x +  zoom_offset2 ## 2*zoom_offset1 + self.cfg['padding']/2

            x0 = self.app.bar_width - ( self.cfg['padding']/2.0 + self.cfg['icon_size'] * self.cfg['zoom_factor']*0.9 )
            y0 = y - offset_click1

        self.x = x
        self.y = y
        self.x0 = x0
        self.y0 = y0
        self.x1 = x1
        self.y1 = y1
        self.offset_click = offset_click
        self.offset_click1 = offset_click1
        self.offset_click2 = offset_click2
        self.zoom_offset1 = zoom_offset1
        self.zoom_offset2 = zoom_offset2
        self.zoom_size = zoom_size
        self.zoom_size_press = zoom_size_press
