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

class WordRescueScreen	
	def initialize(extlayers)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@lang = lang
		@tl = MfsTranslator.new(lang, "WordRescueScreen.xml")
		@extlayers = extlayers
		@layers = Array.new
		# exported button:
		@button = Gtk::EventBox.new.add Gtk::Image.new("reparierenwine.png")
		@selectedfiles = Array.new
		@suitableparts = Array.new
		
		# buttons for selection of target and source
		@fmbutton = nil
		@targetcombo = nil
		@progress = nil
		
		@button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				fill_target_combo
				@progress.fraction = 0.0 
				@fmbutton.label = @tl.get_translation("selectfile")
				extlayers["wordrescuestart"].show_all
			end
		}
		
		firstlayer = create_first_layer(extlayers)
		extlayers["wordrescuestart"] = firstlayer
		@layers.push firstlayer
		proglayer = create_progress_layer(extlayers)
		extlayers["wordrescueprogress"] = proglayer
		@layers.push proglayer
		
	end
	attr_reader :layers, :button 
	
	def create_first_layer(extlayers)
		fixed = Gtk::Fixed.new
		tile = Gtk::EventBox.new.add Gtk::Image.new("reparierenneutral.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("wordreschead") + "</b>\n\n" + @tl.get_translation("wordrescbody"), 510)
		fixed.put(tile, 0, 0)
		fixed.put(deltext, 130, 0)
		back = TileHelpers.place_back(fixed, extlayers) #, false)
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		forwtext = Gtk::Label.new
		forwtext.width_request = 250
		forwtext.wrap = true
		targettext = TileHelpers.create_label(@tl.get_translation("selecttarget"), 250)
		forwtext.set_markup("<span color='white'>" + @tl.get_translation("gotonext") + "</span>")
		fixed.put(forw, 853, 282)
		fixed.put(forwtext, 605, 288)
		@fmbutton = Gtk::Button.new(@tl.get_translation("selectfile"))
		@fmbutton.width_request = 510
		@fmbutton.height_request = 32
		@targetcombo = Gtk::ComboBox.new
		@targetcombo.width_request = 510
		@targetcombo.height_request = 32
		fixed.put(@fmbutton, 130, 150)
		fixed.put(targettext, 130, 190)
		fixed.put(@targetcombo, 130, 210)
		@fmbutton.signal_connect("clicked") {
			dialog = Gtk::FileChooserDialog.new(@tl.get_translation("selectfile"),
			$mainwindow,
			Gtk::FileChooser::ACTION_OPEN,
			nil,
			[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
			[Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
			dialog.select_multiple = true 
			if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
				# @startdir = dialog.filename
				@fmbutton.label = dialog.filenames.to_s  
				@selectedfiles = dialog.filenames
				puts "filename = #{dialog.filenames.to_s}"
			end
			dialog.destroy
		}
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				if @selectedfiles.size > 0 && @suitableparts.size > 0
					extlayers.each { |k,v|v.hide_all }
					extlayers["wordrescueprogress"].show_all
					run_extract(extlayers)
				elsif  @selectedfiles.size < 1
					TileHelpers.error_dialog(@tl.get_translation("selectatleastonefile"))
				else
					TileHelpers.error_dialog(@tl.get_translation("nosuitabletargetfound"))
				end
			end
		}
		return fixed
	end
	
	def create_progress_layer(extlayers)
		fixed = Gtk::Fixed.new
		tile = Gtk::EventBox.new.add Gtk::Image.new("reparierenneutral.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("wordrescproghead") + "</b>\n\n" + @tl.get_translation("wordrescprogbody"), 510)
		back = TileHelpers.place_back(fixed, extlayers, false)
		@progress = Gtk::ProgressBar.new
		@progress.width_request = 510
		@progress.height_request = 32
		fixed.put(tile, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(@progress, 130, 150)
		return fixed
	end
	
	def fill_target_combo
		drives = Array.new
		@suitableparts = Array.new
		# @winparts = Array.new
		# shellentry = Array.new
		# isdefault = Array.new
		# @regfiles = Hash.new
		# activeitem = 0
		199.downto(0) { |n|
			begin
				@targetcombo.remove_text(n)
			rescue
			end
		}
		Dir.entries("/sys/block").each { |l|
			if l =~ /[a-z]$/ ||  l =~ /mmcblk[0-9]$/ ||  l =~ /mmcblk[0-9][0-9]$/
				begin
					d =  MfsDiskDrive.new(l, true)
					drives.push(d) 
				rescue 
					$stderr.puts "Failed adding: #{l}"
				end
			end
		}
		# Now fill each combo box with the respective 
		drives.each { |d|
			d.partitions.each { |p|
				# iswin, winvers = p.is_windows
				if p.fs =~ /FAT/i || p.fs =~ /NTFS/i  
					@suitableparts.push(p)
					@targetcombo.append_text("#{p.device}, #{p.human_size} - #{p.fs}")
					# auxreg = p.regfile
					# @regfiles[p] = auxreg 
					# shellentry.push  auxreg.get_shell
					# isdefault.push auxreg.shell_is_explorer?
				end
			}
		}
		if @suitableparts.size > 0 
			# gobutton.sensitive = true 
			@targetcombo.sensitive = true
			# switch_shelllabel(shelllabel, 0, gobutton)
		else
			@targetcombo.append_text(@tl.get_translation("no_suitable_partition_found"))
			@targetcombo.sensitive = false
			#wincombo.append_text("Windows 8.1 auf sdb1 (SATA/eSATA/IDE)") 
			# wincombo.sensitive = true
		end
		@targetcombo.active = 0
	end
	
	def run_extract(extlayers)
		filect = 0
		@selectedfiles.each { |f|
			@progress.fraction = filect.to_f / @selectedfiles.size.to_f
			@progress.text = @tl.get_translation("rescuing_file") + " " + f 
			filect += 1
			while (Gtk.events_pending?)
				Gtk.main_iteration
			end
			sleep 5
		}
		@progress.fraction = 1.0
		@progress.text = @tl.get_translation("rescuing_done")
		TileHelpers.success_dialog(@tl.get_translation("everything_done"))
		extlayers.each { |k,v|v.hide_all }
		extlayers["start"].show_all
	end
	
	
	# def create_
	
end
