import logging

from pylons import request, response, session, tmpl_context as c, url, config, app_globals
from pylons.controllers.util import abort, redirect

from ppdi.lib.base import BaseController, render
from ppdi.model.meta import Session
from ppdi.model import ModeratorPin, ParticipantPin

from xml.dom.minidom import parseString

log = logging.getLogger(__name__)

from paste.deploy.converters import asbool       

class MonitorController(BaseController):
    
    requires_auth = True

    def index(self, id):
        self.is_my_pin(id)
        c.id = id
        c.mod_pin = None
        c.part_pin = None
        try:
            conf = Session.query(ModeratorPin).filter(ModeratorPin.pin == id).one()
            c.mod_pin = conf.pin
            c.part_pin = conf.participant_pin.pin
        except Exception, e:
            log.exception('Could not get conference pin.')
            
            
        return render('/derived/main/monitor.html')
        
    def getConferenceStatus(self, id):
        self.is_my_pin(id)
        c.pin = id
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            return "<b>There is no one in this conference.</b>"
    
        return render('/component/main/monitor.html')

    def toggleMute(self, id, mid):
        """Mute a specific member"""
        self.is_my_pin(id)
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            log.critical("Conference %s not on dict." % id)
        else:
            confMonitor.conferences[id].toggleMute(mid)
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))
        
    def toggleDeaf(self, id, mid):
        """Mute a specific member"""
        self.is_my_pin(id)
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            log.critical("Conference %s not on dict." % id)
        else:
            confMonitor.conferences[id].toggleDeaf(mid)
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))
        
    def kickMember(self, id, mid):
        """Kick a specific member"""
        self.is_my_pin(id)
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            log.critical("Conference %s not on dict." % id)
        else:
            confMonitor.conferences[id].kick(mid)
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))
        
    def muteAll(self, id):
        """Mute all users but the moderator"""
        self.is_my_pin(id)
        app_globals.confMonitor.muteAllButModToggle(id)
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))

    def unmuteAll(self, id):
        """Mute all users but the moderator"""
        self.is_my_pin(id)
        app_globals.confMonitor.muteAllButModToggle(id)
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))
            
    def lockToggle(self,id):
        """Lock toggle the conference"""
        self.is_my_pin(id)
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            log.critical("Conference %s not on dict." % id)
        else:
            confMonitor.conferences[id].lockToggle()
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))
            
    def kickAll(self, id):
        """Kick all in the conference"""
        self.is_my_pin(id)
        confMonitor = app_globals.confMonitor
        if not confMonitor.conferences.has_key(id):
            log.critical("Conference %s not on dict." % id)
        else:
            confMonitor.conferences[id].endConference()
        redirect(request.headers.get('REFERER', url(controller='main/conferences', action='index')))