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

class RescueScreen	
	def initialize(extlayers, button, nwscreen)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@tl = MfsTranslator.new(lang, "RescueScreen.xml")
		### Combo for writeable drives when file managers are selected
		@writeablecombo = nil
		@writeablelist = Array.new
		@writeableparts = Array.new
		# Try to omit opening of firefox when the cloud layer is shown the first time
		@cloudshowcount = 0
		### First panel, selection of tasks
		# First tile, rescue on file system level
		button1 = Gtk::EventBox.new.add Gtk::Image.new("rescuewine.png")
		text1 = Gtk::Label.new
		text1.width_request = 380
		text1.wrap = true
		text1.set_markup("<span foreground='white'><b>" + @tl.get_translation("localhead") + "</b>\n\n" + @tl.get_translation("localbody") + "</span>")
	
		# Second tile, rescue to cloud
		button2 = Gtk::EventBox.new.add Gtk::Image.new("rescueturq.png")
		text2 = Gtk::Label.new
		text2.width_request = 380
		text2.wrap = true
		text2.set_markup("<span foreground='white'><b>" + @tl.get_translation("cloudhead") + "</b>\n\n"+ @tl.get_translation("cloudbody") + "</span>")
		
		# Third tile, image 1:1
		button3 = Gtk::EventBox.new.add Gtk::Image.new("rescueorange.png")
		text3 = Gtk::Label.new
		text3.width_request = 380
		text3.wrap = true
		# text3.set_markup("<span foreground='white'><b>Klon Ihrer Festplatte</b>\n\nWählen Sie diese Option bei drohenden Festplattendefekten. Sämtliche Partitionen, Dateien und Programme auf dem Laufwerk werden eins zu eins auf eine externe Festplatte überspielt. Achtung: Alle auf dem Ziel-Laufwerk gespeicherte Daten gehen dadurch verloren!</span>.")
		text3.set_markup("<span foreground='white'><b>" + @tl.get_translation("imagehead") + "</b>\n\n" + @tl.get_translation("imagebody")  + "</span>")
		@layers = Array.new
		fixed = Gtk::Fixed.new
		fixed.put(button1, 0, 0)
		fixed.put(button2, 0, 130)
		fixed.put(button3, 0, 260)
		fixed.put(text1, 130, 0)
		fixed.put(text2, 130, 130)
		fixed.put(text3, 130, 260)
		
		TileHelpers.place_back(fixed, extlayers)
		extlayers["rescuestart"] = fixed
		@layers[0] = fixed
		
		button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["rescuestart"].show_all
			end
		}
		button1.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@writeablelist, @writeableparts = reread_targetlist(@writeablecombo)
				extlayers.each { |k,v|v.hide_all }
				extlayers["rescuelocal"].show_all
			end
		}
		button2.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				if nwscreen.test_connection == false
					nwscreen.nextscreen = "rescuecloud"
					nwscreen.fill_wlan_combo 
					# Check for networks first...
					extlayers["networks"].show_all
				else
					extlayers["rescuecloud"].show_all
				end
				
			end
		}
		button3.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				# extlayers["clonesource"].show_all
				extlayers["imagechoice"].show_all
			end
		}
		
		#### Screen for filetypes
		locallayers = get_local_layer(extlayers)
		extlayers["rescuelocal"] = locallayers
		@layers[1]  = locallayers

		#### Screen for file manager
		fmlayers = get_fm_layer(extlayers)
		extlayers["rescuefilemanager"] = fmlayers
		@layers[2]  = fmlayers
		
		#### Screen for cloud
		cloudlayer = get_cloud_layer(extlayers)
		extlayers["rescuecloud"] = cloudlayer
		@layers[3] = cloudlayer

	end
	attr_reader :layers
	
	def get_local_layer(extlayers)
		fixed = Gtk::Fixed.new
		TileHelpers.place_back(fixed, extlayers)
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("targethead") + "</b>\n\n" +@tl.get_translation("targetbody") , 510)
		@writeablecombo = Gtk::ComboBox.new
		@writeablecombo.height_request = 32
		@writeablecombo.width_request = 380
		reread = Gtk::Button.new(@tl.get_translation("reread"))
		reread.width_request = 120
		reread.height_request = 32
		reread.signal_connect('clicked') {
			@writeablelist, @writeableparts = reread_targetlist(@writeablecombo)
		}
		text5 = Gtk::Label.new
		text5.width_request = 250
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("gotofilemanager") + "</span>")
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		button1 = Gtk::Image.new("rescuegreen.png")
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				if @writeableparts.size < 1
					TileHelpers.error_dialog(@tl.get_translation("nosuitabledrives"), @tl.get_translation("nosuitablehead"))
				else
					extlayers.each { |k,v|v.hide_all }
					mount_all( @writeableparts[@writeablecombo.active] )
					extlayers["rescuefilemanager"].show_all
					system("su surfer -c 'Thunar /cobi/" + @tl.get_translation("backupdir") + "' &") 
					system("su surfer -c 'Thunar /media' &") 
				end
			end
		}
		fixed.put(button1, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(reread, 520, 118)
		fixed.put(@writeablecombo, 130, 118)
		fixed.put(forw, 853, 282)
		fixed.put(text5, 605, 288)
		return fixed
	end

	# Layer for filemanager
	def get_fm_layer(extlayers)
		fixed = Gtk::Fixed.new
		backbutton = TileHelpers.place_back(fixed, extlayers, false)
		button1 = Gtk::Image.new("rescuegreen.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("copyhead") + "</b>\n\n" + @tl.get_translation("copybody") , 510)
		thbutton = Gtk::Button.new(@tl.get_translation("openfilemanager"))
		thbutton.width_request = 150
		thbutton.height_request = 32
		thbutton.signal_connect('clicked') {
			system("su surfer -c 'Thunar /media' &") 
		}
		backbutton.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				system("killall Thunar")
				system("killall -9 Thunar")
				TileHelpers.umount_all
				extlayers["start"].show_all
			end
		}
		fixed.put(button1, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(thbutton, 490, 128)
		return fixed
	end

	def get_cloud_layer(extlayers)
		fixed = Gtk::Fixed.new
		backbutton = TileHelpers.place_back(fixed, extlayers, false)
		button1 = Gtk::Image.new("rescuegreen.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("cloudhead") + "</b>\n\n" + @tl.get_translation("cloudbody"), 510)
		ffbutton = Gtk::Button.new(@tl.get_translation("openfirefox"))
		ffbutton.width_request = 150
		ffbutton.height_request = 32
		ffbutton.signal_connect('clicked') {
			system("su surfer -c 'firefox &'") # http://www.computerbild-cloud.de/' &") 
		}
		fixed.signal_connect('show') {
			if @cloudshowcount > 0
				mount_all
				system("su surfer -c 'firefox &'") #  http://www.computerbild-cloud.de/' &") 
			end
			@cloudshowcount += 1
		}
		backbutton.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				system("killall -15 firefox")
				sleep 2
				system("killall -9 firefox")
				TileHelpers.umount_all
				extlayers["start"].show_all
			end
		}
		fixed.put(button1, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(ffbutton, 490, 108)
		return fixed
	end
	
	def get_clonestart_layer(extlayers)
		
	end
	
	def reread_targetlist(combobox)
		100.downto(0) { |i| 
			begin
				combobox.remove_text(i) 
			rescue
			end
		}
		TileHelpers.umount_all
		drives = Array.new
		partitions = Array.new
		Dir.entries("/sys/block").each { |l|
			drives.push(MfsDiskDrive.new(l, false)) if l =~ /[a-z]$/ 
		}
		drives.each { |d|
			d.partitions.each  { |p|
				label = "#{d.vendor} #{d.model}"
				if d.usb == true
					label = label + " (USB) "
				else
					label = label + " (SATA/eSATA/IDE) "
				end
				label = label + "#{p.device} #{p.human_size}"
				pmnt = p.mount_point
				if ( p.fs =~ /vfat/ || p.fs =~ /ntfs/ || p.fs =~ /ext/ || p.fs =~ /btrfs/ ) && pmnt.nil?
					label = label + " #{p.fs}"
					partitions.push p.device
					combobox.append_text(label)
				end
			}
		}
		combobox.active = 0
		return drives, partitions
	end
	
	def mount_all(writeable_part=nil)
		begin
			bookmarks = File.new("/home/surfer/.gtk-bookmarks", "w")
		rescue
			bookmarks = File.new("/tmp/.gtk-bookmarks", "w")
		end
		drives = Array.new
		Dir.entries("/sys/block").each { |l|
			drives.push(MfsDiskDrive.new(l, false)) if l =~ /[a-z]$/ 
		}
		drives.each { |d|
			d.partitions.each { |p|
				if p.device == writeable_part
					system("mkdir -p /cobi/" + @tl.get_translation("backupdir")) 
					p.mount("rw", "/cobi/" + @tl.get_translation("backupdir"), 1000, 1000)
					bookmarks.write("file:///cobi/" + @tl.get_translation("backupdir") + "\n")
				else
					system("mkdir -p /media/#{p.device}")
					p.mount("ro", "/media/#{p.device}", 1000, 1000)
					bookmarks.write("file:///media/#{p.device}\n")
				end
			}
		}
		bookmarks.close
		system("chown 1000:1000 /home/surfer/.gtk-bookmarks")
	end
	
end