#!/usr/bin/env python
#-*- coding: UTF-8 -*-
 
import wx
import os.path
import stomasqlite
from stomamisc import *


class TaskInfoPopup(wx.PopupTransientWindow):
    """  """
    def __init__(self, parent, config, taskname):
        wx.PopupTransientWindow.__init__(self, parent, style = wx.SIMPLE_BORDER)
        self.SetBackgroundColour("#C0C0E0")
        taskdata = config['Tasks'][taskname]
        i0 = 'Task name:               %s\n\n' % (taskname)
        i1 = 'Source path:             %s\n' % (taskdata['sourcepath'])
        i2 = 'Include subdirs:         %s\n' % (taskdata['subinclude'])
        i3 = 'Target path:             %s\n' % (taskdata['targetpath'])
        i4 = 'Override target content: %s\n\n' % (taskdata['override'])
        i5 = 'Compression methode:     %s\n\n' % (taskdata['comprmeth'])
        i6 = 'Time stamp:              %s\n' % (taskdata['timestamp'])
        i7 = 'Append Ts:               %s\n' % ( taskdata['tsappend'])
        i8 = 'Exclude reg exp list:    %s\n' % ((',\n                         ').join(taskdata['exclude']))
        if taskdata['lastrun'].startswith('2000'):
            i9 = '\nLast time executed at:   %s' % ('never')
        else:
            i9 = '\nLast time executed at:   %s' % (taskdata['lastrun'])
        info = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9
        
        st = wx.StaticText(self, -1, info, pos = wx.Point(10,10))
        font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
        st.SetFont(font)
        sz = st.GetBestSize()
        self.SetSize(wx.Size(sz.width + 20, sz.height + 20))


class JobInfoPopup(wx.PopupTransientWindow):
    """  """
    def __init__(self, parent, config, jobname):
        wx.PopupTransientWindow.__init__(self, parent, style = wx.SIMPLE_BORDER)
        self.SetBackgroundColour("#C0C0E0")
        jobdata = config['Jobs'][jobname]     
        i0  = 'Job name:               %s\n\n' % (jobname)
        i1  = 'Immediate execution:    %s\n' % (jobdata['immediate'])
        i2  = 'Scheduled execution:    %s\n' % (jobdata['background'])
        i3  = 'Delayed execution:      %s\n\n' % (jobdata['delayed'])
        if jobdata['lastrun'].startswith('2000'):
            i4  = 'Last time executed at:  %s\n' % ('never')
        else:
            i4  = 'Last time executed at:  %s\n' % (jobdata['lastrun'])
        if (jobdata['background'] == 'False') or \
           (jobdata['delayed'] == 'True' and int(jobdata['interval']) == 0): 
            i5  = 'Next time executed at:  \n' 
        else:    
            i5  = 'Next time executed at:  %s\n' % (jobdata['nextrun'])
        i6  = 'Repeat interval (days): %s\n\n' % (jobdata['interval'])
        i7  = 'Task count:             %s\n' % (jobdata['taskcount'])
        i8  = 'Task list:              '
        for task in jobdata['tasklist']:
            i8 = i8 + task + '\n                        '
        i8 = i8[:-25]
        info = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8
        st = wx.StaticText(self, -1, info, pos = wx.Point(10,10))
        font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
        st.SetFont(font)
        sz = st.GetBestSize()
        self.SetSize(wx.Size(sz.width + 20, sz.height + 20))


class DbInfoPopup(wx.PopupTransientWindow):
    """  """
    def __init__(self, parent, config, dbindex):
        wx.PopupTransientWindow.__init__(self, parent, style = wx.SIMPLE_BORDER)
        self.SetBackgroundColour("#C0C0E0")
             
        dbpathname = config['Database']['dbhistory'][dbindex]
        path, dbfilename = os.path.split(dbpathname)
        i1  = 'Database path:    %s\n' % (path)
        i2  = 'Database file:    %s\n' % (dbfilename)        
        if os.path.exists(dbpathname):
            db = stomasqlite.StoreManDb(dbpathname)
            dirs = db.getDirCount()
            files = db.getRecordCount()
            result = db.getScanDates()
            i3  = 'Database size:    %s Bytes\n\n' % (format3P(os.path.getsize(dbpathname)))
            i4  = 'Directory count:  %s\n' % (format3P(dirs))
            i5  = 'File count:       %s\n' % (format3P(files))
            i6  = 'Scan runs:        %s' % (str(len(result)))
        else:
            i3 = '\n'
            i4 = 'database does not exist'
            i5 = i6 = ''
         
        info = i1 + i2 + i3 + i4 + i5 + i6 
        st = wx.StaticText(self, -1, info, pos = wx.Point(10,10))
        font = wx.Font(9, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
        st.SetFont(font)
        sz = st.GetBestSize()
        self.SetSize(wx.Size(sz.width + 20, sz.height + 20))


        
        
