from os import environ
from pathlib import Path

import uvicorn

from gallery.easel import build_app
from gallery.painting.gismeteo.api import GismeteoApi
from gallery.painting.matchtv.api import MatchTvApi
from gallery.painting.openweather.api import OpenWeatherApi
from gallery.painting.yandextv.api import YandexTvApi
from gallery.sketch.bundle import ApiBundle
from gallery.sketch.schedule.cached import CachedScheduleApi
from gallery.sketch.weather.cached import CachedWeatherApi

api = ApiBundle(
    [
        CachedScheduleApi(YandexTvApi()),
        CachedScheduleApi(MatchTvApi()),
        CachedWeatherApi(GismeteoApi()),
        CachedWeatherApi(OpenWeatherApi()),
    ]
)
app = build_app(api)


def run():
    uvicorn.run(
        "gallery.main:app",
        host=environ.get("GALLERY_HOST", "0.0.0.0"),
        port=int(environ.get("GALLERY_PORT", 8000)),
        log_config=str(Path(__file__).parent / "logging.yaml"),
        reload="DEBUG" in environ,
    )


if __name__ == "__main__":
    run()
