# Drawer Debugger

# I worked on this challenge [by myself].
# I spent [#] hours on this challenge.

# Original Code

# class Drawer

# attr_reader :contents

# # Are there any more methods needed in this class?

# def initialize
# @contents = []
# @open = true
# end

# def open
# @open = true
# end

# def close
# @open = false
# end

# def add_item
# @contents << item
# end

# def remove_item(item = @contents.pop) #what is `#pop` doing?
# @contents.delete(item)
# end

# def dump  # what should this method return?
# puts "Your drawer is empty."
# end

# def view_contents
# puts "The drawer contains:"
# @contents.each {|silverware| puts "- " + silverware.type }
# end


# class Silverware
# attr_reader :type

# # Are there any more methods needed in this class?

# def initialize(type, clean = true)
# @type = type
# @clean = clean
# end

# def eat
# puts "eating with the #{type}"
# @clean = false
# end

# end

# Initial

# class Drawer

#   attr_reader :contents

#   # Are there any more methods needed in this class?

#   def initialize
#     @contents = []
#     @open = true
#   end
  
#   def open
#     @open = true
#   end

#   def close
#     @open = false
#   end

#   def add_item(item)
#     @contents << item
#   end

#   def remove_item(item = @contents.last) #what is `#pop` doing? Default is take last item in @contents. Replaced #pop with #last
#     @contents.delete(item)
#   end

#   def dump  # what should this method return? I'm guessing all the items
#     @removed_contents = @contents.dup
#     @contents.clear
#     if (@removed_contents.empty?)
#       puts "Your drawer is empty."
#     end
#     @removed_contents
#   end

#   def view_contents
#     puts "The drawer contains:"
#     @contents.each {|silverware| puts "- " + silverware.type }
#   end

# end # End class drawer 

# class Silverware
#   attr_reader :type

#   # Are there any more methods needed in this class?

#   def initialize(type, clean = true)
#     @type = type
#     @clean = clean
#   end
  
#   def eat
#     puts "eating with the #{type}"
#     @clean = false
#   end

#   def clean
#     @clean
#   end
  
#   def clean_silverware
#     @clean = true
#   end
  
# end

# REFACTOR

class Drawer

  attr_reader :contents

  # Are there any more methods needed in this class?

  def initialize
    @contents = []
    @open = true
  end
  
  def open
    @open = true
  end

  def close
    @open = false
  end

  def add_item(item)
    @contents << item
  end

  def remove_item(item = @contents.last)
    @contents.delete(item)
  end

  def dump  
    removed_contents = @contents.dup
    @contents.clear
    if (removed_contents.empty?)
      puts "Your drawer is empty."
    end
    removed_contents
  end

  def view_contents
    puts "The drawer contains:"
    @contents.each {|silverware| puts "- " + silverware.type }
  end

end # End class drawer 

class Silverware
  attr_reader :type, :clean

  # Are there any more methods needed in this class?

  def initialize(type, clean = true)
    @type = type
    @clean = clean
  end
  
  def eat
    puts "eating with the #{type}"
    @clean = false
  end

  def clean_silverware
    @clean = true
  end
  
end


  # DO NOT MODIFY THE DRIVER CODE UNLESS DIRECTED TO DO SO
  knife1 = Silverware.new("knife")

  silverware_drawer = Drawer.new
  silverware_drawer.add_item(knife1)
  silverware_drawer.add_item(Silverware.new("spoon"))
  silverware_drawer.add_item(Silverware.new("fork"))
  silverware_drawer.view_contents

  silverware_drawer.remove_item
  silverware_drawer.view_contents
  sharp_knife = Silverware.new("sharp_knife")


  silverware_drawer.add_item(sharp_knife)

  silverware_drawer.view_contents

  removed_knife = silverware_drawer.remove_item(sharp_knife)
  removed_knife.eat
  removed_knife.clean_silverware
  raise Exception.new("Your silverware is not actually clean!") unless removed_knife.clean_silverware == true

  silverware_drawer.view_contents
  silverware_drawer.dump
  raise Exception.new("Your drawer is not actually empty") unless silverware_drawer.contents.empty?
  silverware_drawer.view_contents

  # What will you need here in order to remove a spoon? You may modify the driver code for this error.
  spoon = Silverware.new("spoon")
  silverware_drawer.add_item(spoon)  
  raise Exception.new("You don't have a spoon to remove") unless silverware_drawer.contents.include?(spoon)
  silverware_drawer.remove_item(spoon) #What is happening when you run this the first time?
  spoon.eat
  puts spoon.clean #=> this should be false

  # DRIVER TESTS GO BELOW THIS LINE
  silverware_drawer.add_item(Silverware.new("knife"))
  silverware_drawer.view_contents
  puts "Clearing drawer out..."
  silverware_drawer.dump
  silverware_drawer.view_contents  
  puts "One more time..."
  silverware_drawer.dump # Confirm shows as empty
  silverware_drawer.view_contents
  puts "Adding 4 spoons, two of which are anonymous..."
  spoon1 = Silverware.new("spoon")
  spoon2 = Silverware.new("spoon")
  silverware_drawer.add_item(spoon1)
  silverware_drawer.add_item(spoon2)
  silverware_drawer.add_item(Silverware.new("spoon"))
  silverware_drawer.add_item(Silverware.new("fork"))
  silverware_drawer.add_item(Silverware.new("spoon"))
  silverware_drawer.view_contents 

  puts "Removing 1 spoon. This should show 3 spoons remaining..."
  silverware_drawer.remove_item(spoon1)
  silverware_drawer.view_contents 

  puts "Removing 1 more spoon. This should show 2 spoons remaining..."
  silverware_drawer.remove_item(spoon2)
  silverware_drawer.view_contents 

# Reflection

  # * What concepts did you review in this challenge?

  # The concepts of attr_reader, working with ruby objects, queues.
  
  # * What is still confusing to you about Ruby?

  # I am comfortable except with the advanced topics of enumerables as discussed in WGR.

  # * What are you going to study to get more prepared for Phase 1?

  # I will review enumerables and more built-in functions of array and hash.
  
