import logging
import time

logger = logging.getLogger(__name__)


class DataManager:
    def __init__(self, downloader, extractor, formatter):
        self._downloader = downloader
        self._extractor = extractor
        self._formatter = formatter

    async def download_and_save(self, symbol, timeframe, date_from=None, date_to=None):
        start = time.time()
        await self._downloader.download(symbol, timeframe, date_from=date_from, date_to=date_to)
        logger.info(f"Download stage finished in {time.time() - start:.2f}s")

        start = time.time()
        await self._extractor.extract(symbol, timeframe, date_from=date_from, date_to=date_to)
        logger.info(f"Extract stage finished in {time.time() - start:.2f}s")

        start = time.time()
        await self._formatter.format(symbol, timeframe, date_from=date_from, date_to=date_to)
        logger.info(f"Format stage finished in {time.time() - start:.2f}s")


if __name__ == '__main__':
    import asyncio
    from .data_downloader import DataDownloader
    from .data_extractor import DataExtractor
    from .data_formatter import DataFormatter
    from .binance_metadata_manager import BinanceMetadataManager

    downloader = DataDownloader("downloads/", BinanceMetadataManager("downloads/metadata.json"))
    extractor = DataExtractor("downloads/", "extracts/", BinanceMetadataManager("extracts/metadata.json"))
    formatter = DataFormatter("extracts/", "data/", BinanceMetadataManager("data/metadata.json"))

    manager = DataManager(downloader, extractor, formatter)


    async def main():
        await manager.download_and_save("BTCUSDT", '1m')


    start = time.time()
    asyncio.run(main())

    print(time.time() - start)
