from os.path import exists


from pyalp.skin import get_skin


class Module(object):
    # stores data for a single module
    # var _name, _loc, _str, _link, _sec, _isSlim, _isOpen, _type

    def __init__(self, n='', l='main', sec=1,
                 st='', sl=0, link='', module_type=''):
        # constructor
        self.name = n
        self.loc = l
        self.str = st
        self.link = link
        self.sec = sec
        self.isSlim = sl
        self.isOpen = 1
        self.module_type = module_type

        self.skin = get_skin()

    # display module got re-implemented as template tag

    def get_inner_width(self):
        if self.loc != 'main':
            sides = ['tl', 'tr', 'bl', 'br']

            # the dims array in this case only stores widths
            dims = {}
            for side in sides:
                key = 'mod' + side
                if exists(self.skin.skin_name + 'mod'+side+'.gif'):
                    temp = self.skin.getimagesize(
                        self.skin.skin_name + key + '.gif'
                    )
                    dims[key] = temp[0]
                else:
                    dims[key] = self.skin.container['border_width']

            extra = []
            if dims['modtl'] > dims['modbl'] or dims['modtl'] > dims['modbl']:
                extra[0] = max(dims['modtl'], dims['modbl'])
            else:
                extra[0] = dims['modtl']

            if dims['modtr'] > dims['modbr'] or dims['modtr'] > dims['modbr']:
                extra[1] = max(dims['modtr'], dims['modbr'])
            else:
                extra[1] = dims['modtr']

            px = (
                self.skin.container[self.loc + 'module'] - 2 *
                self.skin.container['horizontalmodulepadding'] - extra[0] -
                extra[1] - 10
            )
            return '{}px'.format(px)
        else:
            return '96%'

    def is_module_secure(self):
        return (self.current_security_level() >= self.get_security())


class ModuleManager(object):
    """
    manages a collection of modules
    """

    def __init__(self):
        self.leftModules = []
        self.rightModules = []
        self.mainModules = []

        self.skin = get_skin()

    def add_module(self, n='', l='', sec=0, st='', sl=0, link='', type=''):
        # adds a module to the manager
        mod = Module(n, l, sec, st, sl, link, type)

        if l == 'left':
            self.leftModules.append(mod)

        if l == 'right':
            self.rightModules.append(mod)

        if l == 'main':
            self.mainModules.append(mod)

    def display_all_modules(self, side):
        raise Exception('Use the appropriate method directly')

    def display_modules_right(self):
        return self.display_modules_for_side(
            self.rightModules,
            'right',
            self.skin.container['rightmodule']
        )

    def display_modules_main(self):
        return self.display_modules_for_side(
            self.mainModules,
            'main'
        )

    def display_modules_left(self):
        return self.display_modules_for_side(
            self.leftModules,
            'left',
            self.skin.container['leftmodule']
        )

    def display_modules_for_side(self, modules, side, width_modules=0):
        "displays all the modules for a side."

        if modules:
            types = {
                'left': 0,
                'right': 0,
                'main': 0
            }

            for mod in modules:
                types[mod.get_type()] += 1

            context = {
                'modules': self,
                'width_modules': width_modules
            }

            return render_to_string('display_all_modules.html', context)
        elif side != 'main':
            node = SpacerNode(
                self.skin.container['horizontalpadding']
            )
            return node.render({})
        else:
            return ''

    def get_width(self, side=None):
        if self.rightModules:
            right = (
                self.skin.container['rightmodule'] + 2 *
                self.skin.container['horizontalpadding']
            )
        else:
            right = self.skin.container['horizontalpadding']

        if self.leftModules:
            left = (
                self.skin.container['leftmodule'] + 2 *
                self.skin.container['horizontalpadding']
            )
        else:
            left = self.skin.container['horizontalpadding']

        output = {
            'right': right,
            'left': left
        }

        # for templating reasons
        if side:
            return output[side]
        else:
            return output

@@ -19,17 +19,22 @@
 t exists
-%0A
+, join
 %0A%0Afrom p
@@ -860,16 +860,17 @@
  + side%0A
+%0A
         
@@ -881,59 +881,20 @@
     
-if exists(self.skin.skin_name + 'mod'+side+'.gif'):
+path = join(
 %0A   
@@ -913,15 +913,8 @@
     
- temp =
  sel
@@ -924,21 +924,39 @@
 kin.
-getimagesize(
+asset_path, 'img', key + '.gif'
 %0A   
@@ -964,32 +964,35 @@
             
+)%0A%0A
         
 self.skin.sk
@@ -983,63 +983,91 @@
     
-self.skin.skin_name + key + '.gif'%0A                    
+        if exists(path):%0A                    width, _ = self.skin.getimagesize(path
 )%0A  
@@ -1100,15 +1100,13 @@
 %5D = 
-temp%5B0%5D
+width
 %0A   
@@ -1316,22 +1316,24 @@
    extra
-%5B0%5D = 
+.append(
 max(dims
@@ -1357,16 +1357,17 @@
 modbl'%5D)
+)
 %0A       
@@ -1402,14 +1402,16 @@
 xtra
-%5B0%5D = 
+.append(
 dims
@@ -1419,16 +1419,17 @@
 'modtl'%5D
+)
 %0A%0A      
@@ -1522,22 +1522,24 @@
    extra
-%5B1%5D = 
+.append(
 max(dims
@@ -1563,16 +1563,17 @@
 modbr'%5D)
+)
 %0A       
@@ -1608,14 +1608,16 @@
 xtra
-%5B1%5D = 
+.append(
 dims
@@ -1625,16 +1625,17 @@
 'modtr'%5D
+)
 %0A%0A      

Make get_inner_width actually work