#! /usr/bin/env ruby

require 'gtk2'
Gtk.init

#変数設定
conf = "arare_init.conf"


#ウィンドウを作る
window = Gtk::Window.new
window.title = "deepconv/arare arare-init.rb"
window.signal_connect("destroy") {Gtk.main_quit}
window.border_width = 10

#テーブルを使ってウィジットを配置するグリッドを準備する
#引数は, 行, 列, true/false である. 
table = Gtk::Table.new(2, 8, false)
window.add(table)

#ノートブックを準備
notebook = Gtk::Notebook.new
#ページインジケータを置く場所の設定
notebook.tab_pos = Gtk::POS_TOP
#テーブルにノートブックを追加
#引数は, ウィジット, left_attach, right_attach, top_attach, bottom_attach 
#1 行目から 8 行目の最上列にノートブックを張り付ける
table.attach_defaults(notebook, 0, 8, 0, 1)

#クラスの定義
class << notebook
  def rotate
    self.tab_pos = (self.tab_pos + 1) % 4
  end

  def tabsborder
    self.show_tabs = !self.show_tabs?
    self.show_border = !self.show_border?
  end
  
  def remove
    self.remove_page(self.page)
  end
  
  def scrollable
    self.scrollable = !self.scrollable?
  end
  
  def popup
    self.enable_popup = !self.enable_popup?
  end
end


def create_bbox(horizontal, title, spacing, layout)
	frame = Gtk::Frame.new(title)

	bbox = (horizontal ? Gtk::HButtonBox : Gtk::VButtonBox).new
	bbox.border_width = 5
	frame.add(bbox)

	bbox.layout_style = layout
	bbox.spacing = spacing

	bbox.add(Gtk::Button.new(Gtk::Stock::OK))
	bbox.add(Gtk::Button.new(Gtk::Stock::CANCEL))
	bbox.add(Gtk::Button.new(Gtk::Stock::HELP))

	frame
end


#実験名
exp_main = Gtk::VBox.new(false, 0)
notebook.append_page(exp_main, Gtk::Label.new("実験設定"))

exp_frame1 = Gtk::Frame.new("実験名")
exp_main.pack_start(exp_frame1, true, true, 10)

exp_frame2 = Gtk::Frame.new("実験資源")
exp_main.pack_start(exp_frame2, true, true, 10)

exp_frame3 = Gtk::Frame.new("組織")
exp_main.pack_start(exp_frame3, true, true, 10)

exp_frame4 = Gtk::Frame.new("ファイルのフラグ")
exp_main.pack_start(exp_frame4, true, true, 10)

exp_box1 = Gtk::VBox.new(false, 0)
exp_box1.border_width = 10
exp_frame1.add(exp_box1)

exp_box2 = Gtk::VBox.new(false, 0)
exp_box2.border_width = 10
exp_frame2.add(exp_box2)

exp_box3 = Gtk::VBox.new(false, 0)
exp_box3.border_width = 10
exp_frame3.add(exp_box3)

exp_box4 = Gtk::VBox.new(false, 0)
exp_box4.border_width = 10
exp_frame4.add(exp_box4)

exp_entry1 = Gtk::Entry.new
exp_entry1.max_length = 50
exp_entry1.text = "2D cumulus model for Jupiter?"
exp_entry1.select_region(0, -1)
exp_box1.pack_start(exp_entry1, true, true, 0)

exp_entry2 = Gtk::Entry.new
exp_entry2.max_length = 50
exp_entry2.text ="http://www.gfd-dennou.org/arch/deepconv/arare/" 
exp_entry2.select_region(0, -1)
exp_box2.pack_start(exp_entry2, true, true, 0)

exp_entry3 = Gtk::Entry.new
exp_entry3.max_length = 50
exp_entry3.text = "GFD_Dennou_Club deepconv project (arare)"
exp_entry3.select_region(0, -1)
exp_box3.pack_start(exp_entry3, true, true, 0)

exp_entry4 = Gtk::Entry.new
exp_entry4.max_length = 50
exp_entry4.text = "R50-TL3-TS03"
exp_entry4.select_region(0, -1)
exp_box4.pack_start(exp_entry4, true, true, 0)



