#!/usr/bin/ruby
# an implementation of the Steak translation dictionary in Ruby

require 'ftools'

class DictionaryFile < File
  def initialize(fileName)
    @fileName = fileName
#    @foundGermanValue = String.new("Deutsch <-> Englisch")
    super(fileName, aModeString="r")
  end
  
  def to_s
    "Database: #{@fileName}"
  end
  
  def printFirstLine               # testing purpose
    readline
  end
  
  def findByGermanEntry(searchEntry)
    rewind
    @searchEntry = String.new(searchEntry)
    foundGermanValue = Array.new
    foundGermanValue << "DE <-> EN"
    foundGermanValue << "\n"
    read.each_line{
      |line|
      if line.include?(searchEntry) then
        foundGermanValue << line
      end
    }
    return foundGermanValue
  end
end


print("Testing interface to Dictionary File...\n");
aDictionaryFile = DictionaryFile.new("/home/kristian/Projects/ruby/rubySteak/ger-eng.txt")

# -------------------------- UI --------------------------

require 'gtk2'

Gtk.init
window = Gtk::Window.new
window.signal_connect("delete_event"){
  puts "Delete event occured!"
  false
}
window.signal_connect("destroy"){
  puts "Destroy event occured! Closing UI."
  Gtk.main_quit
}

labelText = Gtk::Label.new("Enter text and press translate")
entryText = Gtk::Entry.new
buttonText = Gtk::Button.new("Translate")
textView = Gtk::TextView.new

vBoxMain  = Gtk::VBox.new(false, 3)
hBoxEntry = Gtk::HBox.new(false, 3)

window.border_width = 5
hBoxEntry.add(entryText)
hBoxEntry.add(buttonText)
vBoxMain.add(labelText)
vBoxMain.add(hBoxEntry)
vBoxMain.add(textView)

window.add(vBoxMain)
window.title = "rubySteak"
window.show_all

#textView.buffer.insert_at_cursor(aDictionaryFile.printFirstLine)

# print aDictionaryFile.findByGermanEntry(/Welt/)
# textView.buffer.insert_at_cursor(aDictionaryFile.findByGermanEntry(/::saufen/))

entryText.signal_connect("activate"){
  textView.buffer.delete(textView.buffer.start_iter, textView.buffer.end_iter)
  aDictionaryFile.findByGermanEntry(entryText.text).each{
    |line|
    textView.buffer.insert_at_cursor(line)
  }
}

# window.fullscreen

Gtk.main


aDictionaryFile.close
print("File closed. \n")

# -------------------------- DONE ------------------------

