import { getCookie } from "cookies-next";
import Head from "next/head";
import type { ReactNode } from "react";
import {
	SIDEBAR_COOKIE_NAME,
	Sidebar,
} from "~/components/area-riservata/sidebar";
import { BannerFactory } from "~/components/banners";
import { Footer } from "~/components/footer";
import { LoadingPage } from "~/components/loading";
import { SiteHeader } from "~/components/navbar/site-header";
import { UserViewProvider } from "~/lib/userViewContext";
import { cn } from "~/lib/utils";
import {
	EnforcedSessionContext,
	useSession,
} from "~/providers/SessionProvider";
import type { UsersId } from "~/schemas/public/Users";
import { api } from "~/utils/api";
import { UserViewHeader } from "./area-riservata/userViewHeader";

interface Props {
	bodyClassName?: string;
	containerClassName?: string;
	children: ReactNode;
	noFooter?: boolean;
	footerClassName?: string;
}
export const Layout = ({
	children,
	noFooter,
	bodyClassName,
	containerClassName,
	footerClassName,
}: Props) => {
	const { data: bannerData } = api.banners.getBannerData.useQuery({
		area_riservata: false,
	});

	return (
		<div className={cn("flex min-h-screen w-full flex-col", bodyClassName)}>
			<SiteHeader />
			{bannerData?.map((banner) => (
				<div key={`banner-elem-${banner.idbanner}`}>
					{BannerFactory(banner)}
				</div>
			))}
			<main
				className={cn("flex h-full flex-1 grow flex-col", containerClassName)}
			>
				{children}
			</main>

			{!noFooter && <Footer className={footerClassName} />}
		</div>
	);
};

interface AreaRiservataLayoutProps
	extends Omit<Props, "noFooter" | "footerClassName"> {
	ignoreSessionCheck?: boolean;
	noSidebar?: boolean;
}

export const AreaRiservataLayout = ({
	children,
	noSidebar,
	ignoreSessionCheck,
	bodyClassName,
	containerClassName,
}: AreaRiservataLayoutProps) => {
	const { data: bannerData } = api.banners.getBannerData.useQuery({
		area_riservata: true,
	});
	const { status, user } = useSession();
	if (!ignoreSessionCheck) {
		if (status === "LOADING")
			return (
				<div
					className={cn(
						"flex h-full min-h-screen flex-col text-clip",
						bodyClassName,
					)}
				>
					<SiteHeader />

					<main
						className={cn(
							"flex h-full flex-1 overflow-auto",
							containerClassName,
						)}
					>
						<div className="flex h-auto w-full flex-col md:flex-row">
							<LoadingPage />
						</div>
					</main>
				</div>
			);
		if (status !== "AUTHENTICATED") {
			window.location.reload();
			return (
				<div
					className={cn(
						"flex h-full min-h-screen flex-col text-clip",
						bodyClassName,
					)}
				>
					<SiteHeader />

					<main
						className={cn(
							"flex h-full flex-1 overflow-auto",
							containerClassName,
						)}
					>
						<div className="flex h-auto w-full flex-col md:flex-row">
							<LoadingPage />
						</div>
					</main>
				</div>
			);
		}
	}
	if (status !== "AUTHENTICATED") {
		window.location.reload();
		return (
			<div
				className={cn(
					"flex h-full min-h-screen flex-col text-clip",
					bodyClassName,
				)}
			>
				<SiteHeader />

				<main
					className={cn("flex h-full flex-1 overflow-auto", containerClassName)}
				>
					<div className="flex h-auto w-full flex-col md:flex-row">
						<LoadingPage />
					</div>
				</main>
			</div>
		);
	}

	return (
		<div
			className={cn(
				"flex h-full min-h-screen flex-col text-clip",
				bodyClassName,
			)}
		>
			<SiteHeader />
			{bannerData?.map((banner) => (
				<div key={`banner-elem-${banner.idbanner}`}>
					{BannerFactory(banner)}
				</div>
			))}
			<main
				className={cn("flex h-full flex-1 overflow-auto", containerClassName)}
			>
				<div className="flex h-auto w-full flex-col md:flex-row">
					{noSidebar ? null : (
						<Sidebar
							defaultOpen={getCookie(SIDEBAR_COOKIE_NAME) === "true"}
							isAdmin={user?.isAdmin || false}
						/>
					)}
					<EnforcedSessionContext.Provider value={{ user, status }}>
						{children}
					</EnforcedSessionContext.Provider>
				</div>
			</main>
		</div>
	);
};

export const AreaRiservataLayoutUserView = ({
	children,
}: {
	children: ReactNode & { props: { userId: UsersId } };
}) => {
	const { status, user } = useSession();
	const props = children.props as { userId: UsersId };
	if (status === "LOADING") {
		return <LoadingPage />;
	}
	if (status !== "AUTHENTICATED") {
		window.location.reload();
		return <LoadingPage />;
	}
	return (
		<div className="flex h-full min-h-screen flex-col">
			<SiteHeader />
			<main className="flex h-full flex-1 overflow-auto">
				<UserViewProvider userId={props.userId}>
					<EnforcedSessionContext.Provider value={{ user, status }}>
						<Head>
							<title>User View - Infoalloggi.it</title>
						</Head>
						<div className="flex h-auto w-full flex-col md:flex-row">
							<Sidebar
								defaultOpen={getCookie(SIDEBAR_COOKIE_NAME) === "true"}
								isAdmin
							/>

							<div className="mx-2 flex-1 overflow-auto py-3 lg:mx-4">
								<UserViewHeader />

								{children}
							</div>
						</div>
					</EnforcedSessionContext.Provider>
				</UserViewProvider>
			</main>
		</div>
	);
};
