import backgroundImage from "@Assets/images/background_refonte.svg";
import LogoIcon from "@Assets/logo_small_blue.svg";
import SearchBlockList from "@Front/Components/DesignSystem/SearchBlockList";
import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import Image from "next/image";
import { useRouter } from "next/router";
import { useCallback, useEffect, useState } from "react";

import classes from "./classes.module.scss";
import Module from "@Front/Config/Module";

import UserStore from "@Front/Stores/UserStore";

import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService";
import FolderService from "src/common/Api/LeCoffreApi/sdk/FolderService";

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

	useEffect(() => {
		const customer: any = UserStore.instance.getUser();
		if (!customer) {
			return;
		}
		setCustomer(customer);

		LoaderService.getInstance().show();
		FolderService.getFolders().then((processes: any[]) => {
			if (processes.length > 0) {
				let folders: any[] = processes.map((process: any) => process.processData);

				// FilterBy customer.uid
				folders = folders.filter((folder: any) => folder.customers && folder.customers.length > 0 && folder.customers.some((customer: any) => customer.uid === customer.uid));

				// OrderBy created_at desc
				folders = folders.sort((a: any, b: any) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());

				setFolders(folders);
				LoaderService.getInstance().hide();
			}
		});
	}, []);

	const handleSelectBlock = useCallback(
		(folder: IBlock) => {
			router.push(
				Module.getInstance()
					.get()
					.modules.pages.ClientDashboard.props.path.replace("[folderUid]", folder.id ?? "").replace("[profileUid]", customer.uid ?? ""),
			);
		},
		[router],
	);

	return (
		<DefaultDoubleSidePage title="Sélectionner un dossier" image={backgroundImage}>
			<div className={classes["root"]}>
				<div className={classes["title-container"]}>
					<Image src={LogoIcon} alt="logo" width={56} />
					<Typography typo={ETypo.TITLE_H1} color={ETypoColor.TEXT_ACCENT}>
						Vos dossiers en cours
					</Typography>

					<Typography typo={ETypo.TITLE_H5} color={ETypoColor.TEXT_PRIMARY}>
						Veuillez sélectionner le dossier pour lequel vous souhaitez déposer ou consulter des documents.
					</Typography>
				</div>

				<Typography typo={ETypo.TITLE_H6} color={ETypoColor.TEXT_ACCENT}>
					Liste des dossiers disponibles :
				</Typography>

				<SearchBlockList blocks={getBlocks(folders)} onSelectedBlock={handleSelectBlock} fullwidth />
			</div>
		</DefaultDoubleSidePage>
	);
}

function getBlocks(folders: OfficeFolder[]): IBlock[] {
	return folders.map((folder) => {
		return {
			id: folder.uid!,
			primaryText: folder.name!,
			secondaryText: folder.folder_number!,
		};
	});
}
