from typing import Iterable, Tuple

from fetchfox.apis.cardano import gomaestroorg
from fetchfox.constants.book import BOOK_POOL_TICKER, BOOK_POOL_ID


class BookPool:
    id = BOOK_POOL_ID
    ticker = BOOK_POOL_TICKER

    def __init__(self, maestro_api_key: str = None):
        self.maestro_api_key = maestro_api_key

    @property
    def delegated_amount(self) -> float:
        info = gomaestroorg.get_pool_information(
            self.id,
            api_key=self.maestro_api_key,
        )

        return info["live_stake"] / 10**6

    @property
    def delegator_count(self) -> int:
        info = gomaestroorg.get_pool_information(
            self.id,
            api_key=self.maestro_api_key,
        )

        return info["live_delegators"]

    @property
    def saturation(self) -> float:
        info = gomaestroorg.get_pool_information(
            self.id,
            api_key=self.maestro_api_key,
        )

        return float(info["live_saturation"])

    @property
    def delegators(self) -> Iterable[Tuple[str, float]]:
        yield from gomaestroorg.get_pool_delegators(
            self.id,
            api_key=self.maestro_api_key,
        )
