import cv2 as cv
from utils import get_click_point, dig_point, check_for_craft_ok_button, check_for_craft_button
import pydirectinput
from config_file import UserConfigs
from window_capture import WindowCapture
from vision import Vision
from game_base_class import GameBase

MODE25X1 = "m25x1"
MODE10X2 = "m10x2"
MODE5X3 = "m5x3"
MODE_SIMPLE_STACK = "simple"
MODE1X5_T = "m1x5_T"
MODE1X5_X = "m1x5_X"
MODE_HOUSE = "house"


class Craft(GameBase):

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

        self.mode = str(overlay.emitter_use.get())
        self.run_target = int(overlay.energy_use.get())

        self.run_counter = 0
        self.dimension = 172

    def execute_main_loop(self):

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

            if self.run_counter >= self.run_target:
                self.run_counter = 0
                self.overlay.run_mode = 'finished'
                break

            ab1 = [420, 1180, 142, 142]
            ab2 = [562, 1180, 142, 142]
            ab3 = [704, 1180, 142, 142]
            ab4 = [846, 1180, 142, 142]
            ab5 = [986, 1180, 142, 142]
            ab6 = [1128, 1180, 142, 142]
            ab10 = [1698, 1180, 142, 142]

            if self.mode == MODE25X1:
                self.click_square_center(ab1)

                # grid 770 1630  160 1020   172 o
                self.dimension = 172
                for i in range(0, 5, 1):
                    for e in range(0, 5, 1):
                        self.click_square_center(
                            [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension, self.dimension])
                        if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                            break

                self.click_craft_button()

                for i in range(0, 5, 1):
                    for e in range(0, 5, 1):
                        self.collect_grid_reverse_click(
                            [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension, self.dimension])
                        self.run_counter = self.run_counter + 1
                        self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))
                        if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                            break
                self.click_square_center(ab10)
            elif self.mode == MODE10X2:
                for r in range(0, 2, 1):
                    # grid 770 1630  160 1020   172

                    if r == 0:
                        self.click_square_center(ab1)
                        start = 0
                        target = 3
                    else:
                        self.click_square_center(ab2)
                        start = 1
                        target = 4

                    for i in range(start, target, 2):
                        for e in range(0, 5, 1):
                            self.click_square_center(
                                [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension,
                                 self.dimension])
                            if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                                break

                self.click_craft_button()

                for i in range(0, 4, 2):
                    for e in range(0, 5, 1):
                        self.collect_grid_reverse_click(
                            [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension, self.dimension])
                        self.run_counter = self.run_counter + 1
                        self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))
                        if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                            break
                self.click_square_center(ab10)
            elif self.mode == MODE5X3:
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
                for r in range(0, 3, 1):
                    # grid 770 1630  160 1020   172oo
                    self.dimension = 172
                    if r == 0:
                        self.click_square_center(ab1)
                        start = 0
                        target = 1
                    elif r == 1:
                        self.click_square_center(ab2)
                        start = 1
                        target = 2
                    else:
                        self.click_square_center(ab3)
                        start = 2
                        target = 3

                    for i in range(start, target, 1):
                        for e in range(0, 5, 1):
                            self.click_square_center(
                                [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension,
                                 self.dimension])
                            if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                                break

                self.click_craft_button()

                for i in range(0, 1, 1):
                    if self.overlay.run_mode == 'stopped':
                        break
                    for e in range(0, 5, 1):
                        self.collect_grid_reverse_click(
                            [770 + (i * self.dimension), 160 + (e * self.dimension), self.dimension, self.dimension])
                        self.run_counter = self.run_counter + 1
                        self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))
                        if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                            break
                self.click_square_center(ab10)
            elif self.mode == MODE1X5_T:
                tps = [ab1,
                       [770 + (0 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                       [770 + (1 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                       ab2,
                       [770 + (2 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                       [770 + (2 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                       [770 + (2 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension]]

                result_tp = [770 + (0 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension]

                for tp in tps:
                    self.click_square_center(tp)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_craft_button()
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
                self.click_square_center(result_tp)
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
                self.click_square_center(ab10)

                self.run_counter = self.run_counter + 1
                self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))

                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
            elif self.mode == MODE1X5_X:
                tps = [ab1,
                       [770 + (0 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                       [770 + (1 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                       [770 + (2 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                       [770 + (1 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension],
                       [770 + (1 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension]]

                result_tp = [770 + (0 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension]

                for tp in tps:
                    self.click_square_center(tp)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_craft_button()
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
                self.click_square_center(result_tp)
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
                self.click_square_center(ab10)

                self.run_counter = self.run_counter + 1
                self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))

                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
            elif self.mode == MODE_HOUSE:
                cements = [[770 + (0 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension],
                           [770 + (1 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension],
                           [770 + (2 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension],
                           [770 + (3 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension],
                           [770 + (4 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension]]

                bricks = [[770 + (0 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                          [770 + (1 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                          [770 + (3 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                          [770 + (4 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                          [770 + (0 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                          [770 + (2 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                          [770 + (4 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension]]

                boards = [[770 + (2 * self.dimension), 160 + (3 * self.dimension), self.dimension, self.dimension],
                          [770 + (0 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                          [770 + (1 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                          [770 + (2 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                          [770 + (3 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                          [770 + (4 * self.dimension), 160 + (1 * self.dimension), self.dimension, self.dimension],
                          [770 + (2 * self.dimension), 160 + (0 * self.dimension), self.dimension, self.dimension]]

                glasses = [[770 + (1 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension],
                           [770 + (3 * self.dimension), 160 + (2 * self.dimension), self.dimension, self.dimension]]

                tp_hammer = [770 + (1 * self.dimension), 160 + (0 * self.dimension), self.dimension, self.dimension]
                tp_nails = [770 + (3 * self.dimension), 160 + (0 * self.dimension), self.dimension, self.dimension]
                tp_result = [770 + (0 * self.dimension), 160 + (4 * self.dimension), self.dimension, self.dimension]

                self.click_square_center(ab1)

                for cement in cements:
                    self.click_square_center(cement)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_square_center(ab2)

                for brick in bricks:
                    self.click_square_center(brick)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_square_center(ab3)

                for board in boards:
                    self.click_square_center(board)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_square_center(ab4)

                for glass in glasses:
                    self.click_square_center(glass)
                    if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                        break

                self.click_square_center(ab5)
                self.click_square_center(tp_hammer)
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break

                self.click_square_center(ab6)
                self.click_square_center(tp_nails)
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break

                self.click_craft_button()
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break

                self.click_square_center(tp_result)
                self.click_square_center(ab10)
                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break

                self.run_counter = self.run_counter + 1
                self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))

                if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
                    break
            elif self.mode == MODE_SIMPLE_STACK:
                # initialize the StunWindowCapture class
                capture_config = UserConfigs()
                # capture_window = WindowCapture(None, "screen_conf", capture_config)

                capture_window2 = WindowCapture(None, "bla", capture_config)

                # initialize the StunVision class
                vision_stun = Vision()
                # dig_overlay = DiggingOverlay(config)
                check_for_craft_button(capture_window2, vision_stun)
                check_for_craft_ok_button(capture_window2, vision_stun)
                self.run_counter = self.run_counter + 1
                self.overlay.update_status_label(self.mode + ": " + str(self.run_target - self.run_counter))
                # check_for_ok_button(capture_window, vision_stun, capture_config)

    def collect_grid_reverse_click(self, square):
        pydirectinput.moveTo(get_click_point(square)[0], get_click_point(square)[1])
        cv.waitKey(50)
        pydirectinput.mouseUp()
        cv.waitKey(50)
        pydirectinput.mouseDown()
        cv.waitKey(100)

    def click_craft_button(self):
        dig_point(1910, 330, 100)
        cv.waitKey(1500)

    def click_square_center(self, square):
        cp = get_click_point(square)
        dig_point(cp[0], cp[1], 100)
        cv.waitKey(100)
