#!/usr/bin/ruby
# encoding: utf-8

require 'gtk2'
require 'vte'
require "rexml/document"
require 'optparse'
require 'InfoScreen'
require 'SambaScreen'
require 'WindowsScreen'
require 'PhotorecScreen'
# require 'MobilerecScreen'
require 'RescueScreen'
require 'VirusScreen'
require 'ChntpwScreen'
require 'DeleteScreen'
require 'CloneScreen'
require 'TileHelpers'
require 'NetworkScreen' 
require 'CheckScreen'
require 'UsbInstallScreen'
require 'DvdRescueScreen' 
require 'ImageScreen'
require 'PcInspectorScreen'
require 'MfsSinglePartition.rb'
require 'MfsDiskDrive.rb'
require 'MfsTranslator.rb'


$lang = ENV['LANGUAGE'][0..1]
$lang = ENV['LANG'][0..1] if $lang.nil?
$lang = "en" if $lang.nil?
@tl = MfsTranslator.new($lang, "assistant.xml")

@simulate = Array.new
@deco = true
@start = nil
@forcescreen = false
opts = OptionParser.new 
opts.on('-s', '--simulate', :REQUIRED ) { |i| i.split(",").each { |j| @simulate.push(j) } }
opts.on('--no-deco') { @deco = false } 
opts.on('--start', :REQUIRED ) { |i| 
	@start = i.strip 
	@forcescreen = true
} 
opts.parse!

TileHelpers.umount_all

window = Gtk::Window.new
window.border_width = 10
window.window_position = Gtk::Window::POS_CENTER_ALWAYS

if @deco == false
	window.deletable = false
	window.decorated = false
	window.allow_grow = false
	window.allow_shrink = false
end

window.title = "Notfall-DVD 6.0"
window.signal_connect('delete_event') { false }
window.signal_connect('destroy') { 
	system("rm /var/run/lesslinux/assistant_running")
	Gtk.main_quit
}

# Layout table

table = Gtk::Table.new(3,    6,    false)
table.row_spacings = 10
table.column_spacings = 10

button = Array.new
imagefiles = [  "reparieren", "viren", "deletelightgreen", "password120", "recover", "mobilerec", "samba", "rescue", "shutdown", "desktop", "info", "usbinstall", "dvdrescue", "pcinspector" ]
0.upto(imagefiles.size - 1) { |n|
	if File.exists?("#{imagefiles[n]}.#{$lang}.png")
		button[n] = Gtk::EventBox.new.add Gtk::Image.new("#{imagefiles[n]}.#{$lang}.png")
	else
		button[n] = Gtk::EventBox.new.add Gtk::Image.new("#{imagefiles[n]}.png")
	end
}

desctexts = [ 
	@tl.get_translation("maincrash"),
	@tl.get_translation("mainvirus"),
	@tl.get_translation("mainshred"),
	@tl.get_translation("mainchntpw"), 
	@tl.get_translation("mainresc"), 
	@tl.get_translation("maindigi"), 
	@tl.get_translation("mainsave"), 
	@tl.get_translation("maincloud"), 
	@tl.get_translation("mainshutdown"),
	@tl.get_translation("maindesk"), 
	@tl.get_translation("mainmore"), 
	@tl.get_translation("mainusb"), 
	@tl.get_translation("maindvd"),
	@tl.get_translation("pcinspector")
]
descriptions = Hash.new
infolabel = Gtk::Label.new
infolabel.set_markup("<span color='white'>" + @tl.get_translation("mainmouse") + "</span>")
infolabel.wrap = true
infolabel.width_request = 500

button.each { |b| b.set_size_request(120, 120) }
[ 0, 1, 4, 5 ].each { |n|
	button[n].set_size_request(250, 120)
}
# [ 2, 3, 6, 7, 8, 9, 10, 11, 12 ].each { |n|
#	button[n].set_size_request(120, 120)
# }


0.upto(13) { |n|
	descriptions[button[n]] = desctexts[n]
	button[n].signal_connect('enter-notify-event') { |x, y|
		# puts "Entered: " + descriptions[x]
		infolabel.set_markup("<span color='white'>" + descriptions[x] + "</span>")
	}
	button[n].signal_connect('button-release-event') { |x, y|
		if y.button == 1 
			# puts "Clicked: " + descriptions[x]
		elsif y.button == 2 && x == button[10]
			puts "Middle clicked info"
			system("/usr/bin/Terminal --hide-menubar -e '/usr/bin/nInvaders -l 3' &") 
			# system("/usr/bin/xfce4-terminal --hide-menubar -e /usr/bin/nInvaders &")
		elsif y.button == 3 && x == button[10]
			system("wicd-gtk -n &") 
		end
	}
}

options = Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK
table.attach(button[0],  0,  1,  0,  1, options, options, 0,    0)
table.attach(button[4],  1,  3,  0,  1, options, options, 0,    0)
table.attach(button[1],  3,  5,  0,  1, options, options, 0,    0)
table.attach(button[7],  5,  6,  0,  1, options, options, 0,    0)

table.attach(button[5],  0,  1,  1,  2, options, options, 0,    0)
table.attach(button[3],  1,  2,  1,  2, options, options, 0,    0)
table.attach(button[12],  2,  3,  1,  2, options, options, 0,    0)
table.attach(button[11],  3,  4,  1,  2, options, options, 0,    0)
table.attach(button[2],  4,  5,  1,  2, options, options, 0,    0)
table.attach(button[6],  5,  6,  1,  2, options, options, 0,    0)

