"""
 masker functions for each mode to fill img subclass of the main Vivus object
"""

import logging
import numpy as np

from PIL import Image

from .imgproc import *
from .loader  import *


mlg = logging.getLogger(__name__)


def getGraySmask():
    """
    helper function to load grayscale image and
    scalemask image based on input.png
    """

    # grayscale image
    gray = np.array(Image.open('input.png').convert('L'))

    # scalemask from gray image
    smask = getScalemask(gray)

    return gray, smask

def getGrayclnPolar(gray, smask):
    """
    helper function that cleans input grayscale image
    and returns versions in cartesian and polar coordinates
    """

    # cleaned grayscale image with masks for the probe and scales
    graycln = getCleanImage(gray, smask)

    # polar transformation
    polar   = getPolar(graycln)

    return graycln, polar



def fullMasker():
    """
    masker() for mode 0, uses hollow type weights to use for both contours
    note: graycln version of the input is not computed here
    """

    # input image processing
    gray, smask = getGraySmask()

    # grayscale of the mask image
    img  = getGrayNNmask('hollow')
    graymask = np.array(img)

    # polar transformation
    polar = getPolar(graymask)

    # same enhanced polar image mask for both boundaries
    ienh  = getEnhancement(polar, p.threshMaxInn, p.threshMinInn)
    oenh  = np.array(ienh)


    mlg.info(' fullMasker: done')
    return ProcImages(gray, smask, None, polar, oenh, ienh)



def hybridMasker():
    """
    masker() for mode 1, uses filled type weights to use only for inner contour
    outer contour is treated in the same way as mode=2 (deterministically)
    """

    # input image processing
    gray, smask = getGraySmask()

    # grayscale mask image
    img  = getGrayNNmask('filled')
    graymask = np.array(img)

    # polar transformation
    polar = getPolar(graymask)

    # enhanced polar image mask for inner boundary
    # note that outer thresholds are used here!
    ienh    = getEnhancement(polar, p.threshMaxOut, p.threshMinOut)

    # cleaned grayscale image with masks for the probe and scales
    graycln, polar = getGrayclnPolar(gray, smask)

    # enhanced polar image mask for outer boundary
    oenh    = getEnhancement(polar, p.threshMaxOut, p.threshMinOut)

    mlg.info(' hybridMasker: done')
    return ProcImages(gray, smask, graycln, polar, oenh, ienh)



def simpleMasker():
    """
    masker() for mode 2, no weights used, purely deterministic approach
    """

    # input image processing
    gray, smask = getGraySmask()

    # cleaned grayscale image with masks for the probe and scales
    graycln, polar = getGrayclnPolar(gray, smask)

    # enhanced polar image mask for outer boundary
    oenh    = getEnhancement(polar, p.threshMaxOut, p.threshMinOut)

    # enhanced polar image mask for iner boundary
    ienh    = getEnhancement(polar, p.threshMaxInn, p.threshMinInn)


    mlg.info(' simpleMasker: done')
    return ProcImages(gray, smask, graycln, polar, oenh, ienh)
