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

require 'MfsSysProfile'

class PcInspectorScreen	
	def initialize(extlayers, button, nwscreen)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@lang = lang
		@tl = MfsTranslator.new(lang, "PcInspectorScreen.xml")
		@extlayers = extlayers
		@layers = Array.new
		@urllabel = Gtk::Label.new
		## table for first SMART read out
		@smarttab = nil
		@smart_quick_icons = Array.new
		@smart_quick_descriptions = Array.new
		## progressbar for SMART self test routine
		@smart_progress = nil
		@break_smart_test = nil
		## table for second SMART readout
		@smartsumtab = nil
		@smart_detail_icons = Array.new
		@smart_detail_descriptions = Array.new
		## Button and description to jump to save function
		@save_button = nil
		@save_text = nil
		## Button and progress bar for CPU burn
		@cpuburnprogress = Gtk::ProgressBar.new
		@killcpuburn = Gtk::Button.new(@tl.get_translation("cancelcpuburn"))
		@killcpuburn.sensitive = false
		@cooling = false
		@cpuburn_next = "winfixsmart"
		
		button1 = Gtk::EventBox.new.add Gtk::Image.new("systest1.png")
		button2 = Gtk::EventBox.new.add Gtk::Image.new("systest2.png")
		button3 = Gtk::EventBox.new.add Gtk::Image.new("systest3.png")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		
		text1 = Gtk::Label.new
		text1.width_request = 380
		text1.wrap = true
		text1.set_markup("<span foreground='white'>" + @tl.get_translation("inspect") + "</span>")
		
		text2 = Gtk::Label.new
		text2.width_request = 380
		text2.wrap = true
		text2.set_markup("<span foreground='white'>" + @tl.get_translation("sysprofile") + "</span>")
		
		text3 = Gtk::Label.new
		text3.width_request = 380
		text3.wrap = true
		text3.set_markup("<span foreground='white'>" + @tl.get_translation("monitortest") + "</span>")
		
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("cancel") + "</span>")
		
		fixed = Gtk::Fixed.new
		fixed.put(button1, 0, 0)
		fixed.put(button2, 0, 130)
		fixed.put(button3, 0, 260)
		fixed.put(back, 852, 332)
		
		fixed.put(text1, 130, 20)
		fixed.put(text2, 130, 150)
		fixed.put(text3, 130, 280)
		fixed.put(text4, 605, 338)
		
		@layers[0] = fixed
		extlayers["pcinspector"] = fixed 
		
		button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["pcinspector"].show_all
			end
		}
		button1.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["pcloadtest"].show_all
			end
		}
		button2.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				if File.exists?("/var/run/lesslinux/sysprofile.url")
					url = File.new("/var/run/lesslinux/sysprofile.url").read.strip
					@urllabel.set_markup("<span color='white'>" + @tl.get_translation("sysprofiledonebody").gsub("SYSPROFILEURL", url.strip) + "</span>")
					system("nohup su surfer -c \"firefox '#{url}'\" &")
					extlayers["pcsysprofiledone"].show_all
				elsif nwscreen.test_connection == false
					nwscreen.nextscreen = "pcsysprofile"
					nwscreen.fill_wlan_combo 
					# Check for networks first...
					extlayers["networks"].show_all
				else
					extlayers["pcsysprofile"].show_all
				end
			end
		}
		button3.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				system("ruby -I /usr/share/lesslinux/notfallcd4 /usr/share/lesslinux/notfallcd4/monitortest.rb &") 
			end
		}
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["start"].show_all
			end
		}
		create_sysprofile_layer
		create_sysprofile_done
		create_test_layer
		create_result_layer
		return fixed
	end
	attr_reader :layers
	
	def create_sysprofile_layer
		fixed = Gtk::Fixed.new
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		tile = Gtk::EventBox.new.add Gtk::Image.new("systestneutral.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("sysprofilehead") + "</b>\n\n" + @tl.get_translation("sysprofilebody"), 510)
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("cancel") + "</span>")
		showprivacy = Gtk::Button.new @tl.get_translation("showprivacy")
		showprivacy.width_request = 150
		showprivacy.height_request = 32
		mailentry = Gtk::Entry.new
		mailentry.width_request= 300
		maillabel = TileHelpers.create_label(@tl.get_translation("email"), 80)
		newslabel = TileHelpers.create_label(@tl.get_translation("newsletter"), 240)
		shoplabel = TileHelpers.create_label(@tl.get_translation("shopping"), 240)
		shopcheck = Gtk::CheckButton.new("")
		newscheck = Gtk::CheckButton.new("")
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		fixed.put(tile, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(mailentry, 210, 180)
		fixed.put(maillabel, 130, 180)
		fixed.put(newslabel, 150, 100)
		fixed.put(newscheck, 130, 100)
		fixed.put(shoplabel, 150, 140)
		fixed.put(shopcheck, 130, 140)
		fixed.put(showprivacy, 520, 180)
		@layers[1] = fixed
		@extlayers["pcsysprofile"] = fixed
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@extlayers.each { |k,v|v.hide_all }
				@extlayers["start"].show_all
			end
		}
		showprivacy.signal_connect('button-release-event') { |x, y|
			@extlayers.each { |k,v|v.hide_all }
			@extlayers["legal2"].show_all
		}
		text5 = Gtk::Label.new
		text5.width_request = 230
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("createsysprofile") + "</span>")
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		fixed.put(forw, 853, 282)
		fixed.put(text5, 605, 288)
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@extlayers.each { |k,v|v.hide_all }
				prof = MfsSysProfile.new
				n = 0
				n = 1 if newscheck.active? == true
				h = 0
				h = 1 if shopcheck.active? == true
				url = prof.send('http://sysprofile.lesslinux.com/submit.php', mailentry.text.strip, n, h) 
				$stderr.puts url
				u = File.new("/var/run/lesslinux/sysprofile.url", "w")
				u.write url
				u.close 
				system("nohup su surfer -c \"firefox '#{url}'\" &")
				@urllabel.set_markup("<span color='white'>" + @tl.get_translation("sysprofiledonebody").gsub("SYSPROFILEURL", url.strip) + "</span>")
				@extlayers["pcsysprofiledone"].show_all
			end
		}
	end
	
	def create_sysprofile_done
		fixed = Gtk::Fixed.new
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		tile = Gtk::EventBox.new.add Gtk::Image.new("systestneutral.png")
		# deltext = TileHelpers.create_label("<b>" + @tl.get_translation("sysprofiledonehead") + "</b>\n\n" + @tl.get_translation("sysprofiledonebody"), 510)
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("cancel") + "</span>")
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		@urllabel.width_request = 510
		@urllabel.wrap = true
		fixed.put(@urllabel, 130, 0)
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@extlayers.each { |k,v|v.hide_all }
				@extlayers["start"].show_all
			end
		}
		@layers[4] = fixed
		@extlayers["pcsysprofiledone"] = fixed
	end
	
	def create_test_layer
		fixed = Gtk::Fixed.new
		tile = Gtk::EventBox.new.add Gtk::Image.new("systestgreen.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("systesthead") + "</b>\n\n" + @tl.get_translation("systestbody"), 510)
		systestprogress = Gtk::ProgressBar.new
		systestprogress.width_request = 380
		systestprogress.height_request = 32
		killsystest = Gtk::Button.new @tl.get_translation("killsystest")
		killsystest.width_request = 120
		killsystest.height_request = 32
		fixed.put(tile, 0, 0)
		fixed.put(deltext, 130, 0)
		fixed.put(systestprogress, 130, 108)
		fixed.put(killsystest, 515, 108)
		killsystest.signal_connect("clicked") {
			@extlayers.each { |k,v|v.hide_all }
			@extlayers["pcsystestres"].show_all
		}
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("cancel") + "</span>")
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		@layers[2] = fixed
		@extlayers["pcloadtest"] = fixed
		# FIXME: 
		@cpuburnprogress.text = @tl.get_translation("systesthead")
		@cpuburnprogress.pulse
		
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@extlayers.each { |k,v|v.hide_all }
				@extlayers["start"].show_all
			end
		}
	end
	
	def create_result_layer
		fixed = Gtk::Fixed.new
		tile = Gtk::EventBox.new.add Gtk::Image.new("systestneutral.png")
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("sysreshead") + "</b>\n\n" + @tl.get_translation("sysresbody"), 510)
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("normalback") + "</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		fixed.put(tile, 0, 0)
		fixed.put(deltext, 130, 0)
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				@extlayers.each { |k,v|v.hide_all }
				@extlayers["start"].show_all
			end
		}
		@layers[3] = fixed
		@extlayers["pcsystestres"] = fixed
	end
	
end
