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

DEBUG=False

from PySide import QtCore, QtGui

from Code.Traducir import tr
from Code.Constantes import *

if DEBUG :
    prlk( "Info "* 20 )

import random

import Colocacion
import Iconos
import Controles

#==========================================================================================================================================
class WAbout( QtGui.QDialog ):
    def __init__(self, procesador )  :
        super(WAbout, self).__init__(procesador.pantalla)

        self.setWindowTitle( tr( "TIT_ACERCADE" ) )
        self.setWindowIcon( Iconos.Aplicacion() )
        self.setWindowFlags( QtCore.Qt.Dialog | QtCore.Qt.WindowTitleHint )

        f = Controles.TipoLetra( puntos=10 ) #0, peso=75 )

        cabecera = '<span style="font-size:30px; font-weight="700"; font-family:arial; color:#2D2B2B">%s</span><br>'%tr( "El ajedrez de Lucas" )
        cabecera += '<span style="font-size:15px;">%s</span><br>'%tr( "VERSION", procesador.version )
        cabecera += '<span style="font-size:10px;color:2D2B2B">%s</span>'%"Lucas Monge"
        cabecera += ' - <a style="font-size:10px; color:2D2B2B" href="%s">%s</a>'%(procesador.web, procesador.web)
        cabecera += ' - <a style="font-size:10px; color:2D2B2B" href="%s">Blog : Fresh news</a><br>'%(procesador.blog, )
        cabecera += '%s <a style="font-size:10px; color:2D2B2B" href="http://www.gnu.org/copyleft/gpl.html"> GPL</a>'%tr( "LICENCIA" )

        lbIco = Controles.LB().ponImagen( Iconos.pmAplicacion64() )
        lbTitulo = Controles.LB(cabecera)
        btSeguir = Controles.PB( self, tr( "SEGUIR" ), self.accept ).ponPlano( False )

        if DEBUG :
            fdbg = open( "../setup/web/recursos_%s.txt"%procesador.configuracion.traductor, "wb" )
            fdbg.write( "<br />"*3 )

        # Tabs
        tab = QtGui.QTabWidget()
        tab.setFont( f )

        def masTab( titulo, txt ) :
            if "href=" in txt :
                txt = txt.replace( "href=", 'style="color:#3D4667" href=')
            lb =  Controles.LB(txt)
            tab.addTab( lb, titulo )
            if DEBUG :
                txt = """<h3>%s</h3><div class="m40">%s</div><br/>"""%( titulo, txt.replace("<hr>", "") )
                fdbg.write( txt )

        # Colaboradores
        li = [
                "Rui Grafino",
                "Georg Pfefferle",
                "Lolo S.",
                "Joaquín Alvarez",
                "bolokay",
                "Gianfranco Cutipa",
                "Daniel Trebejo",
                "Carmen Martínez",
            ]

        txt = "<hr><ul>"
        for uno in li :
            txt += "<li><b>%s</b></li>"%uno
        txt += "</ul><hr>"
        masTab( tr( "ABOUT_COLABORADORES"), txt)

        # Traducciones
        dic = {
                "ABOUT_TRADUCCIONES_PORTUGUES": "Rui Grafino",
                "ABOUT_TRADUCCIONES_FRANCES": "Lolo S.",
                "ABOUT_TRADUCCIONES_RUSO": "Slavik Pavlov",
                "ABOUT_TRADUCCIONES_ALEMAN":"Georg Pfefferle",
                "ABOUT_TRADUCCIONES_INGLES":"Georg Pfefferle, Lolo S., bolokay, Lucas",
            }
        li = dic.keys()
        random.shuffle( li )
        txt = "<hr><ul>"
        for uno in li :
            txt += "<li>%s : <b>%s</b></li>"%( tr(uno), dic[uno] )
        txt += "</ul><hr>"
        masTab( tr( "ABOUT_TRADUCCIONES"), txt)

        # Imagenes
        txt ='<hr><center><table border="0" cellpadding="5" cellspacing="5" >'

        txt += "<tr>"
        txt += "<td></td>"
        txt += "<td><b>%s</b></td>"%tr( "AUTOR" )
        txt += "<td><b>%s</b></td>"%tr( "ABOUT_WEB" )
        txt += "<td><b>%s</b></td>"%tr( "LICENCIA")
        txt += "</tr>"

        li = [
                ( "ChessiconsV3.5", '<a href=mailto:peterwong@virtualpieces.net>Peter Wong</a>','<a href="http://www.virtualpieces.net">http://www.virtualpieces.net</a>', tr("ABOUT_LICENCIA_AUTOR") ),
                ( "Merida-Internet", 'Felix Kling','<a href="http://www.rybkachess.com">http://www.rybkachess.com</a>', tr("ABOUT_LICENCIA_AUTOR") ),
                ( "Nuvola", '<a href="http://icon-king.com/?p=15">David Vignoni</a>', '<a href="http://www.icon-king.com/projects/nuvola/">http://www.icon-king.com/projects/nuvola</a>', "LGPL" ),
                ( "Gnome", 'Gnome', '<a href="http://svn.gnome.org/viewvc/gnome-icon-theme/">http://svn.gnome.org/viewvc/gnome-icon-theme</a>', "GPL" ),
                ( "Silk icon set 1.3", 'Mark James', '<a href="http://www.famfamfam.com/lab/icons/silk/">http://www.famfamfam.com/lab/icons/silk</a>', "Creative Commons Attribution 2.5 License" ),
                ( "Spatial-Fantasy", 'Maurizio Monge','<a href="http://commons.wikimedia.org/wiki/Category:SVG_chess_pieces">Wikimedia Commons</a>', "LGPL" ),
            ]
        for tipo,autor,web,licencia in li :
            txt += "<tr>"
            txt += "<td align=right><b>%s</b></td>"%tipo
            txt += "<td>%s</td>"%autor
            txt += "<td>%s</td>"%web
            txt += "<td>%s</td>"%licencia
            txt += "</tr>"

        txt += "</table><center><hr>"
        masTab( tr( "ABOUT_IMAGENES"), txt)

        # Entrenamientos
        txt ='<hr><center><table border="0" cellpadding="5" cellspacing="5" >'

        txt += "<tr>"
        txt += "<td align=center><b>%s</b></td>"%tr( "TB_ENTRENAMIENTO" )
        txt += "<td><b>%s</b></td>"%tr( "ABOUT_WEB" )
        txt += "<td><b>%s</b></td>"%tr( "LICENCIA")
        txt += "</tr>"

        ## De las traducciones tdic, buscamos De Eduardo Sadier, por los cambios continuos
        li = (
                ( tr('De Eduardo Sadier 120536 posiciones de mate en dos'),'<a href="http://edusadier.googlepages.com/">http://edusadier.googlepages.com</a>', tr("ABOUT_LICENCIA_AUTOR") ),
                ( tr('De Pascal Georges en Scid'),'<a href="http://scid.sourceforge.net">http://scid.sourceforge.net</a>', tr("ABOUT_LICENCIA_AUTOR") ),
                ( tr('De Rui Grafino'),'', tr("ABOUT_LICENCIA_AUTOR") ),
                ( tr('De Joaquin Alvarez'),'', tr("ABOUT_LICENCIA_AUTOR") ),
            )
        for autor,web,licencia in li :
            txt += "<tr>"
            txt += "<td align=center><b>%s</b></td>"%autor
            txt += "<td>%s</td>"%web
            txt += "<td>%s</td>"%licencia
            txt += "</tr>"

        txt += "</table><center><hr>"
        masTab( tr( "ABOUT_ENTRENAMIENTOS"), txt)

        # Motores
        li = procesador.configuracion.listaMotores()
        clista = "<table><tr><td><ul>"
        medio = int(len(li)/2)
        for num, v in enumerate(li) :
            nombre, autor, url = v
            clista+= '<li><a href="%s">%s</a> - %s</li>'%( url, nombre, autor )
            if num == medio :
                clista += "</ul></td><td><ul>"
        clista += "</ul></td></tr></table>"

        txt ='<hr><center>%s</center><hr>'%clista
        masTab( tr( "MOTORES"), txt)

        # Partidas
        li = (
               ( "Jordi Gonzalez Boada", "http://www.jordigonzalezboada.com/ajedrez/aperturas.php" ),
               ( "PGN Mentor", "http://www.pgnmentor.com/files.html" ),
               ( "Dan Corbit", "http://cap.connx.com/" ),
               ( "Wikipedia", "http://en.wikipedia.org/wiki/List_of_chess_games" ),
            )
        txt = "<hr><ul>"
        for nom, web in li :
            txt += '<li><b>%s</b>  <a href="%s">%s</a></li>'%(nom,web,web)
        txt += "</ul><hr>"
        masTab( tr( "ABOUT_PARTIDAS"), txt)

        # Foros
        # li = [ "http://siliconchess.foroactivo.net", "http://immortalchess.net/", "http://www.chess2u.com" ]
        # random.shuffle( li )
        # txt = "<hr><ul>"
        # for uno in li :
            # txt += '<li><a href="%s">%s</a></li>'%(uno,uno)
        # txt += "</ul><hr>"
        # masTab( tr( "ABOUT_FOROS"), txt)

        # Programación
        li = (
               ( tr( "ABOUT_GUI" ), "PySide - LGPL", "http://www.pyside.org/" ),
               ( tr( "ABOUT_BOOKS_POLYGLOT" ), tr( "ABOUT_BASADO_EN", "Michel Van den Bergh" ),"http://alpha.uhasselt.be/Research/Algebra/Toga/book_format.html"),
               ( "Polyglot1.4w.exe", tr( "ABOUT_CREADO", "Fabien Letouzy" ) + ". " + tr( "ABOUT_MODIFICADO","Fonzy Bluemers" ),"http://www.geenvis.net/" ),
               ( tr( "ABOUT_AUDIO" ), "PyAudio v0.2.4 - MIT License","http://people.csail.mit.edu/hubert/pyaudio/")
            )
        txt = "<hr><ul>"
        txt += '<li>%s : <b><a href="http://www.python.org/">Python 2.7</a></b></li>'%tr( "ABOUT_LENGUAJE" )
        txt += '<li>%s :</li>'%tr( "ABOUT_LIBRERIAS")
        txt += "<ul>"
        for tipo, nom, web in li :
            txt += '<li>%s : <b><a href="%s">%s</a></b></li>'%(tipo, web, nom)
        txt += "</ul></ul><hr>"
        masTab( tr( "ABOUT_PROGRAMACION"), txt)

        lyV1 = Colocacion.H().control( lbIco ).espacio(15).control(lbTitulo).relleno(1)
        layout = Colocacion.V().otro( lyV1 ).espacio(10).control(tab).control( btSeguir ).margen(10)

        self.setLayout( layout )

    def shuffle( self, lista ) :
        li = []
        for n,(txt, peso) in enumerate(lista) :
            for x in range(peso) :
                li.append( n )
        random.shuffle( li )
        li1 = []
        liR = []
        for x in li :
            if x not in li1 :
                li1.append(x)
                liR.append( lista[x][0] )
        return liR

