from ...crypto import Crypto
from ... import exceptions
from ...types import Results

from typing import Union

import aiorubika


class Builder:
    async def builder(
            self: "aiorubika.Client",
            name: str,
            tmp_session: bool = False,
            encrypt: bool = True,
            dict: bool = False,
            input: dict = None,
    ) -> Union[Results, dict]:
        if not self.connection.api_url:
            await self.connection.get_dcs()

        if self.auth is None:
            self.auth = Crypto.secret(length=32)

        if self.key is None:
            self.key = Crypto.passphrase(self.auth)

        result = await self.connection.send(
            method=name,
            tmp_session=tmp_session,
            encrypt=encrypt,
            input=input
        )
        data_enc = result.get('data_enc')
        if data_enc:
            result = Crypto.decrypt(data_enc, key=self.key)

        status = result['status']
        status_det = result['status_det']

        if status == 'OK' and status_det == 'OK':
            if dict:
                return result.get('data')

            result['data']['_client'] = self
            return Results(result['data'])

        raise exceptions(status_det)(result, request=None)
