from ahk import AHK
import os
from random import randint
from league_of_legends_requests import league_of_legends_client_api

ahk = AHK()

import time

account = ""
psw = ""


def main():

    # Countdown Timer
    print("Starting",end="")
    for _ in range(0,10):
        print(".",end="",flush=True)
        time.sleep(1)
    print("Go")

    nome_utente_password(account,psw)
    time.sleep(120)
    first_queque()
    
    # ahk.mouse_move(x=100, y=100, blocking=True)  # Blocks until mouse finishes moving (the default)
    # ahk.mouse_move(x=150, y=150, speed=10, blocking=True) # Moves the mouse to x, y taking 'speed' seconds to move
    # print(ahk.mouse_position)  #  (150, 150)\
    # queque()
    while True:
        try:
            if ahk.key_state("space") == True:
                combo()
        except:
            print("Uscita dal Bot...")
            break
    # Done
    print('Done')


def combo():
    while True:
        try:
                data = league_of_legends_client_api()
                health = data['championStats']['currentHealth']
                gold = data["currentGold"]
                lvl = data["level"]
                abilities_E = data["abilities"]["E"]["abilityLevel"]
                abilities_Q = data["abilities"]["Q"]["abilityLevel"]
                abilities_W = data["abilities"]["W"]["abilityLevel"]
                abilities_R = data["abilities"]["R"]["abilityLevel"]
                sum_abilities = abilities_E + abilities_Q + abilities_W + abilities_R
                if sum_abilities != lvl:
                    ahk.key_down('Control')
                    ahk.key_down('q')
                    time.sleep(0.3)
                    ahk.key_up('Control')
                    ahk.key_up('q')
                if abilities_Q > 0:  
                    ahk.key_press('q')
                if abilities_E > 0:
                    ahk.key_press('e')
                if abilities_R > 0:
                    ahk.key_press('r')
                break
        except:
            break


def nome_utente_password(account,psw):
    script_dir = os.path.dirname(__file__)
    nome_utente_path = os.path.join(
        script_dir,
        'lolbot',
        'nomeutente.png'
    )
    password_path = os.path.join(
        script_dir,
        'lolbot',
        'password.png'
    )
    login_path = os.path.join(
        script_dir,
        'lolbot',
        'login.png'
    )
    
    time.sleep(1)
    nome_utente = ahk.image_search(nome_utente_path)
    ahk.click(nome_utente[0],nome_utente[1])
    ahk.type(account)
    time.sleep(2)
    ahk.click(nome_utente[0],nome_utente[1] + 66)
    ahk.type(psw)
    ahk.key_press('enter')
    # login = ahk.image_search(login_path)
    # ahk.click(login[0],login[1])


def first_queque():
    script_dir = os.path.dirname(__file__)
    play_image_path = os.path.join(
        script_dir,
        'lolbot',
        'gioca.png'
    )
    coopvsia_image_path = os.path.join(
        script_dir,
        'lolbot',
        'coopvsia.png'
    )
    select_coop_mode = os.path.join(
        script_dir,
        'lolbot',
        'introduzione.png'
    )
    confirm = os.path.join(
        script_dir,
        'lolbot',
        'conferma.png'
    )
    found_match = os.path.join(
        script_dir,
        'lolbot',
        'trovapartita.png'
    )
    accept_match = os.path.join(
        script_dir,
        'lolbot',
        'accetta.png'
    )
    # image_positon = pyautogui.locateOnScreen(image_path)
    play_image_positon = ahk.image_search(play_image_path)
    ahk.click(play_image_positon[0],play_image_positon[1])
    time.sleep(2)
    coopvsia_positon = ahk.image_search(coopvsia_image_path)
    ahk.click(coopvsia_positon[0] + 20,coopvsia_positon[1] + 25)
    time.sleep(2)
    introduction_positon = ahk.image_search(select_coop_mode)
    ahk.click(introduction_positon[0],introduction_positon[1])
    # print(introduction_positon)
    time.sleep(2)
    confirm_position = ahk.image_search(confirm)
    ahk.click(confirm_position[0],confirm_position[1])
    # print(confirm_postion)
    time.sleep(2)
    ahk.mouse_move(-100, -100, speed=10, relative=True)
    time.sleep(2)
    found_match_position = ahk.image_search(found_match)
    ahk.click(found_match_position[0],found_match_position[1])
    time.sleep(2)
    accept_match_position = ahk.image_search(accept_match)
    ahk.click(accept_match_position[0],accept_match_position[1])

    # coopvsia_image_position = ahk.image_search(coopvsia_image_path)
    # ahk.click(coopvsia_image_position[0],coopvsia_image_position[1])


if __name__ == "__main__":
    main()