# Copyright 2005, @Last Software, 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'

class LanguageHandler

    def initialize(fileName, use_help_dir = false)

        @strings = Hash.new;
        @use_help_dir = use_help_dir;
        self.ParseLangFile(fileName)

    end

    def GetFullPath(fileName)

        langCode = Sketchup.get_locale
        
		if(@use_help_dir)
			fullFileName = LanguageHandler::GetHelpContentDir() + "/" + fileName + langCode + ".strings"
		else
			fullFileName = fileName + langCode + ".strings"
		end
        
		if(FileTest.exist?(fullFileName))
			return fullFileName;
		else
			return nil
		end
		            
    end

    def ParseLangFile(fileName)

        filePath = self.GetFullPath(fileName)
        
        if filePath==nil
        
            return false
            
        end
        
        langFile = File.open(filePath, "r")

		entryString = ""
		inComment = false
		
		langFile.each do |line|
				
			if !line[0,5].include?("//")
	        
				if line.include?("/*")
				
					inComment = true
					
				end
										
				if inComment==true and line.include?("*/")
				
					inComment=false
					
				else
				
					if inComment==false
					
						entryString += line
						
					end
	            	    
	            end
	            	    	            	    
   			end
   			
   			if entryString.include?(";")
   			
   				#parse the string into key and value
   				
   				#remove the white space
   				entryString.strip!

   				#pull out the key
   				keyvalue = entryString.split("\"=\"")
   				
   				key = keyvalue[0].gsub("\"", "")

   				#pull out the value
   				keyvalue[1].gsub!(";", "")
   				value = keyvalue[1].gsub("\"", "")   					

   				#add to @strings
   				@strings[key]=value
   				
   				entryString = ""
   			
   			end
	        
		end
		                                                
        return true
        
    end

    def GetString(key)
        
        retval = @strings[key]

        if retval!= nil
        
            retval.chomp!
            
        else
        
            retval = key
            
        end

        return retval
        
    end

	def LanguageHandler::GetHelpContentDir()
		# determine the language
		lang = "_" + Sketchup.os_language;

		help_contents_dir = File.dirname(__FILE__)+ "/HelpContent" + lang

		if(!File.exists?(help_contents_dir) || !File.stat(help_contents_dir).directory?)
			help_contents_dir = File.dirname(__FILE__)+ "/"+ "HelpContent_en"
		end
		
		return help_contents_dir;
	end
end