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

import json

from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder

from utils.redis.rkeys import GROUP_INFO, GROUP_PHOTO_DATA, GROUP_PHOTO_THUMB_UP, GROUP_USERS_INFO


r = settings.REDIS_CACHE


# 群组信息相关


def set_group_info(group):
    """ 设置群组信息 """
    group_data = group.data
    r.setex(GROUP_INFO % group.group_id, settings.REDIS_EXPIRED_MONTH, json.dumps(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 json.loads(r.get(GROUP_INFO % group_id) or '{}') or set_group_info_by_id(group_id)


# 群组用户信息相关


def set_group_users_info(group):
    """ 设置群组用户信息 """
    group_users = group.users(admin=False)
    r.setex(GROUP_USERS_INFO % group.group_id, settings.REDIS_EXPIRED_MONTH, json.dumps({
        'group_users': group_users,
        'admin_id': group.admin_id,
    }, cls=DjangoJSONEncoder))
    return group_users


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 = json.loads(r.get(GROUP_USERS_INFO % group_id) or '{}') or set_group_users_info_by_id(group_id)
    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 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.set(GROUP_PHOTO_DATA % group_id, json.dumps(group_photos))
    return group_photos


def get_group_photo_data(group_id):
    """ 获取群组照片数据信息 """
    return json.loads(r.get(GROUP_PHOTO_DATA % group_id) or '[]') 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), settings.REDIS_EXPIRED_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
    else:
        from group.models import PhotoThumbUpInfo
        if PhotoThumbUpInfo.objects.filter(photo_id=photo_id, user_id=user_id, thumbup=True).exists():
            set_group_photo_thumbup_flag(photo_id, user_id)
            return True
        else:
            return False
