import os

import matplotlib.pyplot as plt

from qrdecode.main import QRProcessor
from qrdecode.utils.biostrip import ColorLines, Biostrip
from qrdecode.utils.image import ImageProcessor

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

    biostrip = Biostrip(os.path.join(FILE_PATH, "../", "scripts/images/coloredtest128.png"),
                        threshold="mean",
                        output_size=300,
                        output_border=30)

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

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

        plt.subplot(1, 4, 1)
        plt.imshow(biostrip.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=biostrip.output_border,
                                code_offset=biostrip.output_offset,
                                code_size=biostrip.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()

#lines = colorLines(biostrip.slice)
#lines.mean()
#lines.solve()
# plt.show()
