import os
import matplotlib.pyplot as plt
from qrdecode.main import Scanner, QRProcessor
from qrdecode.utils.image import ImageProcessor

if __name__ == '__main__':
    FILE_PATH = os.path.dirname(os.path.abspath(__file__))
    scanner = Scanner(os.path.join(FILE_PATH, "../", "scripts/images/static_qr_code_without_logo.jpg"),
                      threshold="gaussian",
                      output_size=1000,
                      output_border=50)

    for code in scanner.scan():
        assert isinstance(code, ImageProcessor)

        plt.figure()

        plt.subplot(2, 3, 1)
        plt.imshow(scanner.image[:, :, ::-1])
        plt.title("Original Image")

        plt.subplot(2, 3, 2)
        plt.title("Position Trimmed Image")
        plt.imshow(code.image[:, :, ::-1].copy())

        processor = QRProcessor(code,
                                code_border=scanner.output_border,
                                code_offset=scanner.output_offset,
                                code_size=scanner.output_size,
                                threshold="simple"
                                )

        plt.subplot(2, 3, 3)
        plt.title("Threshold image for analysis")
        plt.imshow(processor.image_processor.threshold(processor.threshold).copy(),
                   interpolation="none",
                   cmap="gray")

        # processor.correct_alignment()
        # Or alternatively:
        processor.correct_fourth_corner()

        processor.sample()

        plt.subplot(2, 3, 4)
        plt.title("Alignment corrected Image")
        plt.imshow(processor.image[:, :, ::-1])

        plt.subplot(2, 3, 5)
        plt.title("Color matrix extracted Image")
        plt.imshow(processor.colors[:, :, ::-1], interpolation="none")

        plt.subplot(2, 3, 6)
        plt.title("Binary matrix extracted Image.")
        plt.imshow(processor.matrix, interpolation="none", cmap="gray")

        data = processor.decode()

        plt.show()
