from dataclasses import dataclass

from core.line_parser import LineParser
from core.parser import *
from core.direction import *
import numpy as np
from paddleocr import PaddleOCR


@dataclass
class IdCardOcr:
    ocr: PaddleOCR
    angle_detector: AngleDetector

    # 检测
    def predict(self, image: np.ndarray, image_type: str = '0'):
        # image, angle = self._pre_process(image)
        # 识别出的 => 字段、置信度、(字段,置信度)
        txts, confs, result = self._ocr(image)
        # 角度
        angle = self.angle_detector.detect_angle(image, result)

        return self._post_process(result, angle, image_type)

    def _pre_process(self, image) -> (np.ndarray, int):
        angle = detect_angle(image)
        print(angle)  # 逆时针
        if angle == 180:
            image = cv2.rotate(image, cv2.ROTATE_180)
        if angle == 90:
            image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
        if angle == 270:
            image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
        return image, angle

    def _ocr(self, image):
        # 获取模型检测结果
        result = self.ocr.ocr(image, cls=True)
        print("------------------")
        print(result)
        if not result:
            raise Exception('无法识别')
        confs = [line[1][1] for line in result]

        # 将检测到的文字放到一个列表中
        txts = [line[1][0] for line in result]
        # print("......................................")
        # print(txts)
        # print("......................................")
        return txts, confs, result

    def _post_process(self, result, angle: int, image_type: str):
        line_parser = LineParser(result)
        line_result = line_parser.parse()
        conf = line_parser.confidence

        if int(image_type) == 0:
            parser = FrontParser(line_result)
        elif int(image_type) == 1:
            parser = BackParser(line_result)
        else:
            raise Exception('无法识别')

        ocr_res = parser.parse()

        res = {
            "confidence": conf,
            "card_type": image_type,
            "orientation": angle // 90,  # 原angle是逆时针，转成顺时针
            **ocr_res
        }
        print(res)
        return res