#座標
grid_main = Gtk::VBox.new(false, 0)
notebook.append_page(grid_main, Gtk::Label.new("空間座標"))

grid_frame1 = Gtk::Frame.new("格子点数")
grid_main.pack_start(grid_frame1, true, true, 10)

grid_frame2 = Gtk::Frame.new("計算領域(X) [m]")
grid_main.pack_start(grid_frame2, true, true, 10)

grid_frame3 = Gtk::Frame.new("計算領域(Z) [m]")
grid_main.pack_start(grid_frame3, true, true, 10)

grid_box1 = Gtk::HBox.new(false, 0)
grid_box1.border_width = 10
grid_frame1.add(grid_box1)

grid_box2 = Gtk::HBox.new(false, 0)
grid_box2.border_width = 10
grid_frame2.add(grid_box2)

grid_box3 = Gtk::HBox.new(false, 0)
grid_box3.border_width = 10
grid_frame3.add(grid_box3)

grid_subframe1 = Gtk::Frame.new("X")
grid_box1.pack_start(grid_subframe1, true, true, 10)

grid_subframe2 = Gtk::Frame.new("Z")
grid_box1.pack_start(grid_subframe2, true, true, 10)

grid_subframe3 = Gtk::Frame.new("始点")
grid_box2.pack_start(grid_subframe3, true, true, 10)

grid_subframe4 = Gtk::Frame.new("終点")
grid_box2.pack_start(grid_subframe4, true, true, 10)

grid_subframe5 = Gtk::Frame.new("始点")
grid_box3.pack_start(grid_subframe5, true, true, 10)

grid_subframe6 = Gtk::Frame.new("終点")
grid_box3.pack_start(grid_subframe6, true, true, 10)

grid_subbox1 = Gtk::VBox.new(false, 0)
grid_subbox1.border_width = 10
grid_subframe1.add(grid_subbox1)

grid_subbox2 = Gtk::VBox.new(false, 0)
grid_subbox2.border_width = 10
grid_subframe2.add(grid_subbox2)

grid_subbox3 = Gtk::VBox.new(false, 0)
grid_subbox3.border_width = 10
grid_subframe3.add(grid_subbox3)

grid_subbox4 = Gtk::VBox.new(false, 0)
grid_subbox4.border_width = 10
grid_subframe4.add(grid_subbox4)

grid_subbox5 = Gtk::VBox.new(false, 0)
grid_subbox5.border_width = 10
grid_subframe5.add(grid_subbox5)

grid_subbox6 = Gtk::VBox.new(false, 0)
grid_subbox6.border_width = 10
grid_subframe6.add(grid_subbox6)

grid_entry1 = Gtk::Entry.new
grid_entry1.max_length = 50
grid_entry1.text = "100"
grid_entry1.select_region(0, -1)
grid_subbox1.pack_start(grid_entry1, true, true, 0)

grid_entry2 = Gtk::Entry.new
grid_entry2.max_length = 50
grid_entry2.text = "100"
grid_entry2.select_region(0, -1)
grid_subbox2.pack_start(grid_entry2, true, true, 0)

grid_entry3 = Gtk::Entry.new
grid_entry3.max_length = 50
grid_entry3.text = "0"
grid_entry3.select_region(0, -1)
grid_subbox3.pack_start(grid_entry3, true, true, 0)

grid_entry4 = Gtk::Entry.new
grid_entry4.max_length = 50
grid_entry4.text = "10000"
grid_entry4.select_region(0, -1)
grid_subbox4.pack_start(grid_entry4, true, true, 0)

grid_entry5 = Gtk::Entry.new
grid_entry5.max_length = 50
grid_entry5.text = "0"
grid_entry5.select_region(0, -1)
grid_subbox5.pack_start(grid_entry5, true, true, 0)

grid_entry6 = Gtk::Entry.new
grid_entry6.max_length = 50
grid_entry6.text = "10000"
grid_entry6.select_region(0, -1)
grid_subbox6.pack_start(grid_entry6, true, true, 0)


#時刻
time_main = Gtk::VBox.new(false, 0)
notebook.append_page(time_main, Gtk::Label.new("数値積分"))

