import React from "react";
import classes from "./classes.module.scss";
import { OfficeFolder } from "le-coffre-resources/dist/Notary";
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
import UserFolder from "@Front/Components/DesignSystem/UserFolder";

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

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

		this.changeUserFolder = this.changeUserFolder.bind(this);
	}

	public override render(): JSX.Element {
		return (
			<div className={classes["root"]}>
				{this.doesFolderHaveCustomer() ? (
					<>
						<div className={classes["client"]}>{this.renderCustomerFolders()}</div>
					</>
				) : (
					<div className={classes["no-client"]}>
						<Typography typo={ITypo.P_18} color={ITypoColor.GREY}>
							Aucun client dans ce dossier
						</Typography>
					</div>
				)}
			</div>
		);
	}

	private renderCustomerFolders() {
		const output = this.props.folder.customers?.map((customer) => {
			if (!customer) return null;
			return (
				<UserFolder
					folder={this.props.folder}
					customer={customer}
					key={customer.uid}
					isOpened={this.state.openedCustomer === customer.uid}
					onChange={this.changeUserFolder}
					isArchived
				/>
			);
		});
		return output ?? null;
	}

	private changeUserFolder(uid: string) {
		this.setState({
			openedCustomer: uid === this.state.openedCustomer ? "" : uid,
		});
	}

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