import time
from threading import Thread
import pyautogui
from core.Logger import Logger
from core.Player import PLAYER_STATE

class IdlingAgent:
  def __init__(self, monitor, playerAgent) -> None:
    self.logger = Logger("Idling Agent")
    self.idleTime = time.time()
    self.idleThread = None
    self.player = playerAgent
    self.monitor = monitor

  def checkIdleTime(self):
    while self.player.state in (PLAYER_STATE.IDLE, PLAYER_STATE.BG_FARMING):
      elapsedTime = time.time() - self.idleTime
      if (elapsedTime > 60):
        self.logger.log("Jumping to avoid being afk...")
        pyautogui.press("space")
        self.idleTime = time.time()
      time.sleep(0.5)

  def run(self):
    self.logger.log("Starting Idle Thread...")

    self.idleThread = Thread(
        target=self.checkIdleTime, 
        args=(),
        name="idling thread",
        daemon=True)
    self.idleThread.start()