time_frame1 = Gtk::Frame.new("タイムステップ")
time_main.pack_start(time_frame1, true, true, 10)

time_frame2 = Gtk::Frame.new("積分時間 [s]")
time_main.pack_start(time_frame2, true, true, 10)

time_frame3 = Gtk::Frame.new("クランク・ニコルソン法の係数")
time_main.pack_start(time_frame3, true, true, 10)

time_box1 = Gtk::HBox.new(false, 0)
time_box1.border_width = 10
time_frame1.add(time_box1)

time_box2 = Gtk::VBox.new(false, 0)
time_box2.border_width = 10
time_frame2.add(time_box2)

time_box3 = Gtk::VBox.new(false, 0)
time_box3.border_width = 10
time_frame3.add(time_box3)

time_subframe1 = Gtk::Frame.new("短い")
time_box1.pack_start(time_subframe1, true, true, 10)

time_subframe2 = Gtk::Frame.new("長い")
time_box1.pack_start(time_subframe2, true, true, 10)

time_subbox1 = Gtk::VBox.new(false, 0)
time_subbox1.border_width = 10
time_subframe1.add(time_subbox1)

time_subbox2 = Gtk::VBox.new(false, 0)
time_subbox2.border_width = 10
time_subframe2.add(time_subbox2)

time_entry1 = Gtk::Entry.new
time_entry1.max_length = 50
time_entry1.text = "0.1"
time_entry1.select_region(0, -1)
time_subbox1.pack_start(time_entry1, true, true, 0)

time_entry2 = Gtk::Entry.new
time_entry2.max_length = 50
time_entry2.text = "10.0"
time_entry2.select_region(0, -1)
time_subbox2.pack_start(time_entry2, true, true, 0)

time_entry3 = Gtk::Entry.new
time_entry3.max_length = 50
time_entry3.text = "1000"
time_entry3.select_region(0, -1)
time_box2.pack_start(time_entry3, true, true, 0)

time_entry4 = Gtk::Entry.new
time_entry4.max_length = 50
time_entry4.text = "0.5"
time_entry4.select_region(0, -1)
time_box3.pack_start(time_entry4, true, true, 0)



#境界条件
bc_main = Gtk::VBox.new(false, 0)
notebook.append_page(bc_main, Gtk::Label.new("境界条件"))

bc_frame_x = Gtk::Frame.new("X 方向")
bc_main.pack_start(bc_frame_x, true, true, 10)

bc_x_box = Gtk::HBox.new(false, 0)
bc_x_box.border_width = 10
bc_frame_x.add(bc_x_box)

bc_frame_z = Gtk::Frame.new("Z 方向")
bc_main.pack_start(bc_frame_z, true, true, 10)

bc_z_box = Gtk::HBox.new(false, 0)
bc_z_box.border_width = 10
bc_frame_z.add(bc_z_box)

x_button = Gtk::RadioButton.new("周期境界")
x_button.active = true
bc_x_box.pack_start(x_button, true, true, 0)

x_button = Gtk::RadioButton.new(x_button, "固定壁")
bc_x_box.pack_start(x_button, true, true, 0)

x_button = Gtk::RadioButton.new(x_button, "フリースリップ")
bc_x_box.pack_start(x_button, true, true, 0)

z_button = Gtk::RadioButton.new("周期境界")
bc_z_box.pack_start(z_button, true, true, 0)

z_button = Gtk::RadioButton.new(z_button, "固定壁")
z_button.active = true
bc_z_box.pack_start(z_button, true, true, 0)

z_button = Gtk::RadioButton.new(z_button, "フリースリップ")
bc_z_box.pack_start(z_button, true, true, 0)


#線形計算パッケージ
lin_main = Gtk::VBox.new(false, 0)
notebook.append_page(lin_main, Gtk::Label.new("行列計算"))

lin_frame_x = Gtk::Frame.new("行列計算パッケージ")
lin_main.pack_start(lin_frame_x, true, true, 10)

lin_x_box = Gtk::HBox.new(false, 0)
lin_x_box.border_width = 10
lin_frame_x.add(lin_x_box)

