import random
from time import time
from window_capture import WindowCapture
from vision import Vision
import cv2 as cv
import pytesseract
from hsvfilter import HsvFilter
from config_file import UserConfigs
# import pyautogui
import pydirectinput
import keyboard


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

    # initialize the StunWindowCapture class
    try:
        capture_window = WindowCapture(
            None, "magic", 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 = Vision()
    # initialize the StunOverlay class
    hsv_filter_orange = HsvFilter(10, 156, 0, 17, 255, 255, 0, 0, 0, 0)
    hsv_filter_p = HsvFilter(130, 156, 0, 179, 255, 255, 0, 0, 0, 0)
    hsv_filter_b = HsvFilter(88, 156, 0, 128, 255, 255, 0, 0, 0, 0)
    hsv_filter_g = HsvFilter(34, 156, 0, 74, 255, 255, 0, 0, 0, 0)
    hsv_filter_y = HsvFilter(24, 156, 0, 33, 255, 255, 0, 0, 0, 0)
    hsv_filter_0 = HsvFilter(0, 0, 0, 179, 255, 255, 255, 0, 0, 0)
    hsv_filter_w = HsvFilter(69, 25, 0, 94, 255, 255, 0, 0, 0, 0)

    loop_time = time()
    pause = True

    magic_list = {"1": "body", "2": "finding", "3": "mind", "4": "perceiving", "5": "physical", "6": "seeing",
                  "7": "spiritual"}
    tier_list = {"1": "0", "2": "orange", "3": "y", "4": "g", "5": "b", "6": "p"}  # , "w"}
    hsv_filter_list = {"1": hsv_filter_0, "2": hsv_filter_orange, "3": hsv_filter_y, "4": hsv_filter_g,
                       "5": hsv_filter_b, "6": hsv_filter_p}

    needle_list = []
    hsv_list = []
    for key1 in tier_list:
        for key2 in magic_list:

            needle_list.append(cv.imread("magic/" + magic_list[key2] + "_" + tier_list[key1] + ".jpg",
                                                  cv.IMREAD_UNCHANGED))
            hsv_list.append(hsv_filter_list[key1])


    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

        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

        spawn_1 = vision_stun.find(screenshot, cv.imread("magic/spawn_1.jpg", cv.IMREAD_UNCHANGED), 0.5, 1)
        if len(spawn_1) == 1:
            spawn_button_active = True
            points = vision_stun.get_click_points(spawn_1)
            for i in range(0, 200, 1):
                pydirectinput.moveTo(points[0][0], points[0][1])
                pydirectinput.mouseDown()
                w = random.randint(1, 50)
                cv.waitKey(30 + w)
                pydirectinput.mouseUp()
        else:
            spawn_0 = vision_stun.find(screenshot, cv.imread("magic/spawn_0.jpg", cv.IMREAD_UNCHANGED), 0.7, 1)
            points = vision_stun.get_click_points(spawn_0)
            for point in points:
                pydirectinput.moveTo(point[0], point[1])
                pydirectinput.mouseDown()
                cv.waitKey(500)
                pydirectinput.mouseUp()
            continue

        # for needles in needle_list:
        for rer in range(0, len(needle_list), 1):
        #for needle in needle_list:
            # do object detection
            # processed_image = vision_stun.apply_hsv_filter(screenshot, hsv_filter)
            # screenshot = capture_window.get_screenshot()
            #cv.imshow("output_image", needle_list[rer])
            #cv.waitKey(150)

            while True:
                screenshot = capture_window.get_screenshot()
                processed_screenshot = vision_stun.apply_hsv_filter(screenshot, hsv_list[rer])
                processed_needle = vision_stun.apply_hsv_filter(needle_list[rer], hsv_list[rer])

                #cv.imshow("output_image", processed_screenshot)
                cv.imshow("output_needle", processed_needle)
                cv.waitKey(150)

                rectangles = vision_stun.find(processed_screenshot, processed_needle, 0.70, 2)
                # 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)
                if len(points) == 2:
                    pydirectinput.moveTo(points[0][0], points[0][1])
                    pydirectinput.mouseDown()
                    w = random.randint(1, 100)
                    cv.waitKey(250 + w)
                    pydirectinput.moveTo(points[1][0], points[1][1])
                    pydirectinput.mouseUp()
                    if keyboard.is_pressed('p') == True or pause == True:
                        pause = True
                        break
                else:
                    break
                if keyboard.is_pressed('p') == True or pause == True:
                    pause = True
                    break
            if keyboard.is_pressed('p') == True or pause == True:
                pause = True
                break

        needles_white = []
        needles_white.append(cv.imread("magic/body_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/finding_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/mind_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/perceiving_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/physical_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/seeing_w.jpg", cv.IMREAD_UNCHANGED))
        needles_white.append(cv.imread("magic/spiritual_w.jpg", cv.IMREAD_UNCHANGED))

        for needle_w in needles_white:
            # do object detection
            screenshot = capture_window.get_screenshot()
            processed_screenshot = vision_stun.apply_hsv_filter(screenshot,hsv_filter_w)
            processed_needle = vision_stun.apply_hsv_filter(needle_w, hsv_filter_w)
            rectangles = vision_stun.find(processed_screenshot, processed_needle, 0.7, 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)

            points = vision_stun.get_click_points(rectangles)
            if len(points) >= 1:
                pydirectinput.moveTo(points[0][0], points[0][1])
                pydirectinput.mouseDown()
                w = random.randint(1, 100)
                cv.waitKey(100 + w)
                pydirectinput.mouseUp()
                screenshot = capture_window.get_screenshot()
                rectangles = vision_stun.find(screenshot, cv.imread("magic/collect.jpg", cv.IMREAD_UNCHANGED), 0.8, 1)
                points = vision_stun.get_click_points(rectangles)
                if len(points) >= 1:
                    pydirectinput.moveTo(points[0][0], points[0][1])
                    pydirectinput.mouseDown()
                    w = random.randint(1, 100)
                    cv.waitKey(100 + 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

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


if __name__ == "__main__":
    run()
