#!/usr/bin/env python2.3

from wxPython.wx import *
from wxPython.grid import *
#from freedb import *


class MainDialog(wxDialog):
    def __init__(self, *args, **kwds):
        kwds["style"] = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER
        wxDialog.__init__(self, *args, **kwds)
        self.AlbumGrid=None
        self.Albums=None
        self.MainSizer=wxBoxSizer(wxVERTICAL)
        self.toppanel = wxPanel(self, -1)
        self.bottompanel = wxPanel(self, -1)
        self.centpanel = wxPanel(self, -1)
        self.toppanel.SetSize((592, 23))
        self.bottompanel.SetSize((592, 24))

        self.Label=wxStaticText(self.toppanel, -1, "Search:")
        self.Label.SetBackgroundColour(self.toppanel.GetBackgroundColour())
        self.Text=wxTextCtrl(self.toppanel, 1333, "Search Text",style=wxTE_PROCESS_ENTER)
        EVT_TEXT_ENTER(self.Text,1333,self.OnClickBtn)
        self.btnSearch = wxButton(self.toppanel, 1221, "Search")
        EVT_BUTTON(self, 1221, self.OnClickBtn)
        self.btnExit = wxButton(self.toppanel, 1220, "Exit")
        EVT_CLOSE(self, self.Exit) # Ez azert kell, hogy a kereten levo X-szel is helyesen lezarja a dialogus ablakot
        EVT_BUTTON(self, 1220, self.Exit)
        #self.fillpanel=wxPanel(self, -1)
        #self.fillpanel.SetSize((300, 24))
        topsizer = wxBoxSizer(wxHORIZONTAL)
        topsizer.Add(self.Label, 0,wxLEFT|wxALIGN_LEFT,2)
        topsizer.Add(self.Text, 2,wxLEFT|wxALIGN_LEFT,2)
        topsizer.Add(self.btnSearch, 1,wxLEFT|wxALIGN_LEFT,2)
        topsizer.Add((1,1),1)
        topsizer.Add(self.btnExit, 0, wxLEFT|wxALIGN_RIGHT,2)
        self.toppanel.SetAutoLayout(1)
        self.toppanel.SetSizer(topsizer)
        topsizer.SetSizeHints(self.toppanel)

        self.centersizer=wxBoxSizer(wxHORIZONTAL)
        self.centpanel.SetAutoLayout(1)
        self.centpanel.SetSizer(self.centersizer)
        self.centersizer.SetSizeHints(self.centpanel)

        self.bottomsizer=wxBoxSizer(wxHORIZONTAL)
        self.bottompanel.SetAutoLayout(1)
        self.bottompanel.SetSizer(self.bottomsizer)
        self.bottomsizer.SetSizeHints(self.bottompanel)

        self.MainSizer.Add(self.toppanel,0, wxEXPAND|wxALL|wxALIGN_CENTER_HORIZONTAL, 2)
        self.MainSizer.Add(self.centpanel,1,wxALL|wxEXPAND,2)
        self.MainSizer.Add(self.bottompanel,1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL, 2)
        self.SetAutoLayout(1)
        self.SetSizer(self.MainSizer)

        self.Layout()
        self.Centre()
        self.SetTitle("FreeDB Search Dialog")
        _icon = wxEmptyIcon()
        #_icon.CopyFromBitmap(wxBitmap("EXITEML.ico", wxBITMAP_TYPE_ANY))
        #self.SetIcon(_icon)
        self.SetSize((640, 480))
        self.SetBackgroundColour(self.bottompanel.GetBackgroundColour())
        self.Centre()
        # end wxGlade

    def EvtChar(self, event):
        cc=event.GetKeyCode()
        if (cc==13):
           self.OnClickBtn(None)
        event.Skip()
        #self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode())

    def SetGridReadOnly(self,grid):
        for c in range(grid.GetNumberCols()):
            for r in range(grid.GetNumberRows()):
                grid.SetReadOnly(r,c,1)


    def OnClickBtn(self,event):
        s=self.Text.GetValue()
        import freedb
        albums=freedb.freedb_search(s,criteria="all")
        def sf(a1,a2):
            l1=[a1.artist,a1.title,a1.checksum,a1.versions]
            l2=[a2.artist,a2.title,a2.checksum,a2.versions]
            for i in range(len(l1)):
                s1=str(l1[i]).lower().strip();s2=str(l2[i]).lower().strip()
                if s1>s2: return 1
                elif s2>s1: return -1
            return 0
        albums.sort(sf)
        self.Albums=albums
        self.ShowAlbums()

    def ShowAlbums(self):
        if self.AlbumGrid<>None:
           self.AlbumGrid.Destroy()
           self.AlbumGrid=None
        self.AlbumGrid=grid=wxGrid(self.centpanel,-1)
        ''',style=wxWANTS_CHARS'''
        albs=self.Albums
        self.AlbumGrid.CreateGrid(len(albs),4)
        self.SetGridReadOnly(grid)
        EVT_GRID_CELL_LEFT_DCLICK(grid,self.OnSubDbl)
        #EVT_CHAR(grid,self.OnSubChar)
        grid.SetColLabelValue(0,"ID")
        grid.SetColLabelValue(1,"Artist")
        grid.SetColLabelValue(2,"Title")
        grid.SetColLabelValue(3,"Versions")
        grid.SetColSize(0,80)
        grid.SetColSize(1,192)
        grid.SetColSize(2,160)
        grid.SetColSize(3,80)
        for idx in range(len(albs)):
            alb=albs[idx]
            grid.SetCellValue(idx,0,alb.checksum)
            grid.SetCellValue(idx,1,alb.artist)
            grid.SetCellValue(idx,2,alb.title)
            grid.SetCellValue(idx,3,str(alb.versions))
        self.centersizer.Add(self.AlbumGrid,1,wxALL|wxEXPAND,4)
        self.SubGrid=None
        self.SubGrid2=None
        self.centpanel.Layout()

    def OnSubChar(self,e):
        cc=event.GetKeyCode()
        if (cc==13):
           cc=self.AlbumGrid.GetSelectedCells()
           row=cc[0][0]
           self.OnSub(row)
        e.Skip()

    def OnSubDbl(self,e):
        row=e.GetRow()
        self.OnSub(row)
        e.Skip()

    def OnSub(self,row):
        albs=self.Albums
        alb=albs[row]
        self.XAlbum=xalb=alb.load()
        if self.SubGrid<>None:
           self.bottomsizer.Remove(self.SubGrid)
           self.SubGrid.Destroy()
        if self.SubGrid2<>None:
           self.bottomsizer.Remove(self.SubGrid2)
           self.SubGrid2.Destroy()
        self.SubGrid=grid=wxGrid(self.bottompanel,-1)
        self.SubGrid2=grid2=wxGrid(self.bottompanel,-1)
        self.SetGridReadOnly(grid)
        self.SetGridReadOnly(grid2)
        grid.CreateGrid(len(xalb.tracks),1)
        grid2.CreateGrid(6,1)
        grid.SetColSize(0,128)
        grid.SetColLabelValue(0,"Track titles")
        grid2.SetRowLabelValue(0,"DiscID")
        grid2.SetRowLabelValue(1,"Artist")
        grid2.SetRowLabelValue(2,"Title")
        grid2.SetRowLabelValue(3,"Versions")
        grid2.SetRowLabelValue(4,"Genre")
        grid2.SetRowLabelValue(5,"Year")
        grid2.SetColLabelValue(0,"Album infos")
        grid2.SetColSize(0,128)
        #grid.SetColSize(1,192)
        for i in range(len(xalb.tracks)):
            data=str(xalb.tracks[i][0]).strip()
            grid.SetCellValue(i,0,data)
        grid2.SetCellValue(2,0,str(xalb.dtitle))
        grid2.SetCellValue(0,0,str(xalb.discid))
        grid2.SetCellValue(1,0,str(alb.artist))
        grid2.SetCellValue(2,0,str(alb.versions))
        grid2.SetCellValue(5,0,str(xalb.dyear))
        grid2.SetCellValue(4,0,str(xalb.dgenre))
        self.bottomsizer.Add(grid2,1,wxALL|wxEXPAND,4)
        self.bottomsizer.Add(grid,1,wxALL|wxEXPAND,4)
        self.bottompanel.Layout()

    def Exit(self,e):
        self.Destroy()


# end of class MyFrame


class FDApplication(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()
        f=MainDialog(None,-1,'')
        f.CenterOnScreen()
        f.Show()
        self.SetTopWindow(f)
        return 1

if __name__ == "__main__":
    redirect_stdout_to_window=0
    app=FDApplication(redirect=redirect_stdout_to_window)
    app.MainLoop()


