"""Cart render functions — called from bp routes."""
from __future__ import annotations

from .utils import _serialize_order, _serialize_calendar_entry


async def render_orders_page(ctx, orders, page, total_pages, search, search_count, url_for_fn, qs_fn):
    from shared.sx.helpers import sx_call, render_to_sx_with_env, search_desktop_sx, search_mobile_sx, full_page_sx
    from shared.utils import route_prefix
    ctx["search"] = search
    ctx["search_count"] = search_count
    pfx = route_prefix()
    list_url = pfx + url_for_fn("orders.list_orders")
    detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
    order_dicts = [_serialize_order(o) for o in orders]
    content = sx_call("orders-list-content", orders=order_dicts,
        page=page, total_pages=total_pages, rows_url=list_url, detail_url_prefix=detail_url_prefix)
    header_rows = await render_to_sx_with_env("layouts/orders-layout-full", {},
        list_url=list_url,
    )
    filt = sx_call("order-list-header", search_mobile=await search_mobile_sx(ctx))
    return await full_page_sx(ctx, header_rows=header_rows, filter=filt,
                          aside=await search_desktop_sx(ctx), content=content)


def render_orders_rows(ctx, orders, page, total_pages, url_for_fn, qs_fn):
    from shared.sx.helpers import sx_call
    from shared.utils import route_prefix
    pfx = route_prefix()
    list_url = pfx + url_for_fn("orders.list_orders")
    detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
    order_dicts = [_serialize_order(o) for o in orders]
    next_url = list_url + qs_fn(page=page + 1) if page < total_pages else ""
    return sx_call("cart-orders-rows-content",
        orders=order_dicts, detail_url_prefix=detail_url_prefix,
        page=page, total_pages=total_pages, next_url=next_url)


async def render_orders_oob(ctx, orders, page, total_pages, search, search_count, url_for_fn, qs_fn):
    from shared.sx.helpers import sx_call, render_to_sx_with_env, search_desktop_sx, search_mobile_sx, oob_page_sx
    from shared.utils import route_prefix
    ctx["search"] = search
    ctx["search_count"] = search_count
    pfx = route_prefix()
    list_url = pfx + url_for_fn("orders.list_orders")
    detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0]
    order_dicts = [_serialize_order(o) for o in orders]
    content = sx_call("orders-list-content", orders=order_dicts,
        page=page, total_pages=total_pages, rows_url=list_url, detail_url_prefix=detail_url_prefix)
    oobs = await render_to_sx_with_env("layouts/orders-layout-oob", {},
        list_url=list_url,
    )
    filt = sx_call("order-list-header", search_mobile=await search_mobile_sx(ctx))
    return await oob_page_sx(oobs=oobs, filter=filt, aside=await search_desktop_sx(ctx), content=content)


async def render_order_page(ctx, order, calendar_entries, url_for_fn):
    from shared.sx.helpers import sx_call, render_to_sx_with_env, full_page_sx
    from shared.utils import route_prefix
    from shared.browser.app.csrf import generate_csrf_token
    pfx = route_prefix()
    detail_url = pfx + url_for_fn("orders.order.order_detail", order_id=order.id)
    list_url = pfx + url_for_fn("orders.list_orders")
    recheck_url = pfx + url_for_fn("orders.order.order_recheck", order_id=order.id)
    pay_url = pfx + url_for_fn("orders.order.order_pay", order_id=order.id)
    order_data = _serialize_order(order)
    cal_data = [_serialize_calendar_entry(e) for e in (calendar_entries or [])]
    main = sx_call("order-detail-content", order=order_data, calendar_entries=cal_data)
    filt = sx_call("order-detail-filter-content", order=order_data,
        list_url=list_url, recheck_url=recheck_url, pay_url=pay_url, csrf=generate_csrf_token())
    header_rows = await render_to_sx_with_env("layouts/order-detail-layout-full", {},
        list_url=list_url, detail_url=detail_url,
        order_label=f"Order {order.id}",
    )
    return await full_page_sx(ctx, header_rows=header_rows, filter=filt, content=main)


async def render_order_oob(ctx, order, calendar_entries, url_for_fn):
    from shared.sx.helpers import sx_call, render_to_sx_with_env, oob_page_sx
    from shared.utils import route_prefix
    from shared.browser.app.csrf import generate_csrf_token
    pfx = route_prefix()
    detail_url = pfx + url_for_fn("orders.order.order_detail", order_id=order.id)
    list_url = pfx + url_for_fn("orders.list_orders")
    recheck_url = pfx + url_for_fn("orders.order.order_recheck", order_id=order.id)
    pay_url = pfx + url_for_fn("orders.order.order_pay", order_id=order.id)
    order_data = _serialize_order(order)
    cal_data = [_serialize_calendar_entry(e) for e in (calendar_entries or [])]
    main = sx_call("order-detail-content", order=order_data, calendar_entries=cal_data)
    filt = sx_call("order-detail-filter-content", order=order_data,
        list_url=list_url, recheck_url=recheck_url, pay_url=pay_url, csrf=generate_csrf_token())
    oobs = await render_to_sx_with_env("layouts/order-detail-layout-oob", {},
        detail_url=detail_url,
        order_label=f"Order {order.id}",
    )
    return await oob_page_sx(oobs=oobs, filter=filt, content=main)


async def render_checkout_error_page(ctx, error=None, order=None):
    from shared.sx.helpers import sx_call, render_to_sx_with_env, full_page_sx
    from shared.infrastructure.urls import cart_url
    err_msg = error or "Unexpected error while creating the hosted checkout session."
    hdr = await render_to_sx_with_env("shared:layout/root-full", {})
    filt = sx_call("checkout-error-header")
    content = sx_call("cart-checkout-error-from-data",
        msg=err_msg, order_id=order.id if order else None,
        back_url=cart_url("/"))
    return await full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)


def render_cart_payments_panel(ctx):
    from shared.sx.helpers import sx_call
    page_config = ctx.get("page_config")
    pc_data = None
    if page_config:
        pc_data = {
            "sumup_api_key": bool(getattr(page_config, "sumup_api_key", None)),
            "sumup_merchant_code": getattr(page_config, "sumup_merchant_code", None) or "",
            "sumup_checkout_prefix": getattr(page_config, "sumup_checkout_prefix", None) or "",
        }
    return sx_call("cart-payments-content", page_config=pc_data)
