import pygame as pg
import sys
import numpy as np
from tile import Tile
from config import *


class Agent:
    def __init__(self):
        self.prev = (4, 4)
        self.pos = (4, 4)
        self.dir = "E"

    def turn(self, a, b):
        x_mov = a[0] - b[0]
        y_mov = a[1] - b[1]
        if (x_mov, y_mov) == (0, 1):
            self.dir = "S"
        if (x_mov, y_mov) == (0, -1):
            self.dir = "N"
        if (x_mov, y_mov) == (1, 0):
            self.dir = "E"
        if (x_mov, y_mov) == (-1, 0):
            self.dir = "W"

    def display(self, step=None):
        pg.time.delay(150)
        agent_img = pg.image.load('tractor.png').convert_alpha()
        if step is not None:
            self.prev = self.pos
            self.pos = step
        self.turn(self.pos, self.prev)
        if self.dir == "N":
            angle = 270
        elif self.dir == "E":
            angle = 180
        elif self.dir == "S":
            angle = 90
        elif self.dir == "W":
            angle = 0
        agent_img = pg.transform.rotate(agent_img, angle)
        screen.blit(agent_img, (self.pos[0] * tile_size, self.pos[1] * tile_size))
        pg.display.flip()