# Copyright 2006, Google, Inc.

# This software is provided as an example of using the Ruby interface
# to SketchUp.

# Permission to use, copy, modify, and distribute this software for 
# any purpose and without fee is hereby granted, provided that the above
# copyright notice appear in all copies.

# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------
require 'sketchup.rb'

module UnitsWizard

	def UnitsWizard::units_wizard()

		html = File.dirname(__FILE__) + "/units.html";
		if(html.length == 0)
			return false;
		end
		template = ""
		
		dlg = UI::WebDialog.new("Choose Default Settings", true, "Units Picker", 739, 641, 150, 150, true);
		template_paths = Sketchup.find_support_files("skp", "Templates");

		args = ""
		titles=[]
		pairs = {}
		template_paths.each { |t|
			
			split_name = File.basename(t,".skp").split('-');
			title = split_name[0];
			dim = split_name.size>1 ? split_name[1].split('.')[0] : nil
			if(dim == nil || (dim != "3D" && dim != "2D"))
				next;
			end
			
			if(nil == pairs[title])
				pairs[title]=1
			else
				pairs[title] += 1
			end
		}
		
		template_paths.each { |t|
			split_name = File.basename(t,".skp").split('-');
			title = split_name[0];
		
			if(nil == titles.index(title) && pairs[title] == 2)
				if(args.length != 0)
					args += "," 
				end
				args += "'" + title + "'"
				titles.push(title)
			end
		}	

		if(titles.size == 0)
			return;
		end;
		template = template_paths[1];
		Sketchup::template = template;			
		Sketchup::file_new;
		Sketchup.send_action "closeDocument:"				
		
		first_load = true;
		
		dlg.add_action_callback("on_close") {|d,p| d.close(); }
		dlg.add_action_callback("onload") {|d,p| 
			if first_load
				first_load = false;
				return;
			end
			cmd = "init_templates(" + args + ")";
			d.execute_script(cmd);
		}

		dlg.add_action_callback("small_window") {|d,p| 
			d.set_size(750,600);
			dlg.execute_script("window.location.reload()");
		}
		
		
		
		dlg.set_on_close {
			if Sketchup::template != template;
				Sketchup::template = template
				Sketchup.send_action "closeDocument:"
			end
			Sketchup.file_new
			
			if file_loaded? "TOTD.rb"
				TotdDialog::get_instance.bring_to_front 
			end
		}

		dlg.add_action_callback("on_template_changed") {|d,p|
			a = p.split(',');
			t = a[0];
			d = a[1];
			# put them together
			t += "-" + d;
			# find it in the template_paths
			template_paths.each {|p|
				if(t == File.basename(p,".skp"))
					template = p;
					return
				end
			}
		}
		
		dlg.add_action_callback("on_ok") {|d,p|
			d.close();
		}
		
		dlg.set_background_color("f3f0f0");
		dlg.set_file(html, nil)
		
		dlg.show_modal{ 
			cmd = "init_templates(" + args + ")";
			dlg.execute_script(cmd);

			dlg.execute_script("sizeWindow()");
		}
	end

end # module UnitsWizard
