# See the loader file for license and author info

require 'sketchup.rb'
module CLF_Extensions_NS

  module CLF_Random_Painter

    def self.rand_painter
      
      #Sets the selection for this ruby to whatever is currently selected
      model = Sketchup.active_model
      clselection = model.selection
      #If nothing is selected, then it returns an message to select something first
      if clselection.empty?
          UI.messagebox "Please select the faces to be painted first"
          resturn
      end
      
      # Initialize a couple of arrays and a variable for use later
      results = []
      color = []
      count = 0
      
      # Starts the Inputbox and makes it keep popping up until no values are entered
      while results != ["", "", ""]
        prompts = ["R:", "G:", "B:"]
        values = []
        results = inputbox( prompts, values )
        color.push results
      end
      
      #Removes the empty array created when the user hits "ok" on the blank inputbox
      color.pop

      model.start_operation("Random Color", true)

      # This takes the selection and runs through each entity selected and decides if it is a face.  
      # If it is a face, then one of the colors previously input by the user gets applied to it.
      clselection.each do
        clent = clselection[count]
          if clent.typename == ("Face")
          rand_col = rand(color.length).to_i
          puts color[rand_col].to_a
          paintcolor = color[rand_col]
          clent.material = [paintcolor[0].to_i, paintcolor[1].to_i, paintcolor[2].to_i]
          clent.back_material=[paintcolor[0].to_i, paintcolor[1].to_i, paintcolor[2].to_i]
        end
        count = count + 1
      end
      
      # This closes the undo stack.  Everything between the start and commit are looked at as a single undo command.
      model.commit_operation
      Sketchup.send_action "selectSelectionTool:"
    end
  end
end

