require_relative '08-adventure-item' # for the weapon

class Character
  attr_accessor :position
  attr_accessor :health
  attr_accessor :name
  def initialize(name, health, position)
    @name=name
    @health=health
    @position=position
  end
  def get_hit_by(source, amount)
    @health -= amount
    if @health <= 0
      self.die
    else
      print "#{self}'s health is now only #{health}.\n"
    end
  end
  def die
    @health = 0
    print "The #{self} dies.\n"
  end
end


class Adventurer < Character
  attr_accessor :weapon

  def initialize(name, health, position)
    super name, health, position
    @items = Array.new
    @weapon = Weapon.new("club", 10)
    @position.describe
  end

  def add_item(item)
    @items << item
    puts "DEBUG: @items = #{@items}"
    if item.is_a? Weapon                # bad code!!
      wield item
    end
  end

  def wield(new_weapon)
    # drop current weapon in the room and wield the new one
    @position.items(@weapon)
    @weapon = new_weapon
  end
    
  
  def move(direction)
    # the user-specified direction is exactly the method names
    # in the room.
    begin
      newposition = @position.send(direction, self)
    rescue Exception => e
      puts e
      puts "You can't move there."
      newposition = @position
    ensure
      @position = newposition
      @position.describe
    end
  end

  def attacks_in(room)
    # printf "#{self} attacks the #{target}.\n"
    # target.get_hit_by(self, @weapon.hit_points)
    room.attack_by(self)
  end

  def position=(room)
    @position=room
    @position.describe
  end

  def to_s
    @name
  end

  def swill 
    # this is a hack. ideally should come from the keg's
    # capacity, which is placed in the room
    @health += 100
  end
  
  def has item_name
    result = false
    @items.each do |item|
      if item.name == item_name
        result = true
      end
    end
    result
  end

  def inventory
    @items.each {|i| print "#{i},"}
    print "\n"
  end

  def die
    puts 'Alas! The hero is dead.'
    exit
  end
end

class Monster < Character
  attr_accessor :weapon
  def initialize(name,weapon,health,position)
    super name, health, position
    @weapon=weapon
  end
  def to_s
    @name
  end
  def attack(target)
    if @weapon != nil
      puts "The #{self} attacks the #{target} with its #{weapon}."
      target.get_hit_by(self, weapon.hit_points)
    end
  end
  def die
    super
    @position.remove_monster(self)
  end
  def get_hit_by(source, amount)
    super source, amount
    attack(source) unless health <= 0
  end
end

class Troll < Monster
  def initialize(name, health, position)
    @weapon = Weapon.new "club", 2
    super name, @weapon, health, position
  end

  @@trollcount = 1
  def self.create # class method
    Troll.new  "troll", 50, nil 
  end
end

class Lizard < Monster
  def initialize(name, health, position)
    @weapon = nil
    super name, @weapon, health, position
  end

  def self.create # class method
    Lizard.new  "lizard", 10, nil
  end
end

class Dragon < Monster
  def initialize(name, health, position)
    @weapon=Weapon.new "breath", 50
    super name, @weapon, health, position
  end
  
  def self.create
    Dragon.new "dragon", 200, nil
  end

  def die
    @health=0
    puts 'The Quest is over. Smaug lies dead.'
  end
end
