# -*- coding: utf-8 -*-
"""
    Filter functions for media and user lists.
"""

from . import delay

# filtering medias


def filter_medias(self, media_items, filtration=True, quiet=False):
    if filtration:
        if not quiet:
            self.logger.info("Received %d medias." % len(media_items))
        media_items = _filter_medias_not_liked(media_items)
        if self.max_likes_to_like:
            media_items = _filter_medias_nlikes(
                media_items, self.max_likes_to_like)
        if not quiet:
            self.logger.info("After filtration %d medias left." % len(media_items))
    return _get_media_ids(media_items)


def _filter_medias_not_liked(media_items):
    not_liked_medias = []
    for media in media_items:
        if 'has_liked' in media.keys():
            if not media['has_liked']:
                not_liked_medias.append(media)
    return not_liked_medias


def _filter_medias_nlikes(media_items, max_likes_to_like):
    filtered_medias = []
    for media in media_items:
        if 'like_count' in media.keys():
            if media['like_count'] < max_likes_to_like:
                filtered_medias.append(media)
    return filtered_medias


def _get_media_ids(media_items):
    result = []
    for m in media_items:
        if 'pk' in m.keys():
            result.append(m['pk'])
    return result


def check_media(self, media_id):
    self.mediaInfo(media_id)
    if len(self.filter_medias(self.LastJson["items"])):
        return check_user(self, self.get_media_owner(media_id))
    else:
        return False

# filter users


def stop_words_found(self, user_info):
    text = ''
    if 'biography' in user_info:
        text += user_info['biography'].lower()

    if 'username' in user_info: