import shared    # For shared variables
import imageTools as iT
import imageManupilation as iM
# from ocr import imageToText
from circleTools import detectCircles, cropDetected
from tools import stdo  # , getTime    # For properly print out
# import os   # For temp files
# from inspect import currentframe, getframeinfo

##########################
# Preprocess and Configs #
##########################
"""

    Preprocesses and Parameters
        "crop" : 3,   # such as [10, 10, 256]
        "resize" : 2,   # such as [256, 256]
        "grayscale" : 0,   # such as []
        "invertColor" : 0,   # such as []
        "bilateralBlur" : 3,   # such as [11, 52, 34]
        "medianBlur" : 1,   # such as [3]
        "cannyEdgeDetection" : 2,   # such as []
        "threshold" : 2,   # such as []
        "findContours" : 0   # such as []

        --- Structure ---

        paramsCount = {
            "crop" : 3,
            "resize" : 2,
            "expand" : 3,
            "grayscale" : 0,
            "invertColor" : 0,
            "bilateralBlur" : 3,
            "medianBlur" : 1,
            "gaussianBlur" : 3,
            "cannyEdgeDetection" : 2,
            "threshold" : 2,
            "findContours" : 0,
            "labelConnected" : 0,
            "deskew" : 2
        }

"""

################################################
# Snippets of Preprocessses and Configurations #
################################################
"""

configs = []
preprocess = [ "" ]

configs = [256, 256, 11, 52, 34]    # First 2 params for resize, and 3 params for bilateralBlur and lasts for findContours
preprocess = ["resize", "bilateralBlur", "findContours"]

configs = [256, 256, 3, -1, -1]    # First 2 params for resize, 1 param for medianBlur, and 2 params for threshold ( -1, -1 for specific threshold)
preprocess = [ "resize", "medianBlur", "threshold" ]

configs = [512, 512, 3, -1, -1]    # First 2 params for resize, 1 param for medianBlur, and 2 params for threshold ( -1, -1 for specific threshold)
preprocess = [ "resize", "medianBlur", "threshold", "invertColor"]

configs = [256, 256, 1, 150, 250]    # First 2 params for resize, 1 param for medianBlur, and 2 params for threshold ( -1, -1 for specific threshold)
preprocess = [ "resize", "medianBlur", "threshold" ]

"""


def run(img, args = ""):
    # Commented because of textRecognition needs rgb (3 channel) image
    img = iM.manupilate( img, ["grayscale"], [] )    # For removing unneccesary colors

    #####################################
    # Detect Circle and Apply OCR Test #
    #####################################
    """
    configs = [3]
    preprocess = [ "medianBlur" ]

    mImg = iM.manupilate(img, preprocess, configs)
    # For Debug
    iT.showImage(mImg, "Manupilated Image ({})".format(str(preprocess[0])), terminal = 0 )
    """
    """
        To Developer:
            - use img variable and remove mImg
    """

    circles = detectCircles(img)
    croppedImages = cropDetected(img, circles)

    # Configuration and Manupilation options

    configsForAll = shared.args["configs"]
    preprocess = shared.args["preprocess"]

    """
    configsForAll = [5, 5, 0, -1, -1, 5, -1, -1, -1, 400, 400]
    preprocess = ["gaussianBlur", "threshold", "medianBlur", "resize", "deskew"]
    """

    """
    configsForAll = [5, 5, 0, -1, -1, 5, 400, 400]
    preprocess = ["gaussianBlur", "threshold", "medianBlur", "resize", "deskew"]

    # Expand Parameters;
    #   First   -> Background Color
    #               0 for Black
    #               1 for White
    #               2 for Grey
    #   Second  -> Lock Aspect Ratio
    #               0 for "NO Lock Aspect Ratio" and Make a square image as background with given size
    #               1 for "Lock Aspect Ratio" and Lock aspect ratio and multiplie with given number
    #   Third  -> Size calculated with Second Parameter
    configsForAll = [1, 0, 500]
    preprocess = ["expand"]

    configsForAll = [255, 255, 1, 0, 500]
    preprocess = ["resize", "expand"]

    configsForAll = [ -1, 2, 5]
    preprocess = ["threshold", "medianBlur"]

    configsForAll = [-1, -1]
    preprocess = ["threshold"]
    """

    for crpImage in croppedImages:

        configs = configsForAll.copy()
        stdo(1, "\n--- Not Manupilated ---")

        iT.infoImage(crpImage)
        # imageToText(crpImage)   # Check if OCR Process reads and string from image

        stdo(1, "\n--- Manupilated ---")
        cmImage = iM.manupilate(crpImage, preprocess, configs)

        # Save Manupilated Image as tempFile
        tempfilePath = shared.tempList["tempPath"] + iT.getTempName()
        iT.infoImage(cmImage)
        iT.saveImage(cmImage, tempfilePath)
        shared.tempImageAdd(cmImage, tempfilePath)

        # For Debug
        # iT.showImage(cmImage, "Cropped and Manupilated Image", terminal = 0)

        # Pytesseract default OCR - Not Accured-Succesfull
        # currentTempImageCounter = len(shared.tempImageList)
        # shared.tempImageList[currentTempImageCounter]["OCR"] = imageToText(cmImage)   # Check if OCR Process reads and string from image

    return 0
