import cv2

from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.image import cropimg, thres2
from mower.utils.log import logger
from mower.utils.scene import Scene

from .utils import last_letters


class LastStageNavigation(SceneGraphSolver):
    def run(self, name: str, mode: str) -> None:
        self.name = name
        self.mode = mode
        self.success = True
        if self.name != "":
            self.scene_graph_navigation(Scene.TERMINAL_MAIN)
        super().run()
        return self.success

    def last_stage(self) -> str:
        img = cropimg(config.recog.gray, ((1690, 845), (1870, 890)))
        dark_img = cv2.bitwise_not(img)
        dark_img = thres2(dark_img, 127)

        contours, _ = cv2.findContours(
            dark_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
        )
        rect = [cv2.boundingRect(c) for c in contours]
        rect.sort(key=lambda c: c[0])

        value = ""
        for x, y, w, h in rect:
            if h < 7:
                if w < 7:
                    continue
                value += "-"
                continue
            digit = cropimg(img, ((x, y), (x + w, y + h)))
            digit = cv2.copyMakeBorder(digit, 10, 10, 10, 10, cv2.BORDER_REPLICATE)
            score = {}
            for letter, letter_img in last_letters.items():
                dh, dw = digit.shape
                lh, lw = letter_img.shape
                if dh < lh or dw < lw:
                    return ""
                result = cv2.matchTemplate(digit, letter_img, cv2.TM_SQDIFF_NORMED)
                min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
                score[letter] = min_val
            value += min(score, key=score.get)
        logger.debug(value)
        return value

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.TERMINAL_MAIN:
            if self.name in ["", self.last_stage()]:
                self.tap((1670, 880))
            else:
                self.success = False
                return True
        elif scene == Scene.OPERATOR_BEFORE:
            if self.mode == "auto":
                return True
            else:
                if config.recog.gray[65][1333] < 200:
                    self.sleep()
                    return
                # 取消代理作战
                if config.recog.gray[907][1600] > 127 and not self.find(
                    "ope_agency_lock"
                ):
                    self.tap((1776, 908))
                    return
                self.tap_element("ope_start", interval=2)
        elif scene == Scene.OPERATOR_SELECT and self.mode == "copy":
            return True

        else:
            self.scene_graph_step(Scene.TERMINAL_MAIN)
