import logging


def docker_obj_status(name, obj_type, docker_helper, logger=None):
    """
    Determine if a specific docker object like an image, container, or network
    exists, and if it is "owned" by the current project

    Args:
        name: str or list, of the name(s) of the object
        obj_type: str, one of 'network', 'container', 'image', or 'image_bare'
            NOTE:
                'image' includes the tag in the image name. Like: 'stuff:1.0'
                'image_bare' is the image name without the tag.
        docker_helper: DockerHelper, to use for querying docker objects
        logger: Logger, for to use for logging
    Return:
        list of dicts.
        dict structure:
            {
                'name': str, of the object name
                'exists': bool, whether and object with 'name' exists.
                'owned': bool, whether the object is owned by the specific project
            }
    """
    if logger:
        log = logger
    else:
        log = logging.getLogger('docker_obj_status')
    result = list()
    res_tmpl = {
        'name': None,
        'exists': False,
        'owned': False
    }
    owned_obj = list()
    all_obj = list()
    if obj_type == 'network':
        owned_networks = docker_helper.get_networks()[1]
        owned_obj = [net['name'] for net in owned_networks]
        all_networks = docker_helper.get_networks(return_all=True)[1]
        all_obj = [net['name'] for net in all_networks]
    elif obj_type == 'container':
        owned_containers = docker_helper.get_containers()[1]
        owned_obj = [cont['name'] for cont in owned_containers]
        all_containers = docker_helper.get_containers(return_all=True)[1]
        all_obj = [cont['name'] for cont in all_containers]
    elif obj_type == 'image':
        owned_obj = docker_helper.get_images()[1]
        all_obj = docker_helper.get_images(return_all=True)[1]
    elif obj_type == 'image_bare':
        owned_images = docker_helper.get_images()[1]
        owned_obj = [image.split(':')[0] for image in owned_images]
        all_images = docker_helper.get_images(return_all=True)[1]
        all_obj = [image.split(':')[0] for image in all_images]
    else:
        log.warning("Unknown docker object type: '%s'", obj_type)
    if isinstance(name, list):
        for nme in name:
            res = dict(res_tmpl)
            res['name'] = nme
            if nme in owned_obj:
                res['exists'] = True
                res['owned'] = True
            elif nme in all_obj:
                res['exists'] = True
            result.append(res)
    else:
        res = dict(res_tmpl)
        res['name'] = name
        if name in owned_obj:
            res['exists'] = True
            res['owned'] = True
        elif name in all_obj:
            res['exists'] = True
        result.append(res)
    return result