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

class InfoScreen
	def initialize(extlayers, button, nwscreen, scanner=nil, checkscreen=false)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@lang = lang
		@tl = MfsTranslator.new(lang, "InfoScreen.xml")
		@nwscreen = nwscreen
		@layers = Array.new
		@checkscreen = checkscreen
		fixed = Gtk::Fixed.new
		
		# Kachel Nutzungsbedingungen
		icon_legal = Gtk::EventBox.new.add  Gtk::Image.new("legal.png")
		text_legal = TileHelpers.create_label("<b>" + @tl.get_translation("lichead") + "</b>\n\n" +@tl.get_translation("licbody") , 250)
		icon_legal.signal_connect("button-release-event") { |x,y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["legal2"].show_all
			end
		}
		
		# Kachel Updates
		icon_update = Gtk::EventBox.new.add  Gtk::Image.new("updater.png")
		text_update = TileHelpers.create_label("<b>" + @tl.get_translation("updhead") + "</b>\n\n" +@tl.get_translation("updbody") , 250)
		icon_update.signal_connect("button-release-event") { |x,y|
			if nwscreen.test_connection == false
				nwscreen.nextscreen = "infobox"
				nwscreen.fill_wlan_combo 
				extlayers.each { |k,v|v.hide_all }
				extlayers["networks"].show_all
			end
			system("/etc/lesslinux/updater/update_wrapper.sh &")
		}
		# Kachel Mattias Schlenker
		icon_mattias = Gtk::EventBox.new.add  Gtk::Image.new("lesslinux.png")
		text_mattias = TileHelpers.create_label("<b>" + @tl.get_translation("techhead") + "</b>\n\n" +@tl.get_translation("techbody") , 380)
		icon_mattias.signal_connect("button-release-event") { |x,y|
			if y.button == 1 
				TileHelpers.open_browser("http://www.mattiasschlenker.de/") 
			end
		}
		
		# Kachel Jan Sass
		icon_jansass = Gtk::EventBox.new.add  Gtk::Image.new("jansass.png")
		text_jansass = TileHelpers.create_label("<b>" + @tl.get_translation("graphhead") + "</b>\n\n" +@tl.get_translation("graphbody") , 380)
		icon_jansass.signal_connect("button-release-event") { |x,y|
			if y.button == 1 
				TileHelpers.open_browser("http://www.jansass.com/") 
			end
		}

		# Kachel Avira
		scanicon = "avira.png"
		scanicon = "clamav.png" unless scanner == "scancl"
		scantext = "<b>" + @tl.get_translation("avirahead") + "</b>\n\n" + @tl.get_translation("avirabody")
		scantext = "<b>" + @tl.get_translation("clamavhead") + "</b>\n\n" + @tl.get_translation("clamavbody") unless scanner == "scancl"
		icon_avira = Gtk::EventBox.new.add  Gtk::Image.new(scanicon)
		text_avira = TileHelpers.create_label(scantext,  250) 
		icon_avira.signal_connect("button-release-event") { |x,y|
			# if y.button == 3
			#	TileHelpers.open_browser("http://www.avira.com/") 
			# end
		}
		
		# Kachel Jan Mattias
		icon_jsms = Gtk::EventBox.new.add  Gtk::Image.new("janmattias.png")
		text_jsms = TileHelpers.create_label("<b>" + @tl.get_translation("exthead") + "</b>\n\n" + @tl.get_translation("extbody"),  250) 
		icon_jsms.signal_connect("button-release-event") { |x,y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["infojanmattias"].show_all
			end
		}
		
		
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" +@tl.get_translation("cancel") + "</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		fixed.put(icon_legal, 0, 0)
		fixed.put(text_legal, 130, 0)
		fixed.put(icon_update, 380, 0)
		fixed.put(text_update, 510, 0)
		# fixed.put(icon_mattias, 380, 0)
		# fixed.put(text_mattias, 510, 0)
		# fixed.put(icon_jansass, 0, 0)
		# fixed.put(text_jansass, 130, 0)
		fixed.put(icon_avira, 0, 130)
		fixed.put(text_avira, 130, 130)
		fixed.put(icon_jsms, 380, 130)
		fixed.put(text_jsms, 510, 130)
		
		
		extlayers["infobox"] = fixed
		@layers[0] = fixed
		
		legallayer = create_legal_screen(extlayers)
		extlayers["legal"] = legallayer
		@layers[1] = legallayer
		
		otherlegallayer = create_other_legal_screen(extlayers)
		extlayers["legal2"] = otherlegallayer
		@layers[2] = otherlegallayer
		
		jsmslayer = create_jsms_screen(extlayers)
		extlayers["infojanmattias"] = jsmslayer
		@layers[3] = jsmslayer
		
		button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["infobox"].show_all
			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
		}
	
	end
	
	def create_jsms_screen(extlayers)
		fixed = Gtk::Fixed.new
		TileHelpers.place_back(fixed, extlayers)
		# Kachel Mattias Schlenker
		icon_mattias = Gtk::EventBox.new.add  Gtk::Image.new("lesslinux.png")
		text_mattias = TileHelpers.create_label("<b>" + @tl.get_translation("techhead") + "</b>\n\n" +@tl.get_translation("techlong") , 380)
		icon_mattias.signal_connect("button-release-event") { |x,y|
			if y.button > 0 
				TileHelpers.open_browser("http://www.mattiasschlenker.de/") 
				sleep 2
				TileHelpers.open_browser("http://blog.lesslinux.org/")
			end
		}
		
		# Kachel Jan Sass
		icon_jansass = Gtk::EventBox.new.add  Gtk::Image.new("jansass.png")
		text_jansass = TileHelpers.create_label("<b>" + @tl.get_translation("graphhead") + "</b>\n\n" +@tl.get_translation("graphlong") , 380)
		icon_jansass.signal_connect("button-release-event") { |x,y|
			if y.button > 0
				TileHelpers.open_browser("http://www.jansass.com/") 
			end
		}
		
		fixed.put(icon_mattias, 0, 0)
		fixed.put(text_mattias, 130, 0)
		fixed.put(icon_jansass, 0, 130)
		fixed.put(text_jansass, 130, 130)
		
		
		return fixed
	end
		
		
	
	def create_legal_screen(extlayers)
		fixed = Gtk::Fixed.new
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("licdecline") + "</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		text5 = Gtk::Label.new
		text5.width_request = 250
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("licaccept") + "</span>")
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		
		disctext = "Ooops nicht gefunden!"
		if File.exist?("legal.#{@lang}.txt")
			disctext = File.new("legal.#{@lang}.txt").read
		elsif File.exist?("haftung.txt")
			disctext = File.new("haftung.txt").read
		end
		disclabel = Gtk::Label.new
		disclabel.set_markup("<span color='white'>" + @tl.get_translation("licheader") + "</span>")
		fat_font = Pango::FontDescription.new("Sans Bold 14")
		disclabel.modify_font(fat_font)
		textview = Gtk::TextView.new
		textview.buffer.text = disctext
		textview.set_wrap_mode(Gtk::TextTag::WRAP_WORD)
		scrolled_win = Gtk::ScrolledWindow.new
		scrolled_win.border_width = 5
		scrolled_win.add(textview)
		scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS)
		textview.modify_bg(Gtk::STATE_NORMAL, Gdk::Color.parse('#007dd2') )
		
		# scrolled_win.add(textview)
		scrolled_win.set_size_request(580, 330)
		
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				system("touch /var/run/lesslinux/license_accepted") 
				extlayers.each { |k,v|v.hide_all }
				if @checkscreen == false
					extlayers["start"].show_all 
				else
					extlayers["checkscreen"].show_all 
				end
			end
		}
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				system("/usr/bin/shutdown-wrapper.sh") 
			end
		}
		fixed.put(disclabel, 0, 3)
		fixed.put(scrolled_win, 0, 40)
		fixed.put(forw, 853, 282)
		fixed.put(text5, 605, 288)
		fixed.put(back, 852, 332)
		fixed.put(text4, 605, 338)
		return fixed 
	end
	
	def create_other_legal_screen(extlayers)
		fixed = Gtk::Fixed.new
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("licdecline") + "</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		text5 = Gtk::Label.new
		text5.width_request = 250
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("licaccept") + "</span>")
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		
		disctext = "Ooops nicht gefunden!"
		if File.exist?("legal.#{@lang}.txt")
			disctext = File.new("legal.#{@lang}.txt").read
		elsif File.exist?("haftung.txt")
			disctext = File.new("haftung.txt").read
		end
		
		disclabel = Gtk::Label.new
		disclabel.set_markup("<span color='white'>" + @tl.get_translation("licheader") + "</span>")
		fat_font = Pango::FontDescription.new("Sans Bold 14")
		disclabel.modify_font(fat_font)
		textview = Gtk::TextView.new
		textview.buffer.text = disctext
		textview.set_wrap_mode(Gtk::TextTag::WRAP_WORD)
		scrolled_win = Gtk::ScrolledWindow.new
		scrolled_win.border_width = 5
		scrolled_win.add(textview)
		scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS)
		textview.modify_bg(Gtk::STATE_NORMAL, Gdk::Color.parse('#007dd2') )
		
		# scrolled_win.add(textview)
		scrolled_win.set_size_request(580, 330)
		
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				system("touch /var/run/lesslinux/license_accepted") 
				extlayers.each { |k,v|v.hide_all }
				extlayers["start"].show_all
			end
		}
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				system("/usr/bin/shutdown-wrapper.sh") 
			end
		}
		fixed.put(disclabel, 0, 3)
		fixed.put(scrolled_win, 0, 40)
		#fixed.put(forw, 853, 282)
		#fixed.put(text5, 605, 288)
		#fixed.put(back, 852, 332)
		#fixed.put(text4, 605, 338)
		TileHelpers.place_back(fixed, extlayers)
		return fixed 
	end	
	
	
	attr_reader :layers

end