import cv2 as cv
from utils import dig_point, check_for_ok_button
from game_base_class import GameBase
from window_capture import WindowCapture


class Mine(GameBase):

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

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

        self.level = 1

        self.width = 32
        self.depth = 24
        self.plot_size = 45
        self.DIG_TIME = 100

        self.run_target = int(overlay.energy_use.get())
        self.run_counter = 0

    def reset(self):
        self.level = 1
        self.plot_size = 45
        self.DIG_TIME = 100

    def execute_main_loop(self):
        if self.run_target == 1:
            self.execute_loop()
        elif self.run_target == 2:
            self.execute_loop()
            if self.overlay.run_mode == 'stopped':
                return
            dig_point(290, 100, 200)
            self.reset()
            self.execute_loop()

    def execute_loop(self):
        while True:
            if self.overlay.run_mode == 'paused':
                cv.waitKey(10)
                self.overlay.show_mining_overlay()
                continue
            elif self.overlay.run_mode == 'stopped':
                break

            self.overlay.hide_mining_overlay()

            if self.level >= 28:
                break
            if self.level == 23:
                self.DIG_TIME = 800
            if self.level == 26:
                self.DIG_TIME = 1800
            if self.level <= 10:
                self.plot_size = self.plot_size - 1
            elif self.level % 2 == 0:
                self.plot_size = self.plot_size - 1

            p1 = int((self.config.returnDiggingWindowPos2()[0] / 2) + self.config.returnDiggingWindowPos2()[2])
            p2 = int((self.config.returnDiggingWindowPos2()[1] / 2) + self.config.returnDiggingWindowPos2()[3])

            # start 705 , 564 -> click
            # move L D -> click
            # move U , U , R, R , D , D , L , L
            # move L D
            # move U , U , U , U , R, R , R, R, D, D, D, D, L, L, L , L
            dig_point(p1, p2, self.DIG_TIME)
            # cv.circle(screenshot, (p1, p2), 7, (0, 255, 0), -1)
            i = 0
            for e in range(2, self.width, 2):
                i = i + 1

                tp1 = p1 - (i * self.plot_size)
                tp2 = p2 + (i * self.plot_size)
                for f in range(0, e, 1):
                    # up click
                    tp2 = tp2 - self.plot_size
                    if tp2 >= self.config.returnDiggingWindowPos2()[1] + (self.plot_size / 2):
                        continue
                    if tp2 <= self.config.returnDiggingWindowPos2()[3] + (self.plot_size / 2):
                        continue
                    '''
                    x = int(tp1 - (plot_size / 2))
                    y = int(tp2 - ((e * plot_size) - (plot_size / 2)))
                    w = plot_size
                    h = plot_size * e
    
                    capture_win_lanes = WindowCapture(None, "lanes", None, (w, h, x+w, y+h))
                    screenshot = capture_win_lanes.get_screenshot()
                    needlew = cv.imread("test1.jpg", cv.IMREAD_UNCHANGED)
                    res = vision_stun.find(needlew, screenshot, 0.9, 1)
                    if len(res) >= 1:
                        break
                    #vision_stun.draw_rectangles(needlew, res)
                    #cv.imshow("screenshot", needlew)
                    #cv.waitKey(150)
    
    
                    # ul[0], ul[1], plot_size, plotsize*e
    
                    # ll -> tp1 - plot/2
                    #    -> tp2 + plot/2
                    # ul -> tp1 - plot/2
                    #    -> tp2 - e*plot - plot/2
                    # lr -> tp1 + plot/2
                    #    -> tp2 - plot/2
                    # ur -> tp1 + plot/2
                    #    -> tp2 - e*plot - plot/2
                    '''
                    dig_point(tp1, tp2, self.DIG_TIME)
                    # cv.circle(screenshot, (tp1, tp2), 7, (255, 0, 255), -1)
                    # cv.imshow("screenshot", screenshot)
                    # cv.waitKey(150)
                    check_for_ok_button(self.capture_window, self.vision_stun, self.config)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                tp1 = p1 - (i * self.plot_size)
                tp2 = p2 - (i * self.plot_size)
                for g in range(0, e, 1):
                    # right click
                    tp1 = tp1 + self.plot_size
                    if tp2 <= self.config.returnDiggingWindowPos2()[3] + (self.plot_size / 2):
                        continue

                    # ll -> tp1 - plot/2
                    #    -> tp2 + plot/2
                    # ul -> tp1 - plot/2
                    #    -> tp2 - plot/2
                    # lr -> tp1 + e*plot + plot/2
                    #    -> tp2 + plot/2
                    # ur -> tp1 + e*plot + plot/2
                    #    -> tp2 - plot/2

                    dig_point(tp1, tp2, self.DIG_TIME)
                    # cv.circle(screenshot, (tp1, tp2), 7, (127, 0, 255), -1)
                    # cv.imshow("screenshot", screenshot)
                    # cv.waitKey(150)
                    check_for_ok_button(self.capture_window, self.vision_stun, self.config)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                tp1 = p1 + (i * self.plot_size)
                tp2 = p2 - (i * self.plot_size)
                for r in range(0, e, 1):
                    # down click
                    tp2 = tp2 + self.plot_size
                    if tp2 >= self.config.returnDiggingWindowPos2()[1] + (self.plot_size / 2):
                        continue
                    if tp2 <= self.config.returnDiggingWindowPos2()[3] + (self.plot_size / 2):
                        continue
                    dig_point(tp1, tp2, self.DIG_TIME)
                    # cv.circle(screenshot, (tp1, tp2), 7, (255, 0, 127), -1)
                    # cv.imshow("screenshot", screenshot)
                    # cv.waitKey(150)
                    check_for_ok_button(self.capture_window, self.vision_stun, self.config)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                tp1 = p1 + (i * self.plot_size)
                tp2 = p2 + (i * self.plot_size)
                for u in range(0, e, 1):
                    # left click
                    tp1 = tp1 - self.plot_size
                    if tp2 >= self.config.returnDiggingWindowPos2()[1] + (self.plot_size / 2):
                        continue
                    dig_point(tp1, tp2, self.DIG_TIME)
                    # cv.circle(screenshot, (tp1, tp2), 7, (0, 0, 255), -1)
                    # cv.imshow("screenshot", screenshot)
                    # cv.waitKey(150)
                    check_for_ok_button(self.capture_window, self.vision_stun, self.config)
                    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
            self.level = self.level + 1
