# -*- coding: utf-8 -*-

from django.core.serializers.json import DjangoJSONEncoder

from utils.redis.connect import r
from utils.redis.rkeys import (GROUP_INFO, GROUP_PHOTO_COMMENT_LIST, GROUP_PHOTO_DATA, GROUP_PHOTO_THUMB_UP,
                               GROUP_PHOTO_THUMB_UP_LIST, GROUP_PHOTO_WATCHER_SET, GROUP_USERS_INFO,
                               GROUP_USERS_KV_INFO)


def set_group_info(group):
    """ 设置群组信息 """
    r.setexjson(GROUP_INFO % group.group_id, r.REDIS_EXPIRED_ONE_MONTH, group.data, cls=DjangoJSONEncoder)
    return group.data


def set_group_info_by_id(group_id):
    """ 设置群组信息 """
    from group.models import GroupInfo
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return {}
    return set_group_info(group)


def get_group_info(group_id):
    """ 获取群组信息 """
    return r.getjson(GROUP_INFO % group_id) or set_group_info_by_id(group_id)


# 群组用户信息相关


def set_group_users_info(group):
    """ 设置群组用户信息 """
    group_users = group.users(admin=False)

    group_users_data = {
        'group_users': group_users,
        'admin_id': group.admin_id,
    }
    r.setexjson(GROUP_USERS_INFO % group.group_id, r.REDIS_EXPIRED_ONE_MONTH, group_users_data, cls=DjangoJSONEncoder)

    group_users_kv_data = {user_info.get('user_id', ''): user_info for user_info in group_users.get('passed', [])}
    r.setexjson(GROUP_USERS_KV_INFO % group.group_id, r.REDIS_EXPIRED_ONE_MONTH, group_users_kv_data, cls=DjangoJSONEncoder)

    return group_users_data, group_users_kv_data


def set_group_users_info_by_id(group_id):
    """ 设置群组用户信息 """
    from group.models import GroupInfo
    try:
        group = GroupInfo.objects.get(group_id=group_id)
    except GroupInfo.DoesNotExist:
        return {}, {}
    return set_group_users_info(group)


def get_group_users_info(group_id, user_id):
    """ 获取群组用户信息 """
    group_users_data = r.getjson(GROUP_USERS_INFO % group_id) or set_group_users_info_by_id(group_id)[0]
    group_users, admin_id = group_users_data.get('group_users', {}), group_users_data.get('admin_id', '')
    if group_users and user_id != admin_id:
        [group_users.pop(k) for k in ['applying_count', 'applying']]
    return group_users


def get_group_users_kv_info(group_id):
    """ 获取群组用户信息 """
    return r.getjson(GROUP_USERS_KV_INFO % group_id) or set_group_users_info_by_id(group_id)[1]


# 群组照片相关


def set_group_photo_data(group_id):
    """ 设置群组照片数据信息 """
    from group.models import GroupPhotoInfo
    group_photos = GroupPhotoInfo.objects.filter(group_id=group_id, status=True)
    group_photos = [photo.photo_data for photo in group_photos]
    r.setjson(GROUP_PHOTO_DATA % group_id, group_photos)
    return group_photos


def get_group_photo_data(group_id):
    """ 获取群组照片数据信息 """
    return r.getjson(GROUP_PHOTO_DATA % group_id, default='[]') or set_group_photo_data(group_id)


def set_group_photo_thumbup_flag(photo_id, user_id):
    """ 设置群组照片用户点赞标识信息 """
    r.setex(GROUP_PHOTO_THUMB_UP % (photo_id, user_id), r.REDIS_EXPIRED_ONE_MONTH, True)


def del_group_photo_thumbup_flag(photo_id, user_id):
    """ 删除群组照片用户点赞标识信息 """
    r.delete(GROUP_PHOTO_THUMB_UP % (photo_id, user_id))


def get_group_photo_thumbup_flag(photo_id, user_id):
    """ 获取群组照片用户点赞标识信息 """
    if r.exists(GROUP_PHOTO_THUMB_UP % (photo_id, user_id)):
        return True

    from group.models import PhotoThumbUpInfo
    if PhotoThumbUpInfo.objects.filter(photo_id=photo_id, user_id=user_id, thumbup=True, status=True).exists():
        set_group_photo_thumbup_flag(photo_id, user_id)
        return True

    return False


def set_group_photo_comment_list(photo_id):
    """ 设置群组照片用户评论列表 """
    from group.models import PhotoCommentInfo
    photo_comments = PhotoCommentInfo.objects.filter(photo_id=photo_id, status=True)
    photo_comments = [comment.comment_info for comment in photo_comments]
    r.setjson(GROUP_PHOTO_COMMENT_LIST % photo_id, photo_comments, cls=DjangoJSONEncoder)
    return photo_comments


def get_group_photo_comment_list(photo_id):
    """ 获取群组照片用户评论列表 """
    return r.getjson(GROUP_PHOTO_COMMENT_LIST % photo_id, default='[]') or set_group_photo_comment_list(photo_id)


def set_group_photo_thumbup_list(photo_id):
    """ 设置群组照片用户点赞列表 """
    from group.models import PhotoThumbUpInfo
    photo_thumbups = PhotoThumbUpInfo.objects.filter(photo_id=photo_id, thumbup=True, status=True)
    photo_thumbups = [thumbup.thumbup_info for thumbup in photo_thumbups]
    r.setjson(GROUP_PHOTO_THUMB_UP_LIST % photo_id, photo_thumbups, cls=DjangoJSONEncoder)
    return photo_thumbups


def get_group_photo_thumbup_list(photo_id):
    """ 获取群组照片用户点赞列表 """
    return r.getjson(GROUP_PHOTO_THUMB_UP_LIST % photo_id, default='[]') or set_group_photo_thumbup_list(photo_id)


def get_group_photo_watchers(photo_id, discarders=None):
    """ 获取群组照片用户关注列表 """
    watchers = r.smembers(GROUP_PHOTO_WATCHER_SET % photo_id)
    [watchers.discard(elem) for elem in discarders or []]
    return watchers
