class JEliza:
	
	debug = 0
	file = "JEliza.txt"
	language = "roh"
	system = "net"
	
	def __init__(self):
		self.util = Util()
		self.file = self.util.getProfilePath(self.util.getCurrentProfile()) + "/input.txt";
		self.createSwapFiles()
		self.markov = AIDatabase()
	
	def getAnswer(self, fra):
		fra = fra.strip().lower()
		fra = fra.replace(u"\xe4", "ae");
		fra = fra.replace(u"\xf6", "oe");
		fra = fra.replace(u"\xfc", "ue");
		
		self.markov.system = self.system
		self.markov.language = self.language
#		self.markov.file = self.file
		self.markov.debug = self.debug
		output = self.markov.reply(fra)
		self.markov.learn(fra)
		print self.markov.language
		print self.language
		print output
		return output
	
	def createSwapFiles(self):
		""" No Swapfiles to create """
		
	def genDatabase(self):
		self.markov.system = self.system
		self.markov.language = self.language
#		self.markov.file = self.file
		self.markov.debug = self.debug
		self.markov.genDatabase()
		
	def clearDatabase(self):
		self.markov.system = self.system
		self.markov.language = self.language
#		self.markov.file = self.file
		self.markov.debug = self.debug
		self.markov.clearDatabase()
