# -*- coding: latin-1 -*-

#==========================================================================================================================================
import operator

from PySide import QtGui

from Code.Traducir import tr

import Code.Tutoriales as Tutoriales

import QTUtil2
import QTVarios
import Colocacion
import Iconos
import Controles
import Columnas
import Grid

import PantallaTTFlechas
import PantallaTTMarcos
# import PantallaTTPartidas
import PantallaTTVariables
import PantallaTTEscenas
import PantallaMotores
#==========================================================================================================================================

#==========================================================================================================================================
class WET_DatosBasicos( QtGui.QWidget ):

    def __init__( self, owner ) :

        super(WET_DatosBasicos, self).__init__(owner)

        self.owner = owner
        self.tutorial = owner.tutorial
        self.configuracion = owner.procesador.configuracion

        # Autor
        lbAutor = Controles.LB( tr( "AUTOR" ) + " :" )
        self.edAutor = Controles.ED( self, self.tutorial.autor ).anchoMinimo( 100 )

        # Descripción
        lbDescripcion = Controles.LB( tr( "TUT_DESCRIPCION" ) + " :" )
        self.edDescripcion = Controles.ED( self, self.tutorial.descripcion ).anchoMinimo( 400 )

        # Tamaño del tablero
        liTams = ( ( tr( "TAM_MUY_GRANDE" ), 80 ), ( tr( "TAM_GRANDE" ), 64 ), ( tr( "TAM_MEDIANO" ), 48 ), \
                    ( tr( "TAM_MEDIANO_PEQUEÑO" ), 32 ), ( tr( "TAM_PEQUEÑO" ), 24 ), ( tr( "TAM_MUY_PEQUEÑO" ), 16 ) )
        tam = self.tutorial.tamTablero
        if tam == 0 :
            tam = self.configuracion.cTableroN.anchoPieza
        self.cbTam, lbTam = QTUtil2.comboBoxLB( liTams, tam, tr( "TABLERO_NORMAL_TAM" ) )
        self.cbTam.currentIndexChanged[int].connect( self.cambiaTamTablero )

        # Layout
        layout = Colocacion.G().controld(lbAutor, 0, 0).control(self.edAutor, 0, 1 )
        layout.controld(lbDescripcion, 1, 0).control( self.edDescripcion, 1, 1, 1, 3 )
        layout.controld(lbTam, 2, 0).control( self.cbTam, 2, 1 )

        ly = Colocacion.V().relleno(1).otro(layout).relleno(1)
        self.setLayout( ly )

    def tamTablero( self ) :
        return self.cbTam.resultado()

    def cambiaTamTablero( self, num ) :
        self.owner.escena.compruebaTablero()

#==========================================================================================================================================
class WET_Recursos( QtGui.QWidget ):

    def __init__( self, owner ) :

        QtGui.QWidget.__init__( self, owner)

        # Tabs
        tab = QtGui.QTabWidget()
        # tab.setTabPosition( tab.South )
        tab.setTabShape( tab.Triangular )
        tab.currentChanged[int].connect( self.tabCambiada )

        self.owner = owner

        # owner.partidas = PantallaTTPartidas.WET_Partidas( self, owner )
        # tab.addTab( owner.partidas, tr( "TUT_PARTIDAS") )
        # owner.registraGrid( owner.partidas.grid )

        owner.flechas = PantallaTTFlechas.WET_Flechas( self, owner, owner.datosBasicos.tamTablero ) # se pasa la función
        tab.addTab( owner.flechas, tr( "TUT_TALLERFLECHAS") )
        owner.registraGrid( owner.flechas.grid )

        owner.marcos = PantallaTTMarcos.WET_Marcos( self, owner, owner.datosBasicos.tamTablero ) # se pasa la función
        tab.addTab( owner.marcos, tr( "TUT_TALLERMARCOS") )
        owner.registraGrid( owner.marcos.grid )

        owner.variables = PantallaTTVariables.WET_Variables( self, owner )
        tab.addTab( owner.variables, tr( "TUT_VARIABLES_GLOBALES") )
        owner.registraGrid( owner.variables.grid )

        # Layout
        layout = Colocacion.V().control(tab)
        self.setLayout( layout )

    def tabCambiada( self, pos ) :
        if pos == 2 :
            self.owner.variables.grid.refresh()

#==========================================================================================================================================
class WEdicionTutorial( QTVarios.WDialogo ):

    def __init__( self, owner, tutorial ) :

        titulo = tr( "TUT_CONTROL" )
        icono = Iconos.Tutorial()
        extparam = "tutorial1"
        QTVarios.WDialogo.__init__(self,owner, titulo, icono, extparam )
        self.procesador = owner.procesador
        self.tutorial = tutorial

        # Toolbar
        liAcciones = [  ( tr( "ME_GRABAR" ), Iconos.TB_Aceptar(), "grabar" ), None,
                        ( tr( "CANCELAR" ), Iconos.TB_Cancelar(), "reject" ), None,
                        ( tr( "TUT_PLAY" ), Iconos.TutorialesPlay(), "play" ), None,
                    ]
        tb = Controles.TB( self, liAcciones )

        # Tabs
        self.tab = tab = QtGui.QTabWidget()

        # Apartados
        self.datosBasicos = WET_DatosBasicos( self )
        tab.addTab( self.datosBasicos, tr( "TUT_DATOSBASICOS") )

        self.recursos = WET_Recursos( self )
        tab.addTab( self.recursos, tr( "TUT_RECURSOS") )

        self.escenas = PantallaTTEscenas.WET_Escenas( self )
        tab.addTab( self.escenas, tr( "TUT_ESCENAS") )
        self.registraGrid( self.escenas.gridEscenas )
        self.registraGrid( self.escenas.gridTareas )

        # Layout
        layout = Colocacion.V().control(tb).control(tab)
        self.setLayout( layout )

        self.recuperarVideo()

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        accion = self.sender().clave
        eval( "self.%s()"%accion )

    #--------------------------------------------------------------------------------------------------------------------------------
    def grabar( self ) :
        self.guardarVideo()
        self.accept()

    #--------------------------------------------------------------------------------------------------------------------------------
    def refreshFlechas( self ) :
        self.flechas.grid.gotop()
        self.flechas.grid.refresh()
