# 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'
require 'LangHandler.rb'

$totdStrings = LanguageHandler.new("GettingStarted",true)

class TotdDialog < UI::WebDialog
    @@instance = nil
    
    def initialize
        @nEntry = 0
        @show_on_startup = true
        @file_loaded = false
        @mydir = File.dirname(__FILE__)
		@help_contents_dir = LanguageHandler::GetHelpContentDir();
        
        super($totdStrings.GetString("Learning Center"), false, "TOTD", 700, 480, 250, 240, false)        
        
        
        # get the list of "real" directories in the Tip subdirectory
        init_dirs();

        add_action_callback("loadSkpFile") 		{ |d,p| loadSkpFile(p)}
        add_action_callback("save_state")  	   	{ |d,p| @show_on_startup = p=="true"; save_state()}
        add_action_callback("require")     	   	{ |d,p| require(@tips_dir + @tip_entries[@nEntry] + "/" + p) }
        add_action_callback("eval")        	   	{ |d,p| eval(p) }
        add_action_callback("on_init")     	   	{ |d,p| initialize_page(d,p) }
        add_action_callback("init_resources")   { |d,p| initialize_resources(d,p) }
        add_action_callback("show_model_info") 	{ |d,p| show_model_info(p) }
        add_action_callback("show_inspector")  	{ |d,p| show_inspector(p) }

		@nEntry = 0
        load_state()		
		if(@tip_entries.length !=0)
			set_file(@tips_dir + @tip_entries[@nEntry] + "/index.html", nil)  # the second argument is relative-to file
		end
    end

	def show_model_info(p)
		UI::show_model_info(p)
	end
	
	def show_inspector(p)
		UI::show_inspector(p)
	end
	
	def initialize_resources(dlg,p)
		# we need the location of the getting started PDF
		qrc = Sketchup.find_support_file("QuickReferenceCard.pdf");
		if(qrc != nil)
			dlg.execute_script("window.qrc = '" + qrc + "'");
		else
			dlg.execute_script("window.qrc = void 0");
		end
		
	end

	def initialize_page(dlg,page_num)
		@nEntry = page_num.to_i
		update_page();
		save_state();
		dlg.execute_script("window.is_pro = " + ((Sketchup.app_name.index("Pro") != nil) ? "true" : "false"));
	end
	
    def init_dirs()
        @tips_dir = @help_contents_dir + "/Tip/Tips/";
        @tip_entries = []
        dirs = Dir.entries(@tips_dir).sort;
        # remove . and .. and anything that's not a directory
        dirs.each {|i| @tip_entries.push(i) if File.stat(@tips_dir + i).directory? && i != "." && i != ".." }
    end
    
    attr_reader :show_on_startup, :nEntry
    
    private :initialize

    def TotdDialog::startup
        get_instance.on_startup
    end
    
    def on_startup
        if(@show_on_startup==true)
            show{ update_page }
        end
    end
    
    def TotdDialog::reset_instance
        @@instance = TotdDialog.new
    end 
    
    def TotdDialog::get_instance
        if @@instance == nil
            @@instance = TotdDialog.new
        end
        return @@instance
    end
    

    def loadSkpFile(file)
        Sketchup.open_file(@help_contents_dir + "/SelfPacedTutorials/" + file)
    end

    
    def save_state()   
        Sketchup.write_default("Google SketchUp TOTD", "ShowOnStartUp", @show_on_startup ? "true" : "false");
        Sketchup.write_default("Google SketchUp TOTD", "HelpPage", @nEntry);
    end
    
    def load_state()
        show_on_startup  = Sketchup.read_default("Google SketchUp TOTD", "ShowOnStartUp", "true");
        @nEntry          = Sketchup.read_default("Google SketchUp TOTD", "HelpPage", 0);
        @show_on_startup = show_on_startup == "true";
    end
    
    def update_page
        if(@show_on_startup)
            execute_script("setStartup(true)")
        else
            execute_script("setStartup(false)")
        end
    end    
        
    def TotdDialog::is_visible?
        @@instance == nil ? false : @@instance.visible?
    end
    
    def TotdDialog::toggle_dialog
        if(!TotdDialog::get_instance().visible?)
            TotdDialog::reset_instance();
            dlg = TotdDialog::get_instance()
            dlg.show  { dlg.update_page }
        else
            TotdDialog::get_instance().close()
        end
    end


end
file_loaded "TOTD.rb"



