import ChevronIcon from "@Assets/Icons/chevron.svg";
import Folders, { IPutFoldersParams } from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";
import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import FolderBoxInformation, { EFolderBoxInformationType } from "@Front/Components/DesignSystem/FolderBoxInformation";
import QuantityProgressBar from "@Front/Components/DesignSystem/QuantityProgressBar";
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import { NextRouter, useRouter } from "next/router";

import BasePage from "../../Base";
import classes from "./classes.module.scss";
import ClientSection from "./ClientSection";

type IProps = {};

type IPropsClass = IProps & {
	router: NextRouter;
	selectedFolderUid: string;
};

type IState = {
	selectedFolder: IDashBoardFolder | null;
	isArchivedModalOpen: boolean;
};
class FolderInformationClass extends BasePage<IPropsClass, IState> {
	public constructor(props: IPropsClass) {
		super(props);
		this.state = {
			selectedFolder: null,
			isArchivedModalOpen: false,
		};
		this.onSelectedFolder = this.onSelectedFolder.bind(this);
		this.openArchivedModal = this.openArchivedModal.bind(this);
		this.closeArchivedModal = this.closeArchivedModal.bind(this);
		this.restoreFolder = this.restoreFolder.bind(this);
	}

	// TODO: Message if the user has not created any folder yet
	// TODO: get the selected folder from the api in componentDidMount
	public override render(): JSX.Element {
		return (
			<DefaultNotaryDashboard title={"Dossier"} onSelectedFolder={this.onSelectedFolder} isArchived={true}>
				<div className={classes["root"]}>
					{this.state.selectedFolder ? (
						<div className={classes["folder-informations"]}>
							<div className={classes["folder-header"]}>
								<div className={classes["header"]}>
									<div className={classes["title"]}>
										<Typography typo={ITypo.H1Bis}>Informations du dossier</Typography>
									</div>
									<Button variant={EButtonVariant.LINE} icon={ChevronIcon}>
										Modifier les collaborateurs
									</Button>
								</div>
								<FolderBoxInformation
									folder={this.state.selectedFolder}
									isArchived
									type={EFolderBoxInformationType.INFORMATIONS}
								/>
								<div className={classes["second-box"]}>
									<FolderBoxInformation
										folder={this.state.selectedFolder}
										isArchived
										type={EFolderBoxInformationType.DESCRIPTION}
									/>
								</div>
								<div className={classes["second-box"]}>
									<FolderBoxInformation
										folder={this.state.selectedFolder}
										isArchived
										type={EFolderBoxInformationType.ARCHIVED_DESCRIPTION}
									/>
								</div>

								<div className={classes["progress-bar"]}>
									<QuantityProgressBar title="Complétion du dossier" total={100} currentNumber={0} />
								</div>
								{this.doesFolderHaveCustomer() && <ClientSection folder={this.state.selectedFolder} />}
							</div>

							{!this.doesFolderHaveCustomer() && <ClientSection folder={this.state.selectedFolder} />}

							<div className={classes["button-container"]}>
								<Button variant={EButtonVariant.GHOST} onClick={this.restoreFolder}>
									Restaurer le dossier
								</Button>
							</div>
						</div>
					) : (
						<div className={classes["no-folder-selected"]}>
							<Typography typo={ITypo.H1Bis}>Informations du dossier</Typography>
							<div className={classes["choose-a-folder"]}>
								<Typography typo={ITypo.P_18} color={ITypoColor.GREY}>
									Aucun dossier sélectionné
								</Typography>
							</div>
						</div>
					)}
				</div>
			</DefaultNotaryDashboard>
		);
	}
	public override async componentDidMount() {
		const folder = await this.getFolder();
		this.setState({ selectedFolder: folder });
	}

	private doesFolderHaveCustomer(): boolean {
		return this.state.selectedFolder?.office_folder_has_customers !== undefined;
	}

	private onSelectedFolder(folder: IDashBoardFolder): void {
		this.setState({ selectedFolder: folder });
	}

	private async restoreFolder() {
		if (!this.state.selectedFolder) return;
		const folder = this.state.selectedFolder;
		folder.archived_description = null;
		await Folders.getInstance().restore(this.state.selectedFolder.uid ?? "", folder as IPutFoldersParams);
		this.props.router.push(
			Module.getInstance()
				.get()
				.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.selectedFolderUid),
		);
	}

	private openArchivedModal(): void {
		this.setState({ isArchivedModalOpen: true });
	}

	private closeArchivedModal(): void {
		this.setState({ isArchivedModalOpen: false });
	}

	private async getFolder(): Promise<OfficeFolder> {
		const query = {
			q: {
				deed: { include: { deed_type: "true" } },
				office: "true",
				office_folder_has_customers: { include: { customer: { include: { contact: true } } } },
			},
		};
		const folder = await Folders.getInstance().getByUid(this.props.selectedFolderUid, query);
		return folder;
	}
}

export default function FolderInformation(props: IProps) {
	const router = useRouter();
	let { folderUid } = router.query;
	folderUid = folderUid as string;
	return <FolderInformationClass {...props} selectedFolderUid={folderUid} router={router} />;
}