table.attach(infolabel,  0,  3,  2,  3, options, options, 0,    0)
table.attach(button[13],  3,  5,  2,  3, options, options, 0,    0)
# table.attach(button[10], 3,  4,  2,  3, options, options, 0,    0)
# table.attach(button[8],  4,  5,  2,  3, options, options, 0,    0)
table.attach(button[9],  5,  6,  2,  3, options, options, 0,    0)

table.width_request = 900 

window.set_size_request(972, 548)
window.border_width = 0

imgfile = "bg_notfall-60.png"
imgfile = "bg_notfall-60.#{$lang}.png" if File.exists? "bg_notfall-60.#{$lang}.png"
imgfile = "/etc/lesslinux/branding/bg_notfall-60.#{$lang}.png" if File.exists? "/etc/lesslinux/branding/bg_notfall-60.#{$lang}.png"
bgimg = Gtk::Image.new(imgfile)

# buttons for shutdown and info
infobutton = Gtk::EventBox.new.add Gtk::Image.new("infobutton.png")
exitbutton = Gtk::EventBox.new.add Gtk::Image.new("exitbutton.png")
kbdbutton = Gtk::EventBox.new.add Gtk::Image.new("keyboard.png")

# inner fixed 
ifixed = Gtk::Fixed.new
ifixed.put(table, 0, 48)
ifixed.put(kbdbutton, 790, 0)
ifixed.put(infobutton, 830, 0)
ifixed.put(exitbutton, 865, 0)

fixed = Gtk::Fixed.new
fixed.put(bgimg, 0, 0)
fixed.put(ifixed, 35, 88)

#
# Now create some objects for each task - return windows as "named layers"
# 

layers = Hash.new
layers["start"] = ifixed

# Network screen
networkscreen = NetworkScreen.new(layers)
networkscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now the Samba share
sambascreen = SambaScreen.new(layers, button[6], networkscreen)
sambascreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now fix windows
winfixscreen = WindowsScreen.new(layers, button[0])
winfixscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now Photorec
photorecscreen = PhotorecScreen.new(layers, [ button[4], button[5] ] )
photorecscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now Photorec
# mobilerecscreen = MobilerecScreen.new(layers, button[5])
# mobilerecscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now Rescue
rescuescreen = RescueScreen.new(layers, button[7], networkscreen)
rescuescreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now virus screen
virusscreen = VirusScreen.new(layers, button[1], networkscreen)
virusscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Check screen (bad memory, hibernation, Intel IMSM)
checkscreen = CheckScreen.new(layers, @simulate)
checkscreen.layers.each { |k| fixed.put(k, 35, 136) }

# The simple info screen
infoscreen = InfoScreen.new(layers, infobutton, networkscreen, virusscreen.scanner, checkscreen.show)
infoscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Now chntpw screen
chntpwscreen = ChntpwScreen.new(layers, button[3] )
chntpwscreen.layers.each { |k| fixed.put(k, 35, 136) }

# Delete screen
deletescreen = DeleteScreen.new(layers, button[2] )
deletescreen.layers.each { |k| fixed.put(k, 35, 136) }

# Clone screen
clonescreen = CloneScreen.new(layers)
clonescreen.layers.each { |k| fixed.put(k, 35, 136) }

# Image screen
imagescreen = ImageScreen.new(layers)
imagescreen.layers.each { |k| fixed.put(k, 35, 136) }

# USB installation
usbscreen = UsbInstallScreen.new(layers, button[11])
usbscreen.layers.each { |k| fixed.put(k, 35, 136) }

# DVD rescue screen
dvdscreen = DvdRescueScreen.new(layers, button[12], photorecscreen)
dvdscreen.layers.each { |k| fixed.put(k, 35, 136) }

# PC Inspector 
inspectorscreen = PcInspectorScreen.new(layers, button[13], networkscreen)
inspectorscreen.layers.each { |k| fixed.put(k, 35, 136) }

# No separate layer for desktop mode
button[9].signal_connect('button-release-event') {
	system("touch /home/surfer/.ll_requested_desktop")
	system("chown 1000:1000 /home/surfer/.ll_requested_desktop")
	Gtk.main_quit 
}

# No separate layer for shutdown
exitbutton.signal_connect('button-release-event') {
	# system("touch /var/run/lesslinux/requested_shutdown")
	system("/usr/bin/shutdown-wrapper.sh") 
}
button[8].signal_connect('button-release-event') {
	# system("touch /var/run/lesslinux/requested_shutdown")
	system("/usr/bin/shutdown-wrapper.sh") 
}

kbdbutton.signal_connect('button-release-event') {
	system('nohup xvkbd &') 
}

layers.keys.sort.each { |n|
	puts "Layer: " + n 
}

#
# Show everything
#

window.add fixed
window.show_all 
layers.each { |k,v| v.hide_all }

@start = "start" if @start.nil?

if File.exist?("/var/run/lesslinux/license_accepted") &&  checkscreen.show == true
	layers["checkscreen"].show_all 
elsif File.exist?("/var/run/lesslinux/license_accepted") || @forcescreen == true
	layers[@start].show_all 
else
	layers["legal"].show_all 
end

# networkscreen.network_dialog

Gtk.main
