# -*- coding: utf-8 -*-
"""
Interface graphique pour l'analyse des fichiers de log minwii.

$Id$
$URL$
"""

import os
os.environ['WINWII_NO_LOG'] = '1'
from Tkinter import *
import tkFileDialog
from glob import glob
from os.path import join as pjoin
from os.path import basename
from os.path import getsize
import os.path
from minwii.loganalyse import LogFileAnalyser
from minwii.config import LOGS_DIR
from pprint import pprint

class Application(Frame) :
    def __init__(self, master=None) :
        Frame.__init__(self, master)
        self.configureStretching()
        self.createWidgets()
        self.logDir = ''
        self.logFiles = []
        self.currentFilePath = ''
        self.resultsFrame = None
        
        if os.path.exists(LOGS_DIR) :
            self.chooseDirDialog(dir=LOGS_DIR)
    
    def configureStretching(self) :
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        
        self.grid(sticky=N+S+E+W, padx=10, pady=10)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
    
    def createWidgets(self) :
        # zone d'affichage des données'
        self.dataFrame = df = Frame(self)
        
        self.identFrame = Identification(df)
        self.identFrame.grid(sticky=NW)
        
        # barre de boutons
        self.btnFrame = bf = Frame(self)
        bf.grid(row=1, column=0, sticky=W+S+E)
        bf.rowconfigure(0, weight=1)
        for i in range(3) :
            bf.columnconfigure(i, weight=1)        

        self.chooseLogDir = Button(bf, text="Parcourir…", command=self.chooseDirDialog)
        self.chooseLogDir.grid(row=0, column=0, sticky=W)
        
        self.nav = Navbar(bf, incCallback=self.loadLogFile, decCallback=self.loadLogFile)

        self.quitButton = Button(bf, text='Terminer', command=self.quit)
        self.quitButton.grid(row=0, column=2, sticky=E)
    
    def chooseDirDialog(self, dir=None) :
        if dir is None :
            self.logDir = tkFileDialog.askdirectory(title='Sélectionnez un dossier de fichiers de logs')
        else :
            self.logDir = dir
        if self.logDir :
            self.logFiles = glob(pjoin(self.logDir, '*.log'))
            self._cleanupJunkFiles()
            self.logFiles.sort()
            self.logFiles.reverse()
            self.dataFrame.grid(row=0, column=0, sticky=NW)
            self.nav.setSize(len(self.logFiles))
            self.nav.grid(row=0, column=1)
            self.loadLogFile(self.nav)
    
    def _cleanupJunkFiles(self) :
        files = []
        while self.logFiles :
            f = self.logFiles.pop()
            if not getsize(f) :
                os.remove(f)
                continue
            else :
                of = open(f)
                lfa = LogFileAnalyser(of)
                if lfa.getLastEventTicks() is None :
                    of.close()
                    os.remove(f)
                    continue
                else :
                    of.close()
                
                files.append(f)
        
        self.logFiles = files
        
    
    def loadLogFile(self, nav) :
        index = nav.index - 1
        filepath = self.logFiles[index]
        self.currentFilePath = filepath
        lfa = LogFileAnalyser(self.currentFilePath)
        self.identFrame.refresh(lfa)
        if self.resultsFrame :
            self.resultsFrame.destroy()
        self.resultsFrame = ResultsFrame(self.dataFrame)
        self.resultsFrame.layResults(lfa)
        lfa.close()
        self.resultsFrame.grid()


class Navbar(Frame) :
    def __init__(self, master=None, size=1, incCallback=None, decCallback=None) :
        Frame.__init__(self, master)
        self.caption = StringVar()
        self.createWidgets()
        self.setSize(size)
        self.incCallback = incCallback if incCallback else lambda x : None
        self.decCallback = decCallback if decCallback else lambda x : None
        self.caption.set('%d / %d' % (self.index, self.to))
    
    def createWidgets(self) :
        self.backBtn = Button(self,
                              text='◀',
                              command = self.dec
                              )
        self.backBtn.grid(row=0, column=0)
        
        self.lbl = Label(self, textvariable=self.caption)
        self.lbl.grid(row=0, column=1)

        self.nextBtn = Button(self,
                              text='▶',
                              command = self.inc)
        self.nextBtn.grid(row=0, column=2)
    
    def refreshStates(self) :
        if self.index == self.from_ :
            self.backBtn.configure(state=DISABLED)
        else :
            self.backBtn.configure(state=NORMAL)

        if self.index < self.to :
            self.nextBtn.configure(state=NORMAL)
        else :
            self.nextBtn.configure(state=DISABLED)

        self.caption.set('%d / %d' % (self.index, self.to))
        
    
    def dec(self) :
        self.index = self.index - 1
        self.refreshStates()
        self.decCallback(self)
    
    def inc(self) :
        self.index = self.index + 1
        self.refreshStates()
        self.incCallback(self)

    def setSize(self, size) :
        self.from_ = 1
        self.to = size
        self.index = 1
        self.refreshStates()


