import base64

from .resources.accounts import Account
from .resources.address import Address
from .resources.constant_symbol import ConstantSymbol
from .resources.divisions import Division
from .resources.firm import Firm
from .resources.period import Period
from .resources.received_invoice import ReceivedInvoice
from .version import __version__
from .requestor import Requestor


class ApiClient(object):
    def __init__(self, api_url: str, username: str, password: str, timeout: int = 30):
        credentials = f"{username}:{password}".encode("ascii")

        self._api_url = api_url
        self._auth_token = f'Basic {base64.b64encode(credentials).decode("ascii")}'
        self._timeout = timeout
        self._requestor = Requestor(api_url=self._api_url, auth_token=self._auth_token, timeout=self._timeout)
        self._address = Address(requestor=self._requestor)
        self._constant_symbol = ConstantSymbol(requestor=self._requestor)
        self._division = Division(requestor=self._requestor)
        self._firm = Firm(requestor=self._requestor)
        self._period = Period(requestor=self._requestor)
        self._received_invoice = ReceivedInvoice(requestor=self._requestor)
        self._account = Account(requestor=self._requestor)

    @property
    def version(self) -> str:
        return __version__

    @property
    def api_url(self) -> str:
        return self._api_url

    @property
    def auth_token(self) -> str:
        return self._auth_token

    @property
    def timeout(self) -> int:
        return self._timeout

    def __str__(self) -> str:
        return f"{self._api_url}"

    @property
    def address(self):
        return self._address

    @property
    def constant_symbol(self):
        return self._constant_symbol

    @property
    def division(self):
        return self._division

    @property
    def firm(self):
        return self._firm

    @property
    def period(self):
        return self._period

    @property
    def received_invoice(self):
        return self._received_invoice

    @property
    def account(self):
        return self._account