#==========================================================================================================================================
class WInfo( QtGui.QDialog ):

    def __init__(self, wParent, titulo, cabecera, txt, minTam, pmIcono )  :
        super(WInfo, self).__init__(wParent)

        self.setWindowTitle( titulo )
        self.setWindowIcon( Iconos.Aplicacion() )
        self.setWindowFlags( QtCore.Qt.Dialog | QtCore.Qt.WindowTitleHint )

        f = Controles.TipoLetra( puntos=20 )

        lbIco = Controles.LB().ponImagen( pmIcono )
        lbTitulo = Controles.LB(cabecera).alinCentrado().ponFuente( f )
        lbTexto = Controles.LB(txt)
        lbTexto.setMinimumWidth( minTam -84)
        lbTexto.setWordWrap( True )
        lbTexto.setTextFormat( QtCore.Qt.RichText )
        btSeguir = Controles.PB( self, tr( "SEGUIR" ), self.seguir ).ponPlano( False )

        lyV1 = Colocacion.V().control( lbIco ).relleno( 1 )
        lyV2 = Colocacion.V().control( lbTitulo ).control( lbTexto ).espacio(10).control(btSeguir)
        lyH = Colocacion.H().otro( lyV1 ).otro( lyV2 ).margen(10)

        self.setLayout( lyH )

        if DEBUG :
            fdbg.close()

    def seguir( self ) :
        self.close()

def info( parent, titulo, cabecera, txt, minTam, pmIcono ) :
    w = WInfo( parent, titulo, cabecera, txt, minTam, pmIcono )
    w.exec_()
