from gm_upload.utils.image_utils import Picture
from google.protobuf import json_format

from utils.gevent_util import get_brain_client
from utils.rpc import get_current_user
from utils.rpc import logging_exception

client = get_brain_client(client_name='user')


def get_fundamental_info_by_user_ids(user_ids, person_ids, current_user_id=0):
    fundamental_info = list()
    try:
        if not user_ids and not person_ids and not current_user_id:
            user = get_current_user()
            current_user_id = user.id if user else 0
            if current_user_id == 0:
                return fundamental_info

        user_ids = [int(i) for i in user_ids]

        resp = client.getUserFundamentalInfo(current_user_id, user_ids, person_ids, 10)
        result = json_format.MessageToDict(resp, including_default_value_fields=True, preserving_proto_field_name=True)
        for base_user in result["base_user"]:
            base_user["portrait"] = Picture.get_thumb_path(base_user["portrait"])
            for k, v in base_user.items():
                if v == -1:
                    base_user[k] = None
            fundamental_info.append(base_user)

        return fundamental_info

    except Exception as ex:
        logging_exception()
        return fundamental_info

