from __future__ import annotations

import cv2
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import (
    QGraphicsPixmapItem,
    QGraphicsScene,
    QGraphicsView,
    QGridLayout,
    QPushButton,
    QWidget,
)

from module_qc_nonelec_gui.qc_tests.VISUAL_INSPECTION.functions.cv2_func import (
    img_cvt_rgb,
    read_img,
    write_img,
)


class SplitImageWindow(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.parent = parent

        layout = QGridLayout()

        self.view = QGraphicsView()
        self.scene = QGraphicsScene()
        self.srect = self.view.rect()
        self.view.setScene(self.scene)

        self.parent.update_img(self.parent.img_bgr)
        self.img_bgr_line, h, w, _d = read_img(
            f"{self.parent.temp_dir_path}/img_{self.parent.mode}_{self.parent.rev}.jpg"
        )

        nsplit = self.parent.config.get("nsplit", 6)
        grid_size_w = int(w / nsplit)
        grid_size_h = int(h / nsplit)

        for i in range(nsplit - 1):
            # draw vertical line
            self.img_bgr_line = cv2.line(
                self.img_bgr_line,
                (grid_size_w * (i + 1), 0),
                (grid_size_w * (i + 1), h),
                (0, 255, 128),
                10,
            )
            # draw horizontal line
            self.img_bgr_line = cv2.line(
                self.img_bgr_line,
                (0, grid_size_h * (i + 1)),
                (w, grid_size_h * (i + 1)),
                (0, 255, 128),
                10,
            )

        path_line = f"{self.parent.temp_dir_path}/split_{self.parent.mode}_img.jpg"
        write_img(self.img_bgr_line, path_line)
        img_bgr_line, h_line, w_line, _d_line = read_img(path_line)
        scale = min(600.0 / float(h_line), 600.0 / float(w_line))
        resize_img = cv2.resize(
            img_bgr_line,
            dsize=None,
            fx=scale,
            fy=scale,
            interpolation=cv2.INTER_LANCZOS4,
        )
        img_rgb, h_rgb, w_rgb, d_rgb = img_cvt_rgb(resize_img)
        bytesPerLine = d_rgb * w_rgb

        # # for test
        # self.parent.update_img(img_bgr_line)

        self.image = QImage(
            img_rgb.data, w_rgb, h_rgb, bytesPerLine, QImage.Format_RGB888
        )
        self.item = QGraphicsPixmapItem(QPixmap.fromImage(self.image))
        self.scene.addItem(self.item)
        self.view.setScene(self.scene)
        self.view.setFixedWidth(600)
        self.view.setFixedHeight(600)
        layout.addWidget(self.view, 0, 0)

        button_start = QPushButton("Start Inspection")
        button_start.clicked.connect(self.start)

        layout.addWidget(button_start, 1, 0)
        self.setLayout(layout)

    def start(self):
        self.close()
        self.parent.inspection()
