from bpy import data, types
from .. import constants, logger
from .constants import MULTIPLY, WIRE, IMAGE


def _material(func):
    """

    :param func:

    """

    def inner(name, *args, **kwargs):
        """

        :param name:
        :param *args:
        :param **kwargs:

        """

        if isinstance(name, types.Material):
            material = name
        else:
            material = data.materials[name]

        return func(material, *args, **kwargs)

    return inner


@_material
def ambient_color(material):
    """

    :param material:
    :return: rgb value
    :rtype: tuple

    """
    logger.debug("material.ambient_color(%s)", material)
    diffuse = diffuse_color(material)
    return (material.ambient * diffuse[0],
            material.ambient * diffuse[1],
            material.ambient * diffuse[2])


@_material
def blending(material):
    """

    :param material:
    :return: THREE_blending_type value

    """
    logger.debug("material.blending(%s)", material)
    try:
        blend = material.THREE_blending_type
    except AttributeError:
        logger.debug("No THREE_blending_type attribute found")
        blend = constants.NORMAL_BLENDING
    return blend


@_material
def bump_map(material):
    """

    :param material:
    :return: texture node for bump

    """
    logger.debug("material.bump_map(%s)", material)
    for texture in _valid_textures(material):
        if texture.use_map_normal and not \
           texture.texture.use_normal_map:
            return texture.texture


@_material
def bump_scale(material):
    """

    :param material:
    :rtype: float

    """
    return normal_scale(material)


@_material
def depth_test(material):
    """

    :param material:
    :return: THREE_depth_test value