# -----------------------------------
# OCR Digicoche
# -----------------------------------

# Imports des librairies
try:
    import sys
    import traceback
    from PIL import Image
    from pdf2image import convert_from_path
    import pytesseract
    from pytesseract import Output
    import cv2
    import re
    from SBA import *
    from Cooperl import *
    from TSLibOCR import *
    from Holvia import *
    from Loudeac import *
    import time
    import os
    import numpy as np
    from datetime import datetime
except Exception as err:
    print(f"Unexpected {err=}, {type(err)=}")
    print("ERROR")
# -----------------------------------
# Récupération du fichier PDF passé en argument de la ligne de commande
pdfs = sys.argv[1]
try:
    outputFileName = sys.argv[2]
    ocrDirectory = sys.argv[3]
    dbName = sys.argv[4]
    sqlHost = sys.argv[5]
    if sys.argv[6] == 'True':
        debug = True
    if sys.argv[6] == 'False':
        debug = False
except Exception as err:
    outputFileName = ""

if debug != True and debug != False:
    debug = False

# On efface le terminal
#os.system('clear')
# Initialisation de l'heure de démarrage du programme
start_time = time.time()

""" Retourne le temps passé depuis le lancement du programme """
def getTime():
    result = (time.time() - start_time)
    result = round(result,3)
    return result


def printDebug(pTxt):
    if debug:
        print(pTxt)

printDebug("%s - Import du fichier PDF > Conversion en image" % getTime())

try:
    pages = convert_from_path(pdfs, 480)    # 450dpi
except Exception as err:
    print(f"Unexpected {err=}, {type(err)=}")
    print("ERROR")

i = 1
ocrReturnArray = []

"""
# On commence par détecter l'abattoir
"""
printDebug("%s - Détection de l'abattoir" % getTime())

# conversion en fichier jpeg de la premiere page
try:
    image_name = "Page_" + str(i) + ".jpg"
    pages[0].save(ocrDirectory+"tmp/" + image_name, "JPEG")
except Exception as e:
    exception_type, exception_object, exception_traceback = sys.exc_info()
    filename = exception_traceback.tb_frame.f_code.co_filename
    line_number = exception_traceback.tb_lineno

    print("Exception type: ", exception_type)
    print("File name: ", filename)
    print("Line number: ", line_number)
    self.printDebug(f"Unexpected {e=}, {type(e)=}")
    self.printDebug("ERROR")

printDebug("%s - Optimisation de l'image" % getTime())
# img = mark_region("BL/Page_"+str(i)+".jpg")
img = cv2.imread(ocrDirectory+"tmp/" + image_name)
result = img.copy()
# On lance la reconaissance OCR sur l'image
printDebug("%s - Lecture OCR" % getTime())
try:
    df = pytesseract.image_to_string(np.array(result), lang='fra',
                                     config=' --oem 0 --psm 4')
except Exception as err:
    printDebug(f"Unexpected {err=}, {type(err)=}")
    printDebug("ERROR")

cv2.imwrite(ocrDirectory+'tmp/Page_' + str(i) + '_result.jpg', result)

try:
    ocrReturnArray.append(df)
except Exception as err:
    printDebug(f"Unexpected {err=}, {type(err)=}")
    printDebug("ERROR")

printDebug("%s - Lecture OCR Terminée" % getTime())
printDebug("%s - Recherche de l'abattoir" % getTime())

"""
# Fin de détection de l'abattoir
"""
find = False
try:
# On contrôle le type d'abattoir du BL
    printDebug(ocrReturnArray[0])
    printDebug("-------------")
    #printDebug(ocrReturnArray[0])
    """ 
    # SBA
    """

    m = re.findall(r"SBA",ocrReturnArray[0],re.IGNORECASE)
    if m is not None and find == False:
        if len(m) > 0 :
            printDebug("Abattoir détecté : SBA")
            find = True
            bl = SBA(pdfs,outputFileName,ocrDirectory,dbName,sqlHost)
            bl.read()
            bl.mergeData()

    """ 
    # COOPERL
    """
    m = re.findall(r"Cooperl",ocrReturnArray[0],re.IGNORECASE)
    if m is not None and find == False:
        if len(m) > 0 :
            printDebug("Abattoir détecté : Cooperl")
            find = True
            bl = Cooperl(pdfs,outputFileName,ocrDirectory,dbName,sqlHost,debug)
            bl.read()
            bl.mergeData()

    """ 
    # HOLVIA
    """
    m = re.findall(r"SAS H[ ]?O[ ]?L[ ]?V[ ]?I[ ]?A PO[RN][CG]?",ocrReturnArray[0],re.IGNORECASE)
    if m is not None and find == False:
        if len(m) > 0 :
            printDebug("Abattoir détecté : Holvia Porc")
            find = True
            bl = Holvia(pdfs,outputFileName,ocrDirectory,dbName,sqlHost,debug)
            bl.read()
            bl.mergeData()

    """ 
    # CHARAL
    """
   # m = re.findall(r"CHARAL",ocrReturnArray[0],re.IGNORECASE)
   # if m is not None and find == False:
   #     if len(m) > 0 :
   #         printDebug("Abattoir détecté : Charal")
   #         find = True
   #         bl = Charal(pdfs,outputFileName,ocrDirectory,dbName,sqlHost)
   #         bl.motsASurligner.append("CHARAL")
   #         bl.read()
   #         bl.mergeData()

    """ 
    # LOUDEAC
    """
    m = re.findall("LO[UÜ]DEAC",ocrReturnArray[0],re.IGNORECASE)
    if m is not None and find == False:
        if len(m) > 0 :
            printDebug("Abattoir détecté : LOUDEAC VIANDES")
            find = True
            bl = Loudeac(pdfs,outputFileName,ocrDirectory,dbName,sqlHost)
            bl.motsASurligner.append("LOUDEAC")
            bl.read()
            bl.mergeData()



    if find == False:
        print('{"blNonIdentifie":1}')

except Exception as e:
    exception_type, exception_object, exception_traceback = sys.exc_info()
    filename = exception_traceback.tb_frame.f_code.co_filename
    line_number = exception_traceback.tb_lineno

    print("Exception type: ", exception_type)
    print("File name: ", filename)
    print("Line number: ", line_number)
    printDebug(f"Unexpected 2(main.py) {e=}, {type(e)=}")
    printDebug("ERROR 2")
