'''
Disk Image Module
'''

from __future__ import unicode_literals
from ..pluginbase import PluginBase
from ..action import ActionInstance
from main.model import Computer
import web
import views
import config
from jobs import DiskCurrentStateJob

class Plugin(PluginBase):
    '''
    This plugin can save a partition of a computer in a image file.
    This image will be uploaded to a repo. Later you can use those
    images to others computers.
    '''

    def enable(self):
        self.urls = ('images',
                views.AdminImages,
                'action/currentstate/(.*)',
                views.CurrentState,
                'action/adminimages/',
                views.AdminImages,
                'currentstate/computer/(\w+)',
                views.CurrentState,
                'newdiskprofile',
                views.NewDiskProfile,
                'edit/diskprofile/id/(\w+)',
                views.EditDiskProfile,
                'view/diskstate/(\w+)',
                views.DiskState,
                'edit/image/(name|id)/(\w+)',
                views.EditImage)

    def create_new_current_state(self, computer_id):
        '''
        Creates a new job to know the disk state of the given computer.
        Returns the id of the created job.
        '''
        job = DiskCurrentStateJob()
        job.send(computer_id)
        return job.id

    def get_action_instances_for_view(self, view_path, args):
        '''
        Reimplementing function to do more things.
        '''
        if view_path == 'navigator.navigator.NavigatorComputer':
            # Getting the computer
            computer_name = args[0]

            orm = web.ctx.orm
            computer = orm.query(Computer).filter(
                    Computer.name == computer_name).first()

            if not computer:
                return []

            functions = []

            # FIXME hacking momentaneo
            # if computer.online:
            functions.append(ActionInstance(self.actions['currentstate'],
                    self.fill_url(views.CurrentState, computer.name)))

            '''
            if computer.diskprofile:
                functions.append(ActionsInstance(
                    self.actions['viewdiskprofile'],
                    self.get_view_disk_profile_by_computer_url(computer.diskprofile.id)))

            else:
                functions.append(ActionsInstance(
                    self.actions['creatediskprofile'],
                    self.get_create_disk_profile_url()))
                functions.append(ActionsInstance(
                    self.actions['associatediskprofile'],
                    self.get_associate_disk_profile_url()))
            '''

        return []
