from __future__ import unicode_literals
from decorators import pi18n
import web

from clientjob.model import Job
from ..booting.jobs import ShutdownJob, RestartJob, BootJob
from main.model import Computer, OrganizativeUnit


class BootView:
    def error(self, msg):
        web.ctx.notify(msg, 'error')
        raise web.seeother(web.ctx.environ.get('HTTP_REFERER', '/'))

    @pi18n
    def GET(self, entity_type, name):
        orm = web.ctx.orm
        if entity_type == 'computer':
            computer = orm.query(Computer).filter(Computer.name == name).first()
            if not computer:
                self.error(_('Computer "%s" not found') % name)
            job = BootJob(computer)
            job.send()

        elif entity_type == 'ou':
            ou = orm.query(OrganizativeUnit).filter(OrganizativeUnit.name == name).first()
            if not ou:
                self.error(_('Organizative Unit "%s" not found') % name)
            parent_job = Job()
            for computer in ou.get_all_descendant_computers():
                if computer.online:
                    continue
                job = BootJob(computer, parent_job)
            if not parent_job.children:
                self.error(_('Organizative Unit "%s" has no offline children') % name)
            parent_job.send()
        else:
            self.error(_('This command is only for computers or ous'))

        web.ctx.notify(_('Boot command sent to %s') % name, 'notification')
        raise web.seeother('/navigator/%s/%s' % (entity_type, name))

class ShutdownView:
    def error(self, msg):
        web.ctx.notify(msg, 'error')
        raise web.seeother(web.ctx.environ.get('HTTP_REFERER', '/'))

    @pi18n
    def GET(self, entity_type, name):
        orm = web.ctx.orm
        if entity_type == 'computer':
            computer = orm.query(Computer).filter(Computer.name == name).first()
            if not computer:
                self.error(_('Computer "%s" not found') % name)
            job = ShutdownJob()
            job.send(computer)

        elif entity_type == 'ou':
            ou = orm.query(OrganizativeUnit).filter(OrganizativeUnit.name == name).first()
            if not ou:
                self.error(_('Organizative Unit "%s" not found') % name)

            parent_job = ShutdownJob()
            for computer in ou.get_all_descendant_computers():
                if not computer.online:
                    continue
                job = ShutdownJob(parent_job)
            if not parent_job.children:
                self.error(_('Organizative Unit "%s" has no online children') % name)
            parent_job.send()
        else:
            self.error(_('This command is only for computers or ous'))
        web.ctx.notify(_('Shutdown command sent to %s') % name, 'notification')
        raise web.seeother('/navigator/%s/%s' % (entity_type, name))


class RestartView:
    def error(self, msg):
        web.ctx.notify(msg, 'error')
        raise web.seeother(web.ctx.environ.get('HTTP_REFERER', '/'))

    @pi18n
    def GET(self, entity_type, name):
        orm = web.ctx.orm
        if entity_type == 'computer':
            computer = orm.query(Computer).filter(Computer.name == name).first()
            if not computer:
                self.error(_('Computer "%s" not found') % name)
            job = RestartJob()
            job.send(computer)

        elif entity_type == 'ou':
            ou = orm.query(OrganizativeUnit).filter(OrganizativeUnit.name == name).first()
            if not ou:
                self.error(_('Organizative Unit "%s" not found') % name)

            parent_job = RestartJob()
            for computer in ou.get_all_descendant_computers():
                if computer.online:
                    pass
                job = RestartJob(parent_job)
                job.computer = computer
            if not parent_job.children:
                self.error(_('Organizative Unit "%s" has no online children') % name)
            parent_job.send()
        else:
            self.error(_('This command is only for computers or ous'))
        web.ctx.notify(_('Restart command sent to %s') % name, 'notification')
        raise web.seeother('/navigator/%s/%s' % (entity_type, name))