class Identification(Frame) :
    def __init__(self, master=None) :
        Frame.__init__(self, master)
        self.fileName = StringVar()
        self.hid = StringVar()
        self.patientName = StringVar()
        self.createWidgets()
    
    def refresh(self, lfa) :
        filename = basename(lfa.logfile.name)
        self.fileName.set(filename)
        self.hid.set(lfa.getHID())
        metadata = lfa.getMetadata()
        self.patientName.set(metadata.get('PatientName', ''))
        self.commentsText.delete(1.0, END)
        self.commentsText.insert(1.0, metadata.get('Comments', ''))
    
    def createWidgets(self) :
        fileLbl = Label(self, text='Fichier :')
        fileLbl.grid(row=0, column=0, sticky=E)

        fileNameLbl = Label(self, textvariable=self.fileName)
        fileNameLbl.grid(row=0, column=1, sticky=W)
        
        hidLbl = Label(self, text='HID :')
        hidLbl.grid(row=1, column=0, sticky=E)
        
        hidNameLbl = Label(self, textvariable=self.hid)
        hidNameLbl.grid(row=1, column=1, sticky=W)
        
        nameLbl = Label(self, text='Patient :')
        nameLbl.grid(row=2, column=0, sticky=E)
        
        self.nameEntry = Entry(self, width=40, textvariable=self.patientName)
        self.nameEntry.grid(row=2, column=1, sticky=W)
        
        commentsLbl = Label(self, text='Commentaires :')
        commentsLbl.grid(row=3, column=0, sticky=E)
        
        self.commentsText = Text(self, width=40, height=4, undo=True, wrap=WORD)
        self.commentsText.grid(row=3, column=1, sticky=W)
        
        self.saveBtn = Button(self, text='Enregistrer', command=self.saveMetadata)
        self.saveBtn.grid(row=4, column=1, sticky=E)
    
    def saveMetadata(self):
        app = self.master.master
        filepath = app.currentFilePath
        lfa = LogFileAnalyser(filepath, mode='r+')
        patientName = '%s\n' % self.nameEntry.get().replace('\n', ' ').strip()
        comments = '%s\n' % self.commentsText.get(1.0, END).replace('\n', ' ').strip()
        metadata = (('PatientName', self.nameEntry.get()),
                    ('Comments', comments))
        lfa.setMetadata(metadata)


class ResultsFrame(Frame) :
    
    def layResults(self, lfa) :
        results = lfa.analyse()
        if results :
            for i, kvt in enumerate(results) :
                k, v, timeBased = kvt
                kl = Label(self, text='%s :' % k)
                kl.grid(row=i, column=0, sticky=E)

                if not timeBased :
                    vl = Label(self, text=v)
                    vl.grid(row=i, column=1, sticky=W)
                else :
                    maxv = max(v)
                    if maxv :
                        cw, ch = 200, 100
                        c = Canvas(self, background='#fff', width=cw, height=ch)
                        rectW = int(float(cw) / len(v))
                        unitRectH = float(ch) / maxv
                        for j, fv in enumerate(v) :
                            if not fv : continue
                            x0 = j * rectW
                            y0 = ch - int(unitRectH * fv)
                            x1 = (j + 1) * rectW
                            y1 = ch
                            c.create_rectangle(x0, y0, x1, y1, fill="#9085ba")
                        c.grid(row=i, column=1, sticky=W)
                            
                    else :
                        vl = Label(self, text='—')
                        vl.grid(row=i, column=1, sticky=W)
        else :
            msg = Label(self, text="Pas de données exploitables.")
            msg.grid()
            
        
def main() :
    app = Application()
    app.master.title("Analyseur des sessions MINDs")
    app.mainloop()

if __name__ == '__main__' :
    main()