class JElizaWX:
        
    def __init__(self):
       self.app = wx.App(redirect = False)
      
    def gui(self):
        global guiLang
        
        """ Initialisation """
        self.language = "roh"
        self.util = Util()
        self.util.chProfile("default")
        self.dirname = "."
        
        """ Create new JEliza """
        self.jel = JEliza()
        
        """ Create Frame """
        self.frame = wx.Frame(None, wx.ID_ANY, GetString(guiLang, "0001"), 
        wx.DefaultPosition, wx.Size(800, 700))
        self.frame.CreateStatusBar()
        
        """ Create Sizers """
        self.sizer = wx.GridBagSizer()
        self.topsizer = wx.BoxSizer(wx.VERTICAL)
        self.control = wx.GridBagSizer()
        self.langs = wx.GridBagSizer()
        
        """ Create some Comboboxes """
        self.langin = wx.ComboBox(self.frame, 30, "Deutsch", 
                                  choices = ["Deutsch", "Englisch", "Spanisch", "Italienisch", "Franzoesisch", "Portugiesisch", "andere Sprache", "Finnisch"], 
                                  style=wx.CB_DROPDOWN)
        self.lang = wx.ComboBox(self.frame, 31, "Roh", 
                                choices = ["Deutsch", "Englisch", "Spanisch", "Italienisch", "Franzoesisch", "Portugiesisch", "Finnisch"], 
                                style=wx.CB_DROPDOWN)
        wx.EVT_COMBOBOX(self.frame, 30, self.chLang)
        wx.EVT_COMBOBOX(self.frame, 31, self.chLang)
        
        """ Create some "Statictext"s """
        statictext = wx.StaticText(self.frame, 50, 
                                "\n" + " " + GetString(guiLang, "0002") + ":" + "\n")
        statictext.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana'))
        self.control.Add(statictext, (0, 0), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.control.Add(wx.StaticText(self.frame, 50, GetString(guiLang, "0003") + ":"), (1, 0), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.control.Add(self.langin, (2, 0), wx.DefaultSpan, wx.EXPAND)
        self.control.Add(wx.StaticText(self.frame, 50, GetString(guiLang, "0004") + ":"), (3, 0), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.control.Add(self.lang, (4, 0), wx.DefaultSpan, wx.EXPAND)
 
        statictext = wx.StaticText(self.frame, 50, 
                                   "\n " + GetString(guiLang, "0005") + ":" + "\n")
        statictext.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False, 'Verdana'))
 
        self.control.Add(statictext, (6, 0), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.gendata = wx.Button(self.frame, 70, GetString(guiLang, "0006"))
        self.control.Add(self.gendata, (7, 0), wx.DefaultSpan, wx.EXPAND)
        self.cleardata = wx.Button(self.frame, 71, GetString(guiLang, "0007"))
        self.control.Add(self.cleardata, (8, 0), wx.DefaultSpan, wx.EXPAND)
        self.loadonlinewiss = wx.Button(self.frame, 72, GetString(guiLang, "0008"))
        self.control.Add(self.loadonlinewiss, (9, 0), wx.DefaultSpan, wx.EXPAND)
        self.loadstdwiss = wx.Button(self.frame, 73, GetString(guiLang, "0009"))
        self.control.Add(self.loadstdwiss, (10, 0), wx.DefaultSpan, wx.EXPAND)
 
        """ Create some Events """
        wx.EVT_BUTTON(self.frame, 70, self.genDatabase)
        wx.EVT_BUTTON(self.frame, 71, self.clearDatabase)
        wx.EVT_BUTTON(self.frame, 72, self.loadonlinewissen)
        wx.EVT_BUTTON(self.frame, 73, self.loadstandardwissen)
        
        """ Add Menubar """
        self.menubar = wx.MenuBar()
        self.datei = wx.Menu()
        self.help = wx.Menu()
        
        """ Create some Widgets """
        self.button = wx.Button(self.frame, 10, GetString(guiLang, "0010"))
        wx.EVT_BUTTON(self.frame, 10, self.say)
        self.entry = wx.TextCtrl(self.frame, 555, "", style=wx.TE_PROCESS_ENTER)
        wx.EVT_TEXT_ENTER(self.frame, 555, self.say)
        self.log = wx.TextCtrl(self.frame, 9, style=wx.TE_MULTILINE) 
        self.gauge = wx.Gauge(self.frame, -1, 100, (10, 70), (390, 25))#, wx.VERTICAL)
        
        """ Create Menu entries """
        self.datei.Append(101, GetString(guiLang, "0011"), '')
        self.datei.Append(102, GetString(guiLang, "0012"), '')
        self.datei.AppendSeparator()
        self.datei.Append(103, GetString(guiLang, "0013"), '')
        self.datei.Append(104, GetString(guiLang, "0014"), '')
        
        self.help.Append(201, GetString(guiLang, "0015"), '')
       
        self.frame.Bind(wx.EVT_MENU, self.openDialog, id=101)
        self.frame.Bind(wx.EVT_MENU, self.saveDialog, id=102)
        self.frame.Bind(wx.EVT_MENU, self.openWissen, id=103)
        self.frame.Bind(wx.EVT_MENU, self.saveWissen, id=104)
        
        self.frame.Bind(wx.EVT_MENU, self.about, id=201)
        
        self.menubar.Append(self.datei, GetString(guiLang, "0016"))
        self.menubar.Append(self.help, GetString(guiLang, "0017"))
        self.frame.SetMenuBar(self.menubar)
        
        """ Add some widgets to a sizer """
        self.sizer.Add(self.log, (0, 0), (1, 2), wx.EXPAND)
        self.sizer.Add(self.control, (0, 2), (1, 2), wx.ALIGN_LEFT)
        self.sizer.Add(self.entry, (1, 0), (1, 3), wx.ALIGN_CENTER | wx.EXPAND)
        self.sizer.Add(self.gauge, (2, 0), (1, 4), wx.ALIGN_CENTER | wx.EXPAND)
        self.sizer.Add(self.button, (1, 3), wx.DefaultSpan, wx.ALIGN_RIGHT)
        self.sizer.AddGrowableRow(0)
        self.sizer.AddGrowableCol(1)
        self.sizer.AddGrowableCol(0)
        
        """ Set the Sizer """
        self.frame.SetSizer(self.sizer)
        
        """ Print some infos """
        print ""
        print "+---------------------------------------------------------------+"
        print "| JEliza                                            Version 2.0 |"
        print "| Copyright 2006 - 2007 by Tobias Schulz                        |"
        print "| License: GNU Gerneral Public License, www.gnu.org      |"
        print "+---------------------------------------------------------------+"
        print GetString(guiLang, "0018")
        print "+---------------------------------------------------------------+"
        print ""
        print "Application Starter : I 'm going to start the application now"

        """ Show the Window """
        self.frame.Center()       
        self.frame.Show(True)
        
        """ Check for updates """
        self.checkForUpdates()       
        print "Application Starter : OK ; I checked for updates"
       
        gau = GaugeThread(self.gauge, self.frame)
        gau.start()
        
        self.setGaugeValue(0)
        
        EVT_RESULT(self.frame, self.OnResult) 
        
        print "Application Starter : OK"

        self.app.MainLoop()
        
    def say(self, x):
        self.jel.language = self.language
        self.jel.system = "markov"
        self.util = Util()
        self.jel.file = "JEliza.txt"
        self.jel.debug = 1
        input = self.entry.GetLineText(0) 

        self.win = self	
        self.input = input
        out = self.jel.getAnswer(input)
        self.ready = True
        self.win.entry.Clear()
        self.win.log.AppendText("Mensch: " + input.decode("iso-8859-1").encode("utf-8") + "\n")
        self.win.log.AppendText("JEliza: " + out.decode("iso-8859-1").encode("utf-8") + "\n")
        print "Benutzte Sprache: " + self.win.jel.language
        if out == ".":
            dlg2 = wx.MessageDialog(self.win.frame, "Es kann sein, dass JEliza sich nicht in "
                               "der Ausgabesprache ausdruecken kann, da " 
                               "der notwendige Programmteil noch nicht implementiert wurde."
                               , "Language Error", wx.OK)
            dlg2.ShowModal()
            dlg2.Destroy()
        

            
    def setGaugeValue(self, x):
        fp = open("gauge.txt", "w")
        fp.write(str(x))
        fp.close()
 
    def checkForUpdates(self):
        up = UpdateChecker()
        up.run()

    def genDatabase(self, x):
        self.jel.language = self.language
        self.util = Util()
        self.jel.file = self.util.getProfilePath(self.util.getCurrentProfile()) + "/input.txt"
        self.jel.debug = 0
        self.jel.genDatabase()
 
    def clearDatabase(self, x):
        self.jel.language = self.language
        self.util = Util()
        self.jel.file = self.util.getProfilePath(self.util.getCurrentProfile()) + "/input.txt"
        self.jel.debug = 0
        self.jel.clearDatabase()
        
    def chLang(self, event):
        inl = self.langin.GetValue().lower().strip().replace("andere sprache", "roh")
        outl = self.lang.GetValue().lower().strip()
        if inl == "deutsch" and outl == "deutsch":
            self.language = "roh"
        else:
            self.language = inl + " -> " + outl
        print self.language
        
    def chSys(self, event):
        sys = self.useSystem.GetValue().lower().strip()
        sys = sys.replace("neuronales netz", "net")
        sys = sys.replace("hidden markov modell", "markov")
        self.system = sys
        print self.system
        
    def saveDialog(self, x):
        dlg = wx.FileDialog(self.frame, "Gespaech speichern", self.dirname, "JEliza-Gespraech.gsp", "*.gsp", wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f = os.path.join(self.dirname, self.filename)
            if f == 0 or f == "":
                return
            if not f.endswith(".gsp"):
                f = f + ".gsp"
            fp=open(f, 'w')
            try:
                fp.writelines(self.log.GetValue())
            except IOError:
                dlg2 = wx.MessageDialog(self.frame, "Konnte Gespraech nicht speichern", "Error", wx.OK)
                dlg2.ShowModal()
                dlg2.Destroy()
                return
            dlg2 = wx.MessageDialog(self.frame, "Gespraech wurde gespeichert", "Nachricht", wx.OK)
            dlg2.ShowModal()
            dlg2.Destroy()
        dlg.Destroy()        
        
    def openDialog(self, x):
        dlg = wx.FileDialog(self.frame, "Gespaech laden", self.dirname, "", "*.gsp", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f = os.path.join(self.dirname, self.filename)
            if f == 0 or f == "":
                return
            fp=open(f, 'r')
            try:
                self.log.SetValue(fp.read())
            except IOError:
                dlg2 = wx.MessageDialog(self.frame, "Konnte Gespraech nicht laden", "Error", wx.OK)
                dlg2.ShowModal()
                dlg2.Destroy()
                return
            dlg2 = wx.MessageDialog(self.frame, "Gespraech wurde geladen", "Nachricht", wx.OK)
            dlg2.ShowModal()
            dlg2.Destroy()
        dlg.Destroy()        
        
    def saveWissen(self, x):
        dlg = wx.FileDialog(self.frame, "Wissen speichern", self.dirname, "JEliza-Wissen.jwwf", "*.jwwf", wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f = os.path.join(self.dirname, self.filename)
            if f == 0 or f == "":
                return
            if not f.endswith(".jwwf"):
                f = f + ".jwwf"
            try:
                jwwf = JWWF()
                jwwf.save(self.util.getCurrentProfile(), f)
            except IOError:
                dlg2 = wx.MessageDialog(self.frame, "Konnte Wissen nicht speichern", "Error", wx.OK)
                dlg2.ShowModal()
                dlg2.Destroy()
                return 
 
            dlg2 = wx.MessageDialog(self.frame, "Wissen wurde gespeichert", "Nachricht", wx.OK)
            dlg2.ShowModal()
            dlg2.Destroy()
        dlg.Destroy()        
        
    def openWissen(self, x):
        dlg = wx.FileDialog(self.frame, "Wissen laden", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f = os.path.join(self.dirname, self.filename)
            if f == 0 or f == "":
                return
            fp=open(f, 'r')
            try:
                jwwf = JWWF()
                jwwf.load(self.util.getCurrentProfile(), f)
            except IOError:
                dlg2 = wx.MessageDialog(self.frame, "Konnte Wissen nicht laden", "Error", wx.OK)
                dlg2.ShowModal()
                dlg2.Destroy()
                return
            dlg2 = wx.MessageDialog(self.frame, "Wissen wurde geladen", "Nachricht", wx.OK)
            dlg2.ShowModal()
            dlg2.Destroy()
        dlg.Destroy()     
       
    def about(self, x):
        fp = open("version.ver", "r")
        try:
             ver = fp.read().strip()
        except IOError:
             ver = "?"
        
        txt = ""
        
        txt += "JEliza " + ver + "\n"
        txt += "Copyright 2006 - 2007 by Tobias Schulz, http://jeliza.berlios.de" + "\n"
        txt += "License: GNU Gerneral Public License, www.gnu.org" + "\n"
        txt += "e-Mail: [email protected] (Tobias Schulz)" + "\n" + "\n" + "\n"
        txt += "Autoren: / Autors:" + "\n"
        txt += "- Tobias Schulz (Projektleiter / project leader)" + "\n"
        txt += "- Marcel Kunzmann (Betatester / beta tester)" + "\n"
        txt += "- und Andere / and some others" + "\n"
        txt += "\n" + "\n"
        txt += GetString(guiLang, "0018").replace(" |\n| ", "\n").replace("|", "").strip() + "\n"
#        print "+---------------------------------------------------------------+"

        dlg2 = wx.MessageDialog(self.frame, txt, 
        "JEliza - Version " + ver, wx.OK)
        dlg2.ShowModal()
        dlg2.Destroy()
        
    def loadonlinewissen(self, x):
        try:
            con = urllib.urlopen("http://jeliza.berlios.de/jeliza-applet/jeliza.sat.brn.php").read()
        except IOError:
            dlg2 = wx.MessageDialog(self.frame, "Herunterladen war nicht moeglich"
                             "\nKlicken sie auf OK!", 
                             "Herunterladen", wx.OK)
            dlg2.ShowModal()
        fp = open("tmp.tmp", "w")
        fp.write(con)
        fp.close()
        jwwf = JWWF()
        jwwf.load(self.util.getCurrentProfile(), "tmp.tmp")
        self.genDatabase("")
        dlg2 = wx.MessageDialog(self.frame, "Wissen der online-Version geladen", 
                         "Erfolg", wx.OK)
        dlg2.ShowModal()
       
    def loadstandardwissen(self, x):
        try:
            con = urllib.urlopen("http://jeliza.berlios.de/addons/jeliza.standard.jwwf").read()
        except IOError:
            dlg2 = wx.MessageDialog(self.frame, "Herunterladen war nicht moeglich"
                             "\nKlicken sie auf OK!", 
                             "Herunterladen", wx.OK)
            dlg2.ShowModal()
        fp = open("tmp.tmp", "w")
        fp.write(con)
        fp.close()
        jwwf = JWWF()
        jwwf.load(self.util.getCurrentProfile(), "tmp.tmp")
        self.genDatabase("")
        dlg2 = wx.MessageDialog(self.frame, "Wissen der Standard-Version geladen", 
                         "Erfolg", wx.OK)
        dlg2.ShowModal()
       
    def initWortschatz(self, x):
        """ """
       
    def OnResult(self, event):
        self.win = self
        if event.data.startswith("gauge"):
            self.gauge.SetValue(int(event.data.split("=")[1]))
            return
        elif event.data.startswith("endOfAsk"):
            #            input = self.win.entry.GetLineText(0)
        #self.win.input = input
        #out = self.jel.getAnswer(input)
        #wx.PostEvent(self._notify_window, ResultEvent("endOfAsk=" + out.replace("=", " ")))
            input = self.input
            out = event.data.split("=")[1]
            self.ready = True
            self.win.entry.Clear()
            self.win.log.AppendText("Mensch: " + input.decode("iso-8859-1").encode("utf-8") + "\n")
            self.win.log.AppendText("JEliza: " + out.decode("iso-8859-1").encode("utf-8") + "\n")
            print "Benutzte Sprache: " + self.win.jel.language
            if out == ".":
                dlg2 = wx.MessageDialog(self.win.frame, "Es kann sein, dass JEliza sich nicht in "
                                    "der Ausgabesprache ausdruecken kann, da " 
                                    "der notwendige Programmteil noch nicht implementiert wurde."
                                    , "Language Error", wx.OK)
                dlg2.ShowModal()
                dlg2.Destroy()
        
            time.sleep(2)
            self.win.setGaugeValue(0)