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

if __name__ == '__main__':
    FILE_PATH = os.path.dirname(os.path.abspath(__file__))

    scanner = Scanner(os.path.join(FILE_PATH, "../", "scripts/images/2018-05-10-123203.jpg"),
                      threshold="mean",
                      output_size=300,
                      output_border=30)

    print(scanner.image_processor.patterns.get("mean").find("position").centers)

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

        plt.subplot(1, 4, 1)
        plt.imshow(scanner.image[:, :, ::-1])

        plt.title("Original Image")

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

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

        processor.sample(correct_image=True)

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

        plt.subplot(1, 4, 4)
        plt.title("Binary matrix extracted Image.")
        plt.imshow(1 - processor.matrix, interpolation="none", cmap="gray")

        data = processor.decode()
        print(data)

        plt.show()

    plt.imshow(scanner.strip_scan())
    plt.imshow(strip_back(scanner.image))

    lines = ColorLines(scanner.strip_slice())
    lines.mean()
    lines.solve()
    plt.show()


