#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Wed Jan  4 20:52:55 2012

import wx
import os
from indexer import indexer
from buscador import buscador

# begin wxGlade: extracode
# end wxGlade

class mainFrame(wx.Frame):

    def __init__(self, *args, **kwds):
        # begin wxGlade: mainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.relleno1 = wx.Panel(self, -1)
        self.relleno2 = wx.Panel(self, -1)
        self.relleno3 = wx.Panel(self, -1)
        self.relleno4 = wx.Panel(self, -1)
        self.rellenoPregIzq = wx.Panel(self, -1)
        self.txPregunta = wx.TextCtrl(self, -1, "")
        self.btBuscar = wx.Button(self, -1, "Buscar")
        self.rellenoBotonDer = wx.Panel(self, -1)
        self.rellenoBajo1 = wx.Panel(self, -1)
        self.rellenoBajo2 = wx.Panel(self, -1)
        self.btXML = wx.Button(self, -1, "Ver XML")
        self.rellenoBajo4 = wx.Panel(self, -1)
        self.rellenoIzqVer = wx.Panel(self, -1)
        self.lbVisualiza = wx.StaticText(self, -1, "Selecciona un documento para visualizarlo.")
        self.rellenoVerDer1 = wx.Panel(self, -1)
        self.rellenoVerDer2 = wx.Panel(self, -1)
        self.rellenoIzqTxVer = wx.Panel(self, -1)
        self.comboDocumentos = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN|wx.CB_READONLY)
        self.btVer = wx.Button(self, -1, "Ver")
        self.rellenoDerTxVer = wx.Panel(self, -1)
        self.rellenoBajoVer1 = wx.Panel(self, -1)
        self.lbNom = wx.StaticText(self, -1, " Nombre.")
        self.lbPath = wx.StaticText(self, -1, " Path del documento.")
        self.btSimilares = wx.Button(self, -1, "Similares")
        self.rellenoBajoVer4 = wx.Panel(self, -1)
        self.rellenoIzqInsert = wx.Panel(self, -1)
        self.txName = wx.TextCtrl(self, -1, "")
        self.txPath = wx.TextCtrl(self, -1, "")
        self.btInsetar = wx.Button(self, -1, "Insertar")
        self.rellenoDerInsertar = wx.Panel(self, -1)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.busca, self.btBuscar)
        self.Bind(wx.EVT_BUTTON, self.muestraXML, self.btXML)
        self.Bind(wx.EVT_TEXT, self.getIden, self.comboDocumentos)
        self.Bind(wx.EVT_BUTTON, self.visualiza, self.btVer)
        self.Bind(wx.EVT_BUTTON, self.similares, self.btSimilares)
        self.Bind(wx.EVT_BUTTON, self.inserta, self.btInsetar)
        # end wxGlade
        
        index = indexer()
        docs = index.getDocuments()
        self.comboDocumentos.AppendItems(docs)
        self.comboDocumentos.SetValue(docs[0])
        self.idDoc = 1

    def __set_properties(self):
        # begin wxGlade: mainFrame.__set_properties
        self.SetTitle(u"Práctica A.R.I.")
        self.SetSize((709, 220))
        self.btXML.Enable(False)
        self.lbNom.SetMinSize((164, 17))
        self.txName.SetMinSize((164, 27))
        self.txPath.SetMinSize((430, 29))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: mainFrame.__do_layout
        gridSizer = wx.FlexGridSizer(8, 4, 0, 0)
        sizerInsert = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        gridSizer.Add(self.relleno1, 1, wx.EXPAND, 0)
        gridSizer.Add(self.relleno2, 1, wx.EXPAND, 0)
        gridSizer.Add(self.relleno3, 1, wx.EXPAND, 0)
        gridSizer.Add(self.relleno4, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoPregIzq, 1, wx.EXPAND, 0)
        gridSizer.Add(self.txPregunta, 0, wx.EXPAND, 0)
        gridSizer.Add(self.btBuscar, 0, 0, 0)
        gridSizer.Add(self.rellenoBotonDer, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoBajo1, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoBajo2, 1, wx.EXPAND, 0)
        gridSizer.Add(self.btXML, 0, 0, 0)
        gridSizer.Add(self.rellenoBajo4, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoIzqVer, 1, wx.EXPAND, 0)
        gridSizer.Add(self.lbVisualiza, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        gridSizer.Add(self.rellenoVerDer1, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoVerDer2, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoIzqTxVer, 1, wx.EXPAND, 0)
        gridSizer.Add(self.comboDocumentos, 0, wx.EXPAND, 0)
        gridSizer.Add(self.btVer, 0, 0, 0)
        gridSizer.Add(self.rellenoDerTxVer, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoBajoVer1, 1, wx.EXPAND, 0)
        sizer_1.Add(self.lbNom, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_1.Add(self.lbPath, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        gridSizer.Add(sizer_1, 1, wx.EXPAND, 0)
        gridSizer.Add(self.btSimilares, 0, 0, 0)
        gridSizer.Add(self.rellenoBajoVer4, 1, wx.EXPAND, 0)
        gridSizer.Add(self.rellenoIzqInsert, 1, wx.EXPAND, 0)
        sizerInsert.Add(self.txName, 0, wx.EXPAND, 0)
        sizerInsert.Add(self.txPath, 0, wx.EXPAND, 0)
        gridSizer.Add(sizerInsert, 1, wx.EXPAND, 0)
        gridSizer.Add(self.btInsetar, 0, 0, 0)
        gridSizer.Add(self.rellenoDerInsertar, 1, wx.EXPAND, 0)
        self.SetSizer(gridSizer)
        gridSizer.AddGrowableRow(7)
        gridSizer.AddGrowableCol(1)
        self.Layout()
        self.Centre()
        # end wxGlade

    def busca(self, event): # wxGlade: mainFrame.<event_handler>
        pregunta = self.txPregunta.GetValue()
        
        if len(pregunta) == 0:
        	wx.MessageBox('Introduce un término de búsqueda', 
        			  	  'Info', 
        			  	  wx.OK | wx.ICON_INFORMATION)
        else:
        	b = buscador()
        	userTime = b.buscar(pregunta)
        
        	self.btXML.Enable()
        	wx.MessageBox('Búsqueda finalizada, tiempo: %.3f segs' %(userTime), 
        				  'Info', 
        				  wx.OK | wx.ICON_INFORMATION)
        event.Skip()

    def muestraXML(self, event): # wxGlade: mainFrame.<event_handler>
    	os.system('firefox Resultados.xml')
        event.Skip()

    def visualiza(self, event): # wxGlade: mainFrame.<event_handler>
        doc = self.comboDocumentos.GetValue()
        os.system('gedit ' + doc)
        event.Skip()

    def inserta(self, event): # wxGlade: mainFrame.<event_handler>
        ok = True
        doc = self.txName.GetValue()
        path = self.txPath.GetValue()
        
        if len(doc) == 0:
        	wx.MessageBox('Debes introducir el nombre del documento', 
        				  'Info', 
        				  wx.OK | wx.ICON_INFORMATION)
        	ok = False
        elif len(path) == 0:
        	path = os.getcwd() + '/Docs'
        
        if ok:
        	index = indexer()
        	index.registerNewDocument(doc, path)
	    	wx.MessageBox('Documento registrado con éxito', 
        			  	  'Info', 
        			  	  wx.OK | wx.ICON_INFORMATION)
        	
        	index = indexer()
        	docs = index.getDocuments()
        	self.comboDocumentos.AppendItems(docs)

        event.Skip()

    def getIden(self, event): # wxGlade: mainFrame.<event_handler>
        b = buscador()
        self.idDoc = b.getIdDoc(self.comboDocumentos.GetValue()) 
        print 'Id seleccionado: %d' %(self.idDoc)
        event.Skip()

    def similares(self, event): # wxGlade: mainFrame.<event_handler>
        b = buscador()
        userTime = b.similares(self.idDoc)
        
        self.btXML.Enable()
        wx.MessageBox('Búsqueda de similares finalizada, tiempo: %.3f segs' %(userTime), 
        			  'Info', 
        			  wx.OK | wx.ICON_INFORMATION)
        event.Skip()

# end of class mainFrame

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        mainWindow = mainFrame(None, -1, "")
        self.SetTopWindow(mainWindow)
        mainWindow.Show()
        return 1

# end of class MyApp

if __name__ == "__main__":
    GUI = MyApp(0)
    GUI.MainLoop()
