"""TiTiler COG Demo endpoint."""

import pkg_resources
from rio_cogeo.cogeo import cog_info as rio_cogeo_info
from rio_tiler_crs import COGReader

from ..dependencies import PathParams
from ..models.dataset import RioCogeoInfo
from .factory import TMSTilerFactory

from fastapi import Depends, Query

from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates

template_dir = pkg_resources.resource_filename("titiler", "templates")
templates = Jinja2Templates(directory=template_dir)

# Create Router using Tiler Factory
cog = TMSTilerFactory(reader=COGReader, router_prefix="cog")


@cog.router.get("/validate", response_model=RioCogeoInfo)
def cog_validate(
    src_path: PathParams = Depends(),
    strict: bool = Query(False, description="Treat warnings as errors"),
):
    """Validate a COG"""
    return rio_cogeo_info(src_path.url, strict=strict)


@cog.router.get("/viewer", response_class=HTMLResponse)
def cog_demo(request: Request):
    """COG Viewer."""
    return templates.TemplateResponse(
        name="cog_index.html",
        context={
            "request": request,
            "tilejson": request.url_for(f"{cog.router_prefix}tilejson"),
            "metadata": request.url_for(f"{cog.router_prefix}metadata"),
        },
        media_type="text/html",
    )


router = cog.router