lin_button = Gtk::RadioButton.new("SSL 2")
lin_button.active = true
lin_x_box.pack_start(lin_button, true, true, 0)

lin_button = Gtk::RadioButton.new(lin_button, "LAPACK (未実装!)")
lin_x_box.pack_start(lin_button, true, true, 0)



#現在表示するページ
notebook.page = 6
notebook.scrollable = true
notebook.enable_popup = true

button = Gtk::Button.new("next page")
button.signal_connect("clicked") {notebook.next_page}
table.attach_defaults(button, 0, 1, 1, 2)

button = Gtk::Button.new("prev page")
button.signal_connect("clicked") {notebook.prev_page}
table.attach_defaults(button, 1, 2, 1, 2)

button = Gtk::Button.new("close")
button.signal_connect("clicked") {
  Gtk.main_quit
}
table.attach_defaults(button, 2, 3, 1, 2)


button = Gtk::Button.new("初期値生成")
button.signal_connect("clicked") {
  #ファイルのオープン
  OutputFile = open(conf, "w")

  puts "writeing Output File.... "

  OutputFile.puts  '! Config file for deepconv/arare '
  OutputFile.print '!   generated by arare-int.rb at ', Time.now
  OutputFile.print "\n \n"

  OutputFile.puts  "&fileset"
  OutputFile.print "  ss_RegFile = ", io_entry2.text, "\n"
  OutputFile.print "  fs_RegFile = ", io_entry3.text, "\n"
  OutputFile.print "  sf_RegFile = ", io_entry4.text, "\n"
  OutputFile.print "  ss_DimFile = ", io_entry5.text, "\n"
  OutputFile.print "  fs_DimFile = ", io_entry6.text, "\n"
  OutputFile.print "  sf_DimFile = ", io_entry7.text, "\n"
  OutputFile.puts  '/'

  OutputFile.puts  "&expname"
  OutputFile.print "  ExpTitle = ", exp_entry1.text, "\n"
  OutputFile.print "  ExpSrc   = ", exp_entry2.text, "\n"
  OutputFile.print "  ExpInst  = ", exp_entry3.text, "\n"
  OutputFile.puts  '/'

  OutputFile.puts  "&gridset"
  OutputFile.print "  NX    = ", grid_entry1.text, "\n"
  OutputFile.print "  NZ    = ", grid_entry2.text, "\n"
  OutputFile.print "  Xmin  = ", grid_entry3.text, "\n"
  OutputFile.print "  Xmax  = ", grid_entry4.text, "\n"
  OutputFile.print "  Zmin  = ", grid_entry5.text, "\n"
  OutputFile.print "  Zmax  = ", grid_entry6.text, "\n"
  OutputFile.puts  '/'

  OutputFile.puts  "&timeset"
  OutputFile.print "  DelTLong  = ", time_entry1.text, "\n"
  OutputFile.print "  DelTShort = ", time_entry2.text, "\n"
  OutputFile.print "  TimeInt   = ", time_entry3.text, "\n"
  OutputFile.print "  Beta      = ", time_entry4.text, "\n"
  OutputFile.puts  '/'

  OutputFile.puts  "&bcset"
  i = 0
  for radio in x_button.group
    if radio.active?
#      OutputFile.print "  BcX = ",  bc_type(i) "\n"
    end
    i = i + 1
  end
  i = 0
  for radio in z_button.group
    if radio.active?
      if i == 0
        OutputFile.print "  BcX = Free \n"
      elsif i == 1
        OutputFile.print "  BcX = Rigid \n"
      elsif i == 2
        OutputFile.print "  BcX = Cyclic \n"
      end
    end
    i = i + 1
  end
  OutputFile.puts  '/'

  OutputFile.puts  "&linlib"
  i = 0
  for radio in lin_button.group
    if radio.active?
      if i == 1
        OutputFile.print "  LLib = ssl2 \n"
      elsif 
        puts "Not Supported LAPACK!"
      end
    end
    i = i + 1
  end
  OutputFile.puts  '/'
  
  OutputFile.close
}
table.attach_defaults(button, 3, 4, 1, 2)


window.show_all
Gtk.main

