## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (c) 2007-2008 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------

import wx, wx.html, ws
import constants

class ProblemSetPanel(wx.Panel):
    def __init__(self, parent, problemset):
        wx.Panel.__init__(self, parent) #, style=wx.SIMPLE_BORDER)
        self.links = {
            "query" : constants.ID_QUERY_BUILDER,
            "info" : constants.ID_SHOW_INFO,
            "files" : constants.ID_SHOW_FILES }
        self.SetBackgroundColour(wx.Color(250, 250, 250))
        self.problemset = problemset
        self.title = wx.StaticText(self, -1, "iSiLK - %s" % problemset.GetName())
        self.title.SetFont(ws.LgFont(wx.SWISS, wx.NORMAL, wx.BOLD))
        labelcolor = wx.Colour(50,50,50)
        self.localLabel = ws.SmStaticLabel(self, -1, "Local directory:")
        self.localLabel.SetForegroundColour(labelcolor)
        self.localLabel.SetFont(ws.SmFont(wx.SWISS, wx.NORMAL, wx.BOLD))
        self.localPath = ws.SmHyperlinkCtrl(self, -1, problemset.GetLocalDir(), 
            problemset.GetLocalDir())
        remote_path = "%s:%s" % (self.problemset.GetRemoteHost(),
            self.problemset.GetRemotePath()) 
        self.remoteLabel = ws.SmStaticLabel(self, -1, "Remote directory:")
        self.remoteLabel.SetForegroundColour(labelcolor)
        self.remoteLabel.SetFont(ws.SmFont(wx.SWISS, wx.NORMAL, wx.BOLD))  
        self.remotePath = ws.SmHyperlinkCtrl(self, -1, "ssh://" + remote_path,
            remote_path, style = wx.HL_ALIGN_LEFT|wx.NO_BORDER)
        # FIXME: these links don't work on Windows (why?)
        # FIXME: figure out what we really want in the toplevel view
        if False:
            #self.remotePath.SetFont(ws.SmFont(wx.SWISS, wx.NORMAL, wx.NORMAL))
            szq = self._createLink("Query repository", "icons/cog-query.gif", "query")
            szi = self._createLink("Edit info", "icons/info.gif", "info")
        #szf = self._createLink("Show files", "icons/folder.gif", "files")        
        sz = wx.GridBagSizer() 
        sz.SetEmptyCellSize((0,0))
        sz.Add(self.title, (0,0), (1,2), flag=wx.ALL, border=8)
        sz.Add(self.localLabel, (1,0), flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=8)
        sz.Add(self.localPath, (1,1), flag=wx.LEFT|wx.BOTTOM|wx.ALIGN_LEFT, border=8)
        sz.Add(self.remoteLabel, (2,0), flag=wx.ALIGN_LEFT|wx.LEFT|wx.BOTTOM, border=8)        
        sz.Add(self.remotePath, (2,1), flag=wx.LEFT|wx.ALIGN_LEFT, border=8)        
        sz.Add(wx.StaticLine(self, wx.LI_HORIZONTAL), (3,0), (1,2), flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
        # FIXME: figure out what we really want
        if False:
            sz.Add(szq, (4,0), (1,2), flag=wx.LEFT|wx.BOTTOM|wx.TOP|wx.ALIGN_LEFT, border=8)        
            sz.Add(szi, (5,0), (1,2), flag=wx.LEFT|wx.BOTTOM|wx.ALIGN_LEFT, border=8)        
        #sz.Add(szf, (6,0), flag=wx.LEFT|wx.BOTTOM|wx.ALIGN_LEFT, border=8)                
        sz.AddGrowableCol(1)
        self.SetSizerAndFit(sz)
        self.RefreshContents()
        self.Bind(wx.EVT_HYPERLINK, self.OnClickLocal, self.localPath)
        self.Bind(wx.EVT_HYPERLINK, self.OnClickRemote, self.remotePath)
        self.Bind(ws.EVT_RESULT_CHANGED, self.OnSetChanged)
        self.Bind(ws.EVT_UPDATE_STATUS, self.OnSetChanged)
        
    def OnSetChanged(self, event):
        self.RefreshContents()
        
    def OnClickLocal(self, event):
        wx.GetApp().ShowFile(self.problemset.GetLocalDir())

    def OnClickRemote(self, event):
        print "Do nothing"
        
    def _createLink(self, text, img, url):
        link = ws.SmHyperlinkCtrl(self, -1, label=text, url=url)
        bmp = wx.Bitmap(img, wx.BITMAP_TYPE_GIF)
        if False:
            bb = wx.StaticBitmap(self, -1)
            bb.SetBitmap(bmp)
        else:
            bb = wx.BitmapButton(self, -1, bmp, name = url)
        sz = wx.BoxSizer(wx.HORIZONTAL)
        sz.Add(bb, flag=wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border=8)
        sz.Add(link, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=8)
        self.Bind(wx.EVT_HYPERLINK, self.OnHyperlink, link)
        self.Bind(wx.EVT_BUTTON, self.OnButton, bb)
        return sz
        
    def GetStatusString(self):
        return self.problemset.GetLocalDir()
        
    def OnButton(self, event):   
        url = event.GetEventObject().GetName()
        if url in self.links:
            wx.PostEvent(self, wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED,
                self.links[url]))
        
    def OnHyperlink(self, event):
        url = event.GetURL() 
        if url in self.links:
            wx.PostEvent(self, wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED,
                self.links[url]))
        print "Link: %s" % url

    def RefreshContents(self):
        local_path  = self.problemset.GetLocalDir()
        remote_path = "ssh://%s%s" % (self.problemset.GetRemoteHost(),
            self.problemset.GetRemotePath())
        self.remotePath.SetLabel(remote_path)
        self.localPath.SetLabel(local_path)
        self.Layout()
        self.title.SetLabel("iSiLK - %s" % self.problemset.GetName())
        
    def GetClipboardObject(self):
        return None
        
    def IsClipboardFormatAvailable(self, format):
        print "Returning FALSE"
        return False
        

    
