import random
from time import time
from window_capture import WindowCapture
from stun_vision import StunVision
import cv2 as cv
import pytesseract
from hsvfilter import HsvFilter
from config_file import UserConfigs
#import pyautogui
import pydirectinput
import keyboard
from tresh_util import super_tresh_main, super_tresh_needle


def run():
    # initialize the user-class
    config = UserConfigs()

    # initialize the StunWindowCapture class
    try:
        capture_window = WindowCapture(
            None, "stun", config)
        video_mode = False
    except:
        # StunWindowCapture.list_window_names()
        #print("Game not running, switching to video mode")
        #capture_window = cv.VideoCapture("snip_slam.mp4")
        video_mode = True

    # initialize the StunVision class
    vision_stun = StunVision()
    # initialize the StunOverlay class
    hsv_filter = HsvFilter(0, 0, 124, 15, 255, 168, 0, 255, 0, 0)

    loop_time = time()
    event_time = 0.0
    pointstore = []
    max_results = 0
    pause = True
    while True:
        if keyboard.is_pressed('p') == True:
            pause = True
            print('q pressed')
        elif keyboard.is_pressed('o') == True:
            pause = False
            print('o pressed')
        if pause:
            #cv.waitKey(500)
            print("pausing")
            continue

        '''
        # 30 units = 46 + 1 * 30
        for f in range(0, 30, 3):
            for i in range(430, 1705, 47 - f):
                for e in range(45, 470, 47 - f):

                    pydirectinput.moveTo(i, e)
                    pydirectinput.mouseDown()
                    w = random.randint(1, 100)
                    cv.waitKey(1000 + w)
                    pydirectinput.mouseUp()
                    if keyboard.is_pressed('p') == True or pause == True:
                        pause = True
                        break
                if keyboard.is_pressed('p') == True or pause == True:
                    pause = True
                    break
        '''
        

        if video_mode:
            break
        else:
            try:
                # get an updated image of the game
                screenshot = capture_window.get_screenshot()
                # screenshot = cv.imread("buffbar.jpg")
            except:
                capture_window.release()
                print("Game window not available - shutting down application")
                break
        #cv.imshow("screenshot", screenshot)
        #cv.waitKey(150)
        #continue

        needles = []
        #needles.append(cv.imread("wtf.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("Brown0.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("1.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("2.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("3.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("4.jpg", cv.IMREAD_UNCHANGED))
        needles.append(cv.imread("H1.jpg", cv.IMREAD_UNCHANGED))
        needles.append(cv.imread("H2.jpg", cv.IMREAD_UNCHANGED))
        needles.append(cv.imread("H3.jpg", cv.IMREAD_UNCHANGED))
        needles.append(cv.imread("H4.jpg", cv.IMREAD_UNCHANGED))
        needles.append(cv.imread("D1.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D2.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D3.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D3.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D4.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D5.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D6.jpg", cv.IMREAD_UNCHANGED))
        #needles.append(cv.imread("D7.jpg", cv.IMREAD_UNCHANGED))

        for needle in needles:
            # do object detection
            #processed_image = vision_stun.apply_hsv_filter(screenshot, hsv_filter)
            processed_image = super_tresh_needle(screenshot)
            processed_needle = super_tresh_needle(needle)
            rectangles = vision_stun.find(processed_image, processed_needle, 0.5, 1)
            # draw the detection results onto the original image
            output_image = vision_stun.draw_rectangles(screenshot, rectangles)
            cv.imshow("output_image", output_image)
            cv.waitKey(150)

            # only trigger ocr reading if a stun is detected
            points = vision_stun.get_click_points(rectangles)
            for point in points:
                if point not in pointstore:
                    pointstore.append(point)
                    pydirectinput.moveTo(point[0], point[1])
                    pydirectinput.mouseDown()
                    w = random.randint(1, 100)
                    cv.waitKey(400 + w)
                    pydirectinput.mouseUp()
                else:
                    max_results = max_results + 1
                # apply missing buff
                #print("buff missing" + str(i))

                #pydirectinput.click(500,500)
                #pydirectinput.press(key)
                #cv.waitKey(10)
                #pydirectinput.rightClick()
                #pydirectinput.press("C")
                #cv.waitKey(150)
                #pyautogui.rightClick()
                #cv.waitKey(10)
                #pydirectinput.press("c")
                #overlay_stun.update_stun_label("")
                if keyboard.is_pressed('p') == True or pause == True:
                    pause = True
                    break
            if keyboard.is_pressed('p') == True or pause == True:
                pause = True
                break

        # debug the loop rate
        print('FPS {}'.format(1 / (time() - loop_time)))
        loop_time = time()
        cv.waitKey(150)


if __name__ == "__main__":
    run()
