#!/usr/bin/env ruby

POS_VERSION = '0.01'
POS_TITLE   = 'opossum'

require 'gtk'
require 'lib/PosClasses.rb'
require 'lib/Packet.rb'
require 'lib/PosWin.rb'

class PacketListener
  def item_sync(max)
    @max = max.to_i
    @count = 0
    @sync_win = SyncDialog.new(max.to_i)
    @sync_bar = @sync_win.sync_bar
  end
  def destroy_sync_win(dummy)
    @sync_win.destroy
  end
  def insert_item(item_id, item_name, price, barcode, maker)
    args = [item_id, item_name, price, barcode, maker]
    args = args.collect do |arg| (arg.length == 0 ? nil : arg) end
    sql = "insert into tbl_item(item_id, item_name, price, barcode, maker) "+
    "values (?, ?, ?, ?, ?) "
    begin
      @dbh.do(sql, *args)
      @count += 1
      @sync_bar.update(Float(@count.to_i)/@max)
    rescue
      puts $!, $@
    end
  end
  def delete_item(item_id)
    sql = "delete from tbl_item where item_id = ? "
    @dbh.do(sql, item_id)
    $item_list_updated = true    
  end
  def update_item(item_id, item_name, price, barcode, maker)
    args = [item_id, item_name, price, barcode, maker]
    args = args.collect do |arg| (arg.length == 0 ? nil : arg) end
    sql = "update tbl_item set item_name = ?, "+
          "price = ?, barcode = ?, maker = ? "+
	  "where item_id = ? "
    # puts args
    # @dbh.do(sql, *args)
    @dbh.do(sql, item_name, price, barcode, maker, item_id)    
    $item_list_updated = true
  end
end

$item_list_updated = false
conf = PosConfig.new("#{ENV['HOME']}/.opossum-conf.xml")

fs = conf['fontsize']
Gtk::RC::parse_string <<EOS
style "default"
{
  fontset = "-alias-fixed-medium-r-normal--#{fs}-*"
}
widget_class "*" style "default"
EOS

register = Register.new(conf)
keydecoder = Keydecoder.new(register)

poswin = PosWin.new(keydecoder, conf)

poswin.start
