class BOT:
    def __init__(self, grid, source, destination, current_direction):
        self.direction = current_direction
        self.GRID = grid
        self.current_position = source
        self.destination = destination
        self.app = App(grid, source, destination)
    
    def main(self):
        #app = App(self.GRID)
        #self.printGrid(GRID_SIZE)
        while(self.current_position != self.destination):
            next_x,next_y = self.app.next_step(self.current_position[0],self.current_position[1])
            if(self.is_safe(next_x,next_y)):
                print "Next x = %d, Next y = %d is safe" %(next_x,next_y)
                self.move_straight(next_x,next_y)
            else:
                print "Encountered obstacle at Next x = %d, Next y = %d" %(next_x,next_y)
                self.app.mark_obstacle(next_x,next_y)
                #self.app.update_grid(self.current_position[0],self.current_position[1])
				#import ipdb; ipdb.set_trace()
                
    def printGrid(self,n):    
        for i in range(n):
            for j in range(n):
                print self.GRID[i][j],
            print
        print self.current_position
        print self.destination
        
    def is_safe(self,next_x,next_y):
        print "current direction %s" %self.direction
        if(self.current_position[0] < next_x):
            self.turn_north()
        elif(self.current_position[0] > next_x):
            self.turn_south()
        elif(self.current_position[1] < next_y):
            self.turn_west()
        else:
			self.turn_east()
        if(next_x == 2 and next_y == 1):
            return False
        if(self.GRID[next_x][next_y] == "obstacle"):
            return False
        else:
		    return True
        		 
    def turn_south(self):
        print "Moving south now"
        if(self.direction == "north"):
            self.turn_180_deg()
        elif(self.direction == "east"):
            self.turn_90_deg_clock_wise()
        elif(self.direction == "west"):
            self.turn_90_deg_anti_clock_wise()
        else:    
            pass
        self.direction = "south"
            
    def turn_north(self):
        print "Moving north now"
        if(self.direction == "south"):
            self.turn_180_deg()
        elif(self.direction == "west"):
            self.turn_90_deg_clock_wise()
        elif(self.direction == "east"):
            self.turn_90_deg_anti_clock_wise()
        else:
            pass
        self.direction = "north"
        
    def turn_east(self):
        print "Moving east now"
        if(self.direction == "west"):
            self.turn_180_deg()
        elif(self.direction == "south"):
            self.turn_90_deg_anti_clock_wise()
        elif(self.direction == "north"):
            self.turn_90_deg_clock_wise()
        else:
            pass
        self.direction = "east"
        
    def turn_west(self):
        print "Moving west now"
        if(self.direction == "east"):
            self.turn_180_deg()
        elif(self.direction == "north"):
            self.turn_90_deg_anti_clock_wise()
        elif(self.direction == "south"):
            self.turn_90_deg_clock_wise()
        else:
            pass
        self.direction = "west"
    
    def turn_180_deg(self):
        print "Turning 180 degree"
        self.turn_90_deg_clock_wise()
        self.turn_90_deg_clock_wise()
        
    def turn_90_deg_clock_wise(self):
        print "Turning 90 degree clockwise"
    
    def turn_90_deg_anti_clock_wise(self):
        print "Turning 90 degree anticlockwise"    
    
    def move_straight(self,next_x,next_y):
        print "Moving straight now"
        
        self.app.moved(next_x, next_y)   
        self.current_position = (next_x, next_y)