import random
import cv2 as cv
from hsvfilter import HsvFilter
import pydirectinput
from game_base_class import GameBase
from window_capture import WindowCapture


class Magic(GameBase):

    def __init__(self, overlay):
        super().__init__(overlay)

        self.SPAWN_COUNT = int(overlay.spawn_use.get())

        self.capture_window = WindowCapture(None, "magic", self.config)

        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)
        self.hsv_filter_w = HsvFilter(69, 25, 0, 94, 255, 255, 0, 0, 0, 0)

        self.magic_list = {"1": "body", "2": "finding", "3": "mind", "4": "perceiving", "5": "physical", "6": "seeing",
                           "7": "spiritual"}
        self.tier_list = {"1": "0", "2": "orange", "3": "y", "4": "g", "5": "b", "6": "p"}  # , "w"}
        self.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}

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

    def execute_main_loop(self):

        while True:
            if self.overlay.run_mode == 'paused':
                cv.waitKey(1)
                continue
            elif self.overlay.run_mode == 'stopped':
                break

            screenshot = self.capture_window.get_screenshot()
            # screenshot = cv.imread("buffbar.jpg")

            # cv.imshow("screenshot", screenshot)
            # cv.waitKey(150)
            # continue

            spawn_1 = self.vision_stun.find(screenshot, cv.imread("magic/spawn_1.jpg", cv.IMREAD_UNCHANGED), 0.25, 1) #TODO fix emitter threshold
            if len(spawn_1) == 1:
                points = self.vision_stun.get_click_points(spawn_1)
                for i in range(0, self.SPAWN_COUNT, 1):
                    pydirectinput.moveTo(points[0][0], points[0][1])
                    pydirectinput.mouseDown()
                    w = random.randint(1, 50)
                    cv.waitKey(30 + w)
                    pydirectinput.mouseUp()
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break
            else:
                spawn_0 = self.vision_stun.find(screenshot, cv.imread("magic/spawn_0.jpg", cv.IMREAD_UNCHANGED), 0.7, 1)
                points = self.vision_stun.get_click_points(spawn_0)
                for point in points:
                    pydirectinput.moveTo(point[0], point[1])
                    pydirectinput.mouseDown()
                    cv.waitKey(500)
                    pydirectinput.mouseUp()
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break
                continue
            if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                break
            # for needles in needle_list:
            for rer in range(0, len(self.needle_list), 1):
                while True:
                    screenshot = self.capture_window.get_screenshot()
                    processed_screenshot = self.vision_stun.apply_hsv_filter(screenshot, self.hsv_list[rer])
                    processed_needle = self.vision_stun.apply_hsv_filter(self.needle_list[rer], self.hsv_list[rer])

                    rectangles = self.vision_stun.find(processed_screenshot, processed_needle, 0.7, 2)
                    # draw the detection results onto the original image

                    # only trigger ocr reading if a stun is detected
                    points = self.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 self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                            break
                    else:
                        break
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break

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

            for needle_w in needles_white:
                # do object detection
                screenshot = self.capture_window.get_screenshot()
                processed_screenshot = self.vision_stun.apply_hsv_filter(screenshot, self.hsv_filter_w)
                processed_needle = self.vision_stun.apply_hsv_filter(needle_w, self.hsv_filter_w)
                rectangles = self.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 = self.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 = self.capture_window.get_screenshot()
                    rectangles = self.vision_stun.find(screenshot, cv.imread("magic/collect.jpg", cv.IMREAD_UNCHANGED),
                                                       0.8,
                                                       1)
                    points = self.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 self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
