from ahk import AHK
import os
import subprocess
from random import randint
import time
from league_of_legends_bot import combo
from league_of_legends_requests import league_of_legends_client_api
# Inizializziamo AHK AutoScript
ahk = AHK()


# Cartella dove é contenuta l'immagine
script_dir = os.path.dirname(__file__)
cs_life_path = os.path.join(
    script_dir,
    'lolbot',
    'enemycreephealth.png'
    )
my_cs_life_path = os.path.join(
    script_dir,
    'lolbot',
    'alliedcreephealth.png'
    )


shop_path = os.path.join(
    script_dir,
    'lolbot',
    'shop.png'
    )


enemy_path = os.path.join(
    script_dir,
    'lolbot',
    'enemyhealth.png'
    )

"""
Creo uno script per identificare la vita dei CS

"""

def cs_life(cs_life_path,my_cs_life_path):
    print("Starting",end="")
    for _ in range(0,10):
        print(".",end="",flush=True)
        time.sleep(1)
    print("Go")
    last_time = time.time()
    while True:
        try:
            # ahk.key_down("space")
            mouse_position = ahk.mouse_position
            data = league_of_legends_client_api()
            health = data['championStats']['currentHealth']
            gold = data["currentGold"]
            cs = ahk.image_search(cs_life_path)
            enemy = ahk.image_search(enemy_path)
            my_cs_life = ahk.image_search(my_cs_life_path)
            print(f"Miei Minion : {my_cs_life}")
            print(enemy)
            if cs == None and my_cs_life == None:
                ahk.key_down("b")
            elif my_cs_life is not None and cs == None:
                lol_move = ahk.mouse_move(my_cs_life[0], my_cs_life[1], speed=10)
                ahk.right_click(lol_move)
            elif health <= 300:
                ahk.key_down("b")
                if gold >= 1300:
                    shop = ahk.image_search(shop_path)
                    lol_move = ahk.mouse_move(my_cs_life[0], my_cs_life[1], speed=10)
                    ahk.right_click(lol_move)
            elif cs != None and enemy != None:
                lol_move = ahk.mouse_move(cs[0], cs[1], speed=10)
                enemy_move = ahk.mouse_move(enemy[0], enemy[1], speed=10)
                ahk.right_click(lol_move)
                ahk.right_click(enemy_move)
                combo()
                # cs_life(cs_life_path,my_cs_life_path)
            print("Loop took {} seconds".format(time.time()-last_time))
            last_time = time.time()
        except:
            print("Qualcosa é andato storto, riprovo...")
            cs_life(cs_life_path,my_cs_life_path)



if __name__ == "__main__":
    cs_life(cs_life_path,my_cs_life_path)