module Robot
	
	class Position

		attr_reader :x, :y, :dir

		#Constants 
		DIRECTIONS = {'NORTH' => 1, 'EAST'  => 1, 'SOUTH'  => -1, 'WEST'  => -1}
		TURN_DIR = {'RIGHT'  => 1, 'LEFT'  => -1}

	    def initialize(_x, _y, _dir)
	      @x = _x
	      @y = _y
	      @dir = _dir
	    end

	    #Getting next position according the current position and direction
	    def next
	    	case @dir
				when 'NORTH', 'SOUTH'
					next_y = @y + DIRECTIONS[@dir]
					self.class.new(@x,  next_y , @dir)
				
				when 'EAST', 'WEST'
					next_x = @x + DIRECTIONS[@dir]
					self.class.new(next_x, @y, @dir)
			end
		end

		#Turn the robot 90 degrees according the current direction
	    def turn(_dir)
		    index = DIRECTIONS.keys.index(@dir)
		    index += TURN_DIR[_dir]
		    index = 0 if index > DIRECTIONS.keys.count - 1
		  
		  	turn_dir = DIRECTIONS.keys[index]
		  	self.class.new(@x, @y, turn_dir)
		end

	end 
end