# -*- coding: utf-8 -*-
#
# xml0_importer.rb  (e-PRTR XML-0 Import)
# (c) ENDA, 2010
#
# by mlt
#
# Rel-1-1-8

module Gui_callbacks

  def hbox1_destroy_cb(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Gtk.main_quit
  end
  def b_exit_clicked_cb(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Gtk.main_quit
  end

  # HIER ist es spannend:
  def b_do_art15export_clicked_cb(widget)
    $testrun = false
    print widget.to_s, " Name: <", widget.name, ">\n"
    STDOUT.sync = true
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
    xml15_exporter = Art15_XML_Exporter.new(@glade)
    xml15_exporter.export
    #xml15_exporter.prepare_coord_data
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
  end

  def b_do_art17export_clicked_cb(widget)
    $testrun = false
    print widget.to_s, " Name: <", widget.name, ">\n"
    STDOUT.sync = true
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
    xml17_exporter = Art17_XML_Exporter.new(@glade)
    xml17_exporter.export
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
  end

  def b_do_art15testexport_clicked_cb(widget)
    $testrun = true
    print widget.to_s, " Name: <", widget.name, ">\n"
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
    xml15_exporter = Art15_XML_Exporter.new(@glade)
    xml15_exporter.export
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
  end

  def b_do_art17testexport_clicked_cb(widget)
    $testrun = true
    print widget.to_s, " Name: <", widget.name, ">\n"
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
    xml17_exporter = Art17_XML_Exporter.new(@glade)
    xml17_exporter.testexport
    @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
  end

  def button1_clicked_cb(widget)
    # Alisa-Knopf
    print widget.to_s, " Name: <", widget.name, ">\n"
    puts "@glade.widget_names"
    puts @glade.widget_names
    puts
    puts @glade["entry1"].name
    # Text in einem Entry setzen:
    @glade["entry1"].set_text "Alisa"

    # Knopf anders beschriften:
    @glade["b_exit"].set_label "Au revoir"
  end
  def menuaction_test1_activate_cb(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Test1.now
  end
  def menuaction_test2_activate_cb(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Test1.t2
  end

  def menuaction_art15_xml_file_open_activate_cb(widget)
    print widget.to_s, " widget name: <", widget.name, ">\n"
    @art15_xml_datei = nil
    dialog = Gtk::FileChooserDialog.new("Open File",
                                        @glade['main_window'],
                                        Gtk::FileChooser::ACTION_OPEN,
                                        nil,
                                        [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
                                        [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
    if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
      puts "filename = #{dialog.filename}"
      @art15_xml_datei = dialog.filename
    else
      puts "Die Dateiauswahl wurde abgebrochen."
    end
    dialog.destroy
  end

  def menuaction_check_art15_xml_file_for_dangling_refs_dp2tp_activate_cb(widget)
    print widget.to_s, " widget name: <", widget.name, ">\n"
    if @art15_xml_datei
      STDOUT.sync = true
      @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
      xml15_checker = Art15_XML_Checker.new(@glade, @art15_xml_datei)
      result = xml15_checker.check_dp2tp_links_with_schema
      @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
    else
      # Keinen Bock, jedes Mal neu die Datei herauszufrickeln:
      if nil
        STDOUT.sync = true
        @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::WATCH))
        @art15_xml_datei = "/home/mlt/dev/kommu/xml_export/daten/e_kommu_DE2014_1st_deployment_upload_release.xml"
        xml15_checker = Art15_XML_Checker.new(@glade, @art15_xml_datei)
        result = xml15_checker.check_dp2tp_links_with_schema
        @glade['main_window'].window.set_cursor(Gdk::Cursor.new(Gdk::Cursor::LEFT_PTR))
      end
      puts "Es gibt keinen Pfad auf eine Art.15 XML-Datei."
      dialog = Gtk::MessageDialog.new(@glade['main_window'],
                                      Gtk::Dialog::DESTROY_WITH_PARENT,
                                      Gtk::MessageDialog::WARNING,
                                      Gtk::MessageDialog::BUTTONS_CLOSE,
                                      "Es gibt keinen Pfad auf eine Art.15 XML-Datei.\nVielleicht öffnen Sie erst eine solche und probieren\nes dann noch einmal.")
      dialog.run
      dialog.destroy
    end
  end

  def imagemenuitem10_activate_cb(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Gnome::About.new(APPNAME, APPVERSION ,
      "Copyright (c) 2016 ENDA GmbH & Co. KG",
      ABOUT,
      ["M. Lüttgert (mlt)"], ["Es gibt keine Dokumentation"], nil).show
  end

  def gtk_main_quit(widget)
    print widget.to_s, " Name: <", widget.name, ">\n"
    Gtk.main_quit
  end
end
