import PlusIcon from "@Assets/Icons/plus.svg";
import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
import UserFolder from "@Front/Components/DesignSystem/UserFolder";
import { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import Link from "next/link";
import React from "react";

import classes from "./classes.module.scss";

type IProps = {
	folder: IDashBoardFolder;
};
type IState = {
	openedCustomer: string;
};

export default class ClientSection extends React.Component<IProps, IState> {
	public constructor(props: IProps) {
		super(props);
		this.state = {
			openedCustomer: "",
		};

		this.selectUserFolder = this.selectUserFolder.bind(this);
		this.closeUserFolder = this.closeUserFolder.bind(this);
	}

	public override render(): JSX.Element {
		const navigatePath = Module.getInstance()
			.get()
			.modules.pages.Folder.pages.AddClient.props.path.replace("[folderUid]", this.props.folder.uid ?? "");
		return (
			<div className={classes["root"]}>
				{this.doesFolderHaveCustomer() ? (
					<>
						<div className={classes["client"]}>{this.renderCustomerFolders()}</div>
						<Link href={navigatePath}>
							<Button variant={EButtonVariant.LINE} icon={PlusIcon}>
								Ajouter un client
							</Button>
						</Link>
					</>
				) : (
					<div className={classes["no-client"]}>
						<div className={classes["title"]}>
							<Typography typo={ITypo.P_18} color={ITypoColor.GREY}>
								Aucun client n'est associé au dossier.
							</Typography>
						</div>
						<Link href={navigatePath}>
							<Button variant={EButtonVariant.LINE} icon={PlusIcon}>
								Ajouter un client
							</Button>
						</Link>
					</div>
				)}
			</div>
		);
	}

	private renderCustomerFolders() {
		const output = this.props.folder.office_folder_has_customers?.map((folderHasCustomer) => {
			if (!folderHasCustomer.customer) return null;
			console.log();
			// TODO : Les documents ASKED fonctionne mais les autres documents ne doivcent etre seulement ceux qui correspondent au folder
			return (
				<UserFolder
					folder={this.props.folder}
					customer={folderHasCustomer.customer}
					key={folderHasCustomer.customer.uid}
					isOpened={this.state.openedCustomer === folderHasCustomer.customer.uid}
					onOpen={this.selectUserFolder}
					onClose={this.closeUserFolder}
				/>
			);
		});
		return output ?? null;
	}

	private closeUserFolder() {
		this.setState({
			openedCustomer: "",
		});
	}

	private selectUserFolder(index: string) {
		this.setState({
			openedCustomer: index,
		});
	}

	private doesFolderHaveCustomer(): boolean {
		if (!this.props.folder?.office_folder_has_customers) return false;
		return this.props.folder?.office_folder_has_customers!.length > 0;
	}
}