#==========================================================================================================================================
class WTutoriales( QTVarios.WDialogo ):

    def __init__( self, procesador ) :

        self.procesador = procesador

        self.leerTutoriales()

        titulo = tr( "TUTORIALES" )
        icono = Iconos.Tutoriales()
        extparam = "tts"

        super(WTutoriales, self).__init__(procesador.pantalla, titulo, icono, extparam)

        # Toolbar
        liAcciones = [  ( tr( "TB_TERMINAR" ), Iconos.TB_FinPartida(), "terminar" ), None,
                        ( tr( "TUT_PLAY" ), Iconos.TutorialesPlay(), "play" ), None,
                        ( tr( "TUT_CREAR" ), Iconos.TutorialesCrear(), "crear" ), None,
                        ( tr( "MODIFICAR" ), Iconos.TB_Modificar(), "modificar" ), None,
                        ( tr( "BORRAR" ), Iconos.TB_Borrar(), "borrar" ), None,
                        ( tr( "COPIAR" ), Iconos.TB_Copiar(), "copiar" ), None,
                    ]
        tb = Controles.TB( self, liAcciones )

        # Lista
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "DESCRIPCION", tr( "TUT_DESCRIPCION" ), 280 )
        oColumnas.nueva( "AUTOR", tr( "AUTOR" ), 140 )
        oColumnas.nueva( "CLAVE", tr( "TUT_CLAVE" ), 100 )

        self.grid = Grid.Grid( self, oColumnas )
        self.registraGrid( self.grid )

        # Layout
        layout = Colocacion.V().control(tb).control(self.grid)
        self.setLayout( layout )

        if not self.recuperarVideo() :
            self.resize( self.grid.anchoColumnas() + 34, 430 )

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        accion = self.sender().clave
        if accion == "terminar" :
            self.guardarVideo()
            self.accept()
        elif accion == "play" :
            self.play()
        elif accion == "crear" :
            self.crear()

    #--------------------------------------------------------------------------------------------------------------------------------
    def play( self ) :
        pass

    #--------------------------------------------------------------------------------------------------------------------------------
    def crear( self ) :
        t = Tutoriales.Tutorial()
        w = WEdicionTutorial(self, t)
        if w.exec_() :
            self.liTutoriales.append(t)
            # self.grabar()

    #--------------------------------------------------------------------------------------------------------------------------------
    def leerTutoriales( self ) :
        self.liTutoriales = []

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridNumDatos( self, grid ) :
        return len(self.liTutoriales)

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDato( self, grid, fila, oColumna ) :
        claveT, descripcion, autor = self.liTutoriales[fila]
        clave = oColumna.clave
        if clave == "CLAVE" :
            return claveT
        elif clave == "DESCRIPCION" :
            return descripcion
        elif clave == "AUTOR" :
            return autor

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDobleClick( self, grid, fil, col ) :
        self.play()

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDobleClickCabecera( self, grid, oColumna ) :
        clave = oColumna.clave
        if clave == "ALIAS" :
            clave = "alias"
        elif clave == "MOTOR" :
            clave = "idName"
        else :
            return
        self.listaMotores.liMotores.sort(key=operator.attrgetter(clave))
        self.grid.refresh()
        self.grid.gotop( )

    #--------------------------------------------------------------------------------------------------------------------------------
    def modificar( self ) :
        if len(self.listaMotores.liMotores) :
            fila = self.grid.recno()
            if fila >= 0:
                me = self.listaMotores.liMotores[fila]
                # Editamos, y graba si hace falta
                w = PantallaMotores.WMotor( self, self.listaMotores, me )
                if w.exec_() :
                    self.grid.refresh()

    #--------------------------------------------------------------------------------------------------------------------------------
    def borrar( self ) :
        fila = self.grid.recno()
        if fila >= 0:
            if QTUtil2.pregunta( self, tr( "ME_BORRAMOS" ) ) :
                del self.listaMotores.liMotores[fila]
                self.grid.refresh()

    #--------------------------------------------------------------------------------------------------------------------------------
    def copiar( self ) :
        fila = self.grid.recno()
        if fila >= 0:
            me = self.listaMotores.liMotores[fila].copiar()
            w = PantallaMotores.WMotor( self, self.listaMotores,  me )
            if w.exec_() :
                self.listaMotores.nuevo( me )
                self.grid.refresh()
                self.grid.gobottom( 0 )

