import "reflect-metadata";

import ChevronIcon from "@Assets/Icons/chevron.svg";
import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import FolderBoxInformation, { EFolderBoxInformationType } from "@Front/Components/DesignSystem/FolderBoxInformation";
import InputField from "@Front/Components/DesignSystem/Form/Elements/InputField";
import Confirm from "@Front/Components/DesignSystem/Modal/Confirm";
import QuantityProgressBar from "@Front/Components/DesignSystem/QuantityProgressBar";
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
import { folders } from "@Front/Components/Layouts/DesignSystem/dummyData";
import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import Link from "next/link";
import { useRouter } from "next/router";
import BasePage from "../../Base";
import classes from "./classes.module.scss";
import ClientSection from "./ClientSection";


type IProps = {};

type IPropsClass = IProps & {
	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);
	}

	// 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 {
		const redirectPathEditCollaborators = Module.getInstance()
			.get()
			.modules.pages.Folder.pages.EditCollaborators.props.path.replace("[folderUid]", this.props.selectedFolderUid);
		return (
			<DefaultNotaryDashboard title={"Dossier"} onSelectedFolder={this.onSelectedFolder} isArchived={false} mobileBackText="Retour aux dossiers">
				<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>
									<Link href={redirectPathEditCollaborators}>
										<Button variant={EButtonVariant.LINE} icon={ChevronIcon}>
											Modifier les collaborateurs
										</Button>
									</Link>
								</div>
								<FolderBoxInformation folder={this.state.selectedFolder} type={EFolderBoxInformationType.INFORMATIONS} />
								<div className={classes["second-box"]}>
									<FolderBoxInformation folder={this.state.selectedFolder} type={EFolderBoxInformationType.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.openArchivedModal}>
									Archiver le dossier
								</Button>
								{!this.doesFolderHaveCustomer() && (
									<span className={classes["delete-folder"]}>
										<Button variant={EButtonVariant.SECONDARY}>Supprimer le dossier</Button>
									</span>
								)}
							</div>
							<Confirm
								isOpen={this.state.isArchivedModalOpen}
								onClose={this.closeArchivedModal}
								closeBtn
								header={"Archiver le dossier ?"}
								cancelText={"Annuler"}
								confirmText={"Archiver"}>
								<div className={classes["modal-title"]}>
									<Typography typo={ITypo.P_16}>Souhaitez-vous vraiment archiver le dossier ?</Typography>
								</div>
								<InputField name="input field" fakeplaceholder="Description" textarea />
							</Confirm>
						</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}>
									Veuillez sélectionner un dossier.
								</Typography>
							</div>
						</div>
					)}
				</div>
			</DefaultNotaryDashboard>
		);
	}
	public override async componentDidMount() {
		for (const folder of folders) {
			if (folder.uid === this.props.selectedFolderUid) {
				this.setState({ selectedFolder: folder });
				break;
			}
		}
	}

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

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

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

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

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