import json
from enum import Enum
import time
import cv2 as cv
import pyautogui
from core.Logger import Logger

CONNECT_STATUS = Enum('CONNECT_STATUS', ["CONNECTED", "CONNECTING", "DISCONNECTED"])
CLASSES = Enum('CLASSES', ['DRUID', 'MAGE', 'HUNTER', 'PRIEST'])
PLAYER_STATE = Enum('STATE', ['IDLE', 'FISHING', 'FARMING', 'BG_FARMING'])

class Player:
  def __init__(self, monitor) -> None:
    with open("config.json", encoding="utf-8") as jsonFile:
      self.config = json.load(jsonFile)
    self.isConnected = None
    self.idleThread = None
    self.enterWorldButton = cv.imread("assets/menus/EnterWorldButton.jpg", cv.IMREAD_GRAYSCALE)
    self.logger = Logger("Player Agent")
    self.state = PLAYER_STATE.IDLE
    self.monitor = monitor
    self.playerClass = self.config["class"]
    self.playerName = self.config["name"]
    self.logger.log("Connected with " + self.playerName + " (" + self.playerClass + ")")

  def checkConnection(self):
    res = self.monitor.findMatch(self.enterWorldButton)
    if (res == -1):
      self.isConnected = CONNECT_STATUS.CONNECTED
    else:
      self.isConnected = CONNECT_STATUS.DISCONNECTED
      self.logger.log("Player not connected attempting to connect...")
      pyautogui.move(res[0], res[1])
      time.sleep(0.4)
      pyautogui.click()
      time.sleep(5)

  def changeState(self, newState):
    self.state = newState