import DefaultTemplate from "@Front/Components/LayoutTemplates/DefaultTemplate";
import classes from "./classes.module.scss";
import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography";
import BlockList, { IBlock } from "@Front/Components/DesignSystem/BlockList";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import { useCallback, useEffect, useState } from "react";
import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";
import { useRouter } from "next/router";
import Module from "@Front/Config/Module";
import BackgroundImage from "@Assets/images/background.png";
import Image from "next/image";

export default function SelectFolder() {
	const [folders, setFolders] = useState<OfficeFolder[]>([]);
	const router = useRouter();

	useEffect(() => {
		async function getFolders() {
			const folders = await Folders.getInstance().get({});
			setFolders(folders.slice(0, 3));
		}

		getFolders();
	}, []);

	const handleSelectBlock = useCallback(
		(block: IBlock) => {
			router.push(
				Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", block.id),
			);
		},
		[router],
	);

	return (
		<DefaultTemplate title="Sélectionner un dossier" hasHeaderLinks={false}>
			<div className={classes["root"]}>
				<div className={classes["background-container"]}>
					<Image src={BackgroundImage} alt="background" />
				</div>
				<div className={classes["select-folder-container"]}>
					<div className={classes["title"]}>
						<Typography typo={ITypo.H1}>Vos dossiers</Typography>
					</div>
					<div className={classes["folders-container"]}>
						<BlockList
							onSelectedBlock={handleSelectBlock}
							blocks={folders.map((folder) => {
								return {
									id: folder.uid!,
									name: folder.name!,
									selected: false,
								};
							})}
						/>
					</div>
				</div>
			</div>
		</DefaultTemplate>
	);
}
