## ------------------------------------------------------------------------
## 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 silk
import tools
import resultset
import wx, ws

class SetTool(tools.AnalysisTool):
    def __init__(self, **kargs):
        tools.AnalysisTool.__init__(self, **kargs)

    def CreateDialog(parentResult, parent, title):
        return SetToolDialog(parentResult, parent, title=title)
    CreateDialog = staticmethod(CreateDialog)
    
    def CanRun(parentResult):
        if not parentResult:
            return False
        return parentResult.IsFilterResult()
    CanRun = staticmethod(CanRun)

    def _GetToolName():
        return "rwset"
    _GetToolName = staticmethod(_GetToolName)
            
    def _GetResultClass(self):
        return resultset.SetResult

    def _GetResultName(self):
        return "Set"

    def _RunAnalysis(self, context, inputFileName, outputFileName):
        # FIXME: Any better way to deal with non-standard output file args?
        if "dip-file" in self.opts:
            self.opts["dip-file"] = self.opts["dip-file"].replace("$output", 
                outputFileName)
        else:
            self.opts["sip-file"] = self.opts["sip-file"].replace("$output", 
                outputFileName)
        silk.rwset(context, inputFileName, "", **self.opts)
        silk.rwsetcat(context, outputFileName, outputFileName+".asc", **self.opts)
        
    def _GetCommandLines(inputFile, outputFile, **kargs):
        # Note: no output file - expects it as dip-file or sip-file option
        cmd = silk.build_rwset_command(False, "", inputFile, commandsOnly=True, **kargs)
        return cmd
    _GetCommandLines = staticmethod(_GetCommandLines)


class SetToolDialog(wx.Dialog):
    def __init__(self, parentResult, parent, *args, **kargs):
        wx.Dialog.__init__(self, parent, -1, *args, **kargs)
        self.lbl = wx.StaticText(self, -1, "Build sets from:")
        self.rbsip = wx.RadioButton(self, -1, "Source IP addresses", style = wx.RB_GROUP)
        self.rbdip = wx.RadioButton(self, -1, "Destination IP addresses")
        self.rbsip.SetValue(True)
    
        # Create buttons
        sz = wx.BoxSizer(wx.HORIZONTAL)
        sz.Add(wx.Button(self, wx.ID_CANCEL, "Cancel"), flag=wx.ALL, border=10)
        btnCreate = wx.Button(self, wx.ID_OK, "Run Analysis") #, (110,-1), (100,-1))
        sz.Add(btnCreate, flag=wx.ALL, border=10)
        self._createNameEditor()
        # Create is default button
        btnCreate.SetDefault()
        # Escape key or close causes cancel
        self.SetEscapeId(wx.ID_CANCEL)
        szDlg = wx.BoxSizer(wx.VERTICAL)
        szDlg.Add(self.lbl, flag=wx.ALL, border=8)
        szDlg.Add(self.rbsip, flag=wx.ALL, border=8)
        szDlg.Add(self.rbdip, flag=wx.ALL, border=8)        
        szDlg.Add(self.szName, flag=wx.ALL|wx.ALIGN_RIGHT, border=10)
        szDlg.Add(sz, flag=wx.ALL, border=8)
        self.SetSizerAndFit(szDlg)

    def _createNameEditor(self):
        self.name = wx.TextCtrl(self, -1, "Untitled Set", 
            validator=ws.NameValidator(), size=(200,-1))
        self.szName = szName = wx.BoxSizer(wx.HORIZONTAL)
        szName.Add(ws.SmStaticText(self, -1, "Name"),
            flag=wx.RIGHT|wx.ALIGN_CENTER, border=6)
        szName.Add(self.name, flag=wx.ALIGN_CENTER|wx.RIGHT, border=10)


    def GetOptions(self):
        opts = {}
        if self.rbdip.GetValue():
            opts["dip-file"] = "$output"
        else:
            opts["sip-file"] = "$output"
        return opts
        
    def SetOptions(self, opts):
        if "dip-file" in opts:
            self.rbdip.SetValue(True)
        else:
            self.rbsip.SetValue(True)
            
    def GetResultName(self):
        return self.name.GetValue()

        