from eolymp.cognito import UserServiceClient, ListUsersInput, DescribeUserInput
from eolymp.wellknown import expression_pb2


from peolymp.abstract import AbstractAPI


class CognitoAPI(AbstractAPI):

    def __init__(self, space):
        super(CognitoAPI, self).__init__(space)
        self.client = UserServiceClient(self.http_client)

    def find_user_by_nickname(self, nickname):
        #print(nickname)
        s = expression_pb2.ExpressionString(value=nickname)
        setattr(s, 'is', 1)
        f = ListUsersInput.Filter(username=[s])
        users_input = ListUsersInput(filters=f)
        #print(users_input)
        o = self.client.ListUsers(users_input)
       # print('OK')
       # print(o.items)
        if len(o.items) == 0:
            return None
       # print(o.items)
        return o.items[0]

    def find_user_by_id(self, user_id):
        return self.client.DescribeUser(DescribeUserInput(user_id=user_id)).user