# -*- coding: iso-8859-15 -*-

from pyjamas.ui.SimplePanel import SimplePanel
from pyjamas.ui.Button import Button
from pyjamas.ui.RadioButton import RadioButton
from pyjamas.ui.Label import Label
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.TextBox import TextBox
from pyjamas.ui.DialogBox import DialogBox
from pyjamas import Window


class Pregunta:
    def __init__(self, texto, respuestas):
        self.texto = texto
        self.respuestas = respuestas
        self.opciones = {}

    def render(self):
        panel = VerticalPanel()
        
        pregunta = Label(self.texto) 
        pregunta.setStyleName("pregunta")
        panel.add(pregunta)

        for respuesta in self.respuestas:
            opcion = RadioButton(self.texto, respuesta)
            opcion.setStyleName("respuesta")
            panel.add(opcion)
            self.opciones[respuesta] =  opcion
        return panel


    def estaRespondida(self):
        respuesta = False 
        for opcion in self.opciones.values():
            if opcion.isChecked():
                respuesta = True
        return respuesta

    def evalua(self):        

        textoCorrecta = self.respuestas[0]
        correcta = self.opciones.get(textoCorrecta)

        if (correcta.isChecked()):
            return 1
        else:
            return -0.5




class Test:
    def __init__(self, preguntas):
        self.preguntas = preguntas

        
    def todoRespondido(self):
        resultado = True

        for pregunta in self.preguntas:
            if not pregunta.estaRespondida():
                resultado = False

        if self.campo_nombre.getText() == "":
            resultado = False

        return resultado


    def ventana(self, titulo, texto):
        contents = VerticalPanel()
        contents.setSpacing(4)
        contents.add(HTML(texto))
        contents.add(Button("Cerrar", getattr(self, "onClose")))
        contents.setStyleName("Contents")

        self._dialog = DialogBox()
        self._dialog.setHTML("<b>"+titulo+"</b>")
        self._dialog.setWidget(contents)

        left = (Window.getClientWidth() - 200) / 2
        top = (Window.getClientHeight() - 100) / 2
        self._dialog.setPopupPosition(left, top)
        self._dialog.show()

    def onClose(self):
        self._dialog.hide()



    def evalua(self, sender):
        
        if self.todoRespondido():
            puntos = 0
            for pregunta in self.preguntas:
                puntos = puntos + pregunta.evalua()
            self.ventana("Resultado", "Hola, " + self.campo_nombre.getText() +", tu puntuación es:  " + str(puntos))
        else:
            self.ventana("¡Atención!", "Faltan campos por rellenar")


    def render(self):

        RootPanel().add(HTML("<h1> Examen </h1>"))
        
        panelNombre = HorizontalPanel()
        panelNombre.add(Label("Nombre: "))
        self.campo_nombre = TextBox()
        self.campo_nombre.setVisibleLength(30)
        self.campo_nombre.setMaxLength(30)
        panelNombre.add(self.campo_nombre)

        RootPanel().add(panelNombre)
                        


        for pregunta in self.preguntas:
            RootPanel().add(pregunta.render())

        botones = HorizontalPanel()
        botones.setStyleName("botones")
        boton = Button("Finalizar", getattr(self,"evalua"))
        boton.setStyleName("boton")
        botones.add(boton)
        RootPanel().add(botones)




if __name__ == '__main__':

    preguntas = [ Pregunta("¿Cuánto son 2 + 2?", 
                           ["4","5","6"],
                           1,
                           -0.5),
                  Pregunta("¿Cómo se llama el creador de Linux?", 
                           ["Linus Torvalds","Gustavo Gates","Linux Finux",],
                           1,
                           -0.5),
                  ]
    
    test = Test(preguntas)
    test.render()
