#!/usr/bin/env python
import barcode
from barcode.writer import ImageWriter
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Cm
from egcg_core.config import cfg

from EPPs.common import SendMailEPP


class GenerateTrackingLetter(SendMailEPP):
    '''Automatically generates the tracking letter sent to customers for tube shipments by populating a word template
    with a 128 format barcode containing the container name'''

    _max_nb_project = 1


    # additional argument required to obtain the file location for newly created tracking letter in the LIMS step
    def __init__(self, argv=None):
        super().__init__(argv)
        self.letter = self.cmd_args.letter

    @staticmethod
    def add_args(argparser):
        argparser.add_argument(
            '-t', '--letter', type=str, required=True, help='Tracking letter generated by the LIMS'
        )

    def _run(self):

        # obtain all of the inputs for the step
        all_inputs = self.artifacts

        # 96 well plate so don't need tracking letter
        if all_inputs[0].container.type.name == "96 well plate":
            return 0

        EAN = barcode.get_barcode_class('code128')

        ean = EAN(all_inputs[0].container.name, writer=ImageWriter())
        save_options = {'font_size': 20,
                        'text_distance': 2,
                        'module_height': 15,
                        'module_width': 0.3}
        ean.save('code128', options=save_options)

        document = Document(cfg.query('file_templates', 'tracking_letter'))

        for paragraph in document.paragraphs:
            if 'The barcode(s) above provides confirmation' in paragraph.text:
                p = paragraph.insert_paragraph_before('')
                p = p.insert_paragraph_before('')
                p.alignment = WD_ALIGN_PARAGRAPH.CENTER
                r = p.add_run()
                r.add_picture('code128.png', width=Cm(5))

        document.save(self.letter + '-Edinburgh_Genomics_Sample_Tracking_Letter_' + self.projects[0].name + '.docx')


if __name__ == '__main__':
    GenerateTrackingLetter().run()
