'''
Action base module
'''

import os
import web
import config

DEFAULT_LANG = config.langs[0]

class Action:
    def __init__(self, plugin):
        self.owner = plugin
        self.name = "unnamed"
        self.human_name = "unnamed"
        self.description = "undescripted"
        self.appear_in_main_panel = False

    def get_image(self, size):
        dpath = 'images/%s_action.png' % size
        path = "plugins/%s/%s_%s.png" %  (self.owner.name, size, self.name)
        fpath = os.path.join(config.staticdir, path)
        if os.path.exists(fpath):
            path = config.static + '/' + path
        else:
            path = config.static + '/' + dpath

        return path
    def get_small_image(self):
        return self.get_image('small')

    def get_big_image(self):
        return self.get_image('big')

    def get_human_name(self, lang=DEFAULT_LANG):
        try:
            lang = web.ctx.session.lang
        except:
            pass

        # TODO return the correct language
        return self.human_name


    def get_description(self, lang=DEFAULT_LANG):
        try:
            lang = web.ctx.session.lang
        except:
            pass

        # TODO return the correct language
        return self.description

    def get_url(self):
        return '/plugin/%s/action/%s' % (self.owner.name, self.name)


class ActionInstance(Action):
    '''
    It's like an action, but it has an specific url
    '''
    def __init__(self, action, url):
        self.owner = action.owner
        self.name = action.name
        self.human_name = action.human_name
        self.description = action.description
        self.appear_in_main_panel = action.appear_in_main_panel
        self.url = url

    def get_url(self):
        return self.url
