# copyright= 'Huynh Duong Phuong Vi @ 2014-2015'
# suforyou.vn@gmail.com
module S4U::S4u_material
		Sketchup::require 'sketchup.rb' 
		Sketchup::require 'langhandler.rb'
		if not file_loaded?(__FILE__)
			S4U.instance_variable_set(:@s4u_menu,UI.menu("Plugins").add_submenu("Suforyou")) if !S4U.instance_variable_defined?(:@s4u_menu)
			menu=S4U.instance_variable_get :@s4u_menu 
			submenu=menu.add_submenu(Strings.GetString("Material")) 
			toolbar = UI::Toolbar.new("s4u-"+Strings.GetString(TOOL))             
			names=[ [Strings.GetString("Back to front"),"material1.png","material11.png"],
					[Strings.GetString("Back to front and reverse"),"material2.png","material21.png"],
					[Strings.GetString("Exchange front,back material"),"material3.png","material31.png"],
					[Strings.GetString("Exchange material and reverse"),"material4.png","material41.png"],
					[Strings.GetString("Reverse front face to view"),"material5.png","material51.png"],
					[Strings.GetString("Reverse front face to view and Back to Front"),"material6.png","material61.png"]
			]
			(0..names.length-1).each{|i|
				cmd=UI::Command.new(names[i][0]){ S4U::S4u_material.main(i) }
				cmd.large_icon = names[i][1] 
				cmd.small_icon = names[i][2]
				cmd.status_bar_text = names[i][0] 
				cmd.tooltip= names[i][0] 
				cmd.menu_text = names[i][0]
				toolbar=toolbar.add_item cmd
				submenu.add_item cmd
			}
			toolbar.show if toolbar.get_last_state.abs == 1 
		end
		def self.main(mode=0)
			model=Sketchup.active_model            
			@faces=[]            
			sel=model.selection            
			if sel.length==0       
				status=UI.messagebox(Strings.GetString("Do you want to process All Active Entities?"),MB_YESNO)
				return if status!=6
				model.active_entities.each{|s| self.select_face(s) if s.is_a?Sketchup::ComponentInstance or s.is_a?Sketchup::Group}
				model.active_entities.each{|f| 
					if f.is_a? Sketchup::Face
						vers=f.vertices.collect{|v| v.position}
						@faces<< [f,vers,f.normal]                                       
					end
				}
			else
				sel.each{|s| self.select_face(s) if s.is_a?Sketchup::ComponentInstance or s.is_a?Sketchup::Group}                                         
				sel.each{|f| 
					if f.is_a? Sketchup::Face
						vers=f.vertices.collect{|v| v.position}
						@faces<< [f,vers,f.normal]                                       
					end
				}
			end   
			if mode==0
				model.start_operation Strings.GetString("Back to front material"),true,false,false 
				status=nil
				@faces.each{|f| 
					if f[0].material && (!status)
						status=UI.messagebox(Strings.GetString("Do you want to Overwrite front material?"),MB_YESNO)
					end
					f[0].material=f[0].back_material if (f[0].back_material && status==6 && f[0].material) || ((!f[0].material) && f[0].back_material)
				} 
				model.commit_operation
			elsif mode==1
				model.start_operation Strings.GetString("Back to front and reverse"),true,false,false 
				@faces.each{|f| 
					f[0].reverse!
					f[0].material=f[0].back_material if f[0].back_material} 
				model.commit_operation
			elsif mode==2
				model.start_operation Strings.GetString("Exchange material"),true,false,false 
				@faces.each{|f| 
					front=f[0].material
					f[0].material=f[0].back_material
					f[0].back_material=front
				} 
				model.commit_operation
			elsif mode==3
				model.start_operation Strings.GetString("Exchange and reverse"),true,false,false 
				@faces.each{|f| 
					front=f[0].material
					f[0].material=f[0].back_material 
					f[0].back_material=front
					f[0].reverse!
				} 
				model.commit_operation
			elsif mode==4
				model.start_operation Strings.GetString("Reverse front face to view"),true,false,false 
				view=model.active_view                
				@faces.each{|f| 
					flag=true;ray=nil
					f[1].each{|v|                        
						pv=view.screen_coords(v)
						ray = view.pickray pv.x,pv.y
						ray=[v,ray[1].reverse]
						flag=false if model.raytest(ray,true)
					}	                                   
					normal=f[2]
					ang=normal.angle_between ray[1]     
					f[0].reverse! if flag && ((ang> Math::PI*0.5) || (normal.samedirection?(ray[1].reverse)))
				} 
				model.commit_operation
			else
				model.start_operation Strings.GetString("Reverse to view and Back to Front"),true,false,false 
				view=model.active_view 
				status=nil
				@faces.each{|f| 
					flag=true;ray=nil
					f[1].each{|v|                        
						pv=view.screen_coords(v)
						ray = view.pickray pv.x,pv.y
						ray=[v,ray[1].reverse]
						flag=false if model.raytest(ray, false)
					}	                                   
					normal=f[2]
					ang=normal.angle_between ray[1]     
					f[0].reverse! if flag && ((ang> Math::PI*0.5) || (normal.samedirection?(ray[1].reverse)))
					if f[0].material && (!status)
						status=UI.messagebox(Strings.GetString("Do you want to Overwrite front material?"),MB_YESNO)
					end
					f[0].material=f[0].back_material if (f[0].back_material && status==6 && f[0].material) || ((!f[0].material) && f[0].back_material)
				} 
				model.commit_operation
			end
		end 
		def self.select_face(element,t=Geom::Transformation.new) 
			entities=[]
			if element.is_a?Sketchup::ComponentInstance
				entities=element.definition.entities     
			elsif element.is_a?Sketchup::Group 
				element.make_unique
				entities=element.entities 
			end  
			entities.each{|s| self.select_face(s,t*element.transformation) if s.is_a?Sketchup::ComponentInstance or s.is_a?Sketchup::Group  }     			
			entities.each{|f| 
				if f.is_a? Sketchup::Face
					vers=f.vertices.collect{|v| v.position.clone.transform(t*element.transformation)}
					@faces<< [f,vers,f.normal.clone.transform(t*element.transformation)]                                       
				end
			}
		end
end