import ChevronIcon from "@Assets/Icons/chevron.svg";
import Folders, { IGetFoldersParams } from "@Front/Api/LeCoffreApi/Notary/Folders/Folders";
import Button, { EButtonstyletype, EButtonVariant } from "@Front/Components/DesignSystem/Button";
import FolderArchivedListContainer from "@Front/Components/DesignSystem/FolderArchivedListContainer";
import FolderListContainer from "@Front/Components/DesignSystem/FolderListContainer";
import Header from "@Front/Components/DesignSystem/Header";
import Version from "@Front/Components/DesignSystem/Version";
import BackArrow from "@Front/Components/Elements/BackArrow";
import WindowStore from "@Front/Stores/WindowStore";
import { ChevronLeftIcon } from "@heroicons/react/24/outline";
import classNames from "classnames";
import EFolderStatus from "le-coffre-resources/dist/Customer/EFolderStatus";
import { OfficeFolder } from "le-coffre-resources/dist/Notary";
import Image, { StaticImageData } from "next/image";
import React, { ReactNode } from "react";

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

type IProps = {
	title: string;
	children?: ReactNode;
	isArchived?: boolean;
	onSelectedFolder: (folder: OfficeFolder) => void;
	hasBackArrow: boolean;
	backArrowUrl?: string;
	mobileBackText?: string;
	image?: StaticImageData;
};
type IState = {
	folders: OfficeFolder[] | null;
	isLeftSideOpen: boolean;
	leftSideCanBeClosed: boolean;
};

export default class DefaultNotaryDashboard extends React.Component<IProps, IState> {
	private onWindowResize = () => {};
	public static defaultProps: Partial<IProps> = {
		hasBackArrow: false,
		isArchived: false,
	};

	public constructor(props: IProps) {
		super(props);
		this.state = {
			folders: null,
			isLeftSideOpen: false,
			leftSideCanBeClosed: typeof window !== "undefined" ? window.innerWidth < 1024 : false,
		};
		this.onOpenLeftSide = this.onOpenLeftSide.bind(this);
		this.onCloseLeftSide = this.onCloseLeftSide.bind(this);
	}

	public override render(): JSX.Element {
		return (
			<div className={classes["root"]}>
				<Header isUserConnected={true} />
				<div className={classes["content"]}>
					{this.state.isLeftSideOpen && <div className={classes["overlay"]} onClick={this.onCloseLeftSide} />}
					<div className={classNames(classes["left-side"], this.state.isLeftSideOpen && classes["opened"])}>
						{this.props.isArchived && this.state.folders && (
							<FolderArchivedListContainer
								folders={this.state.folders}
								onSelectedFolder={this.props.onSelectedFolder}
								onCloseLeftSide={this.onCloseLeftSide}
								isArchived={true}
							/>
						)}
						{!this.props.isArchived && this.state.folders && (
							<FolderListContainer
								folders={this.state.folders}
								onSelectedFolder={this.props.onSelectedFolder}
								onCloseLeftSide={this.onCloseLeftSide}
								isArchived={false}
							/>
						)}
					</div>
					<div className={classNames(classes["closable-left-side"])}>
						<Image alt="open side menu" src={ChevronIcon} className={classes["chevron-icon"]} onClick={this.onOpenLeftSide} />
					</div>

					<div className={classes["right-side"]}>
						{this.props.hasBackArrow && (
							<div className={classes["back-arrow-desktop"]}>
								<BackArrow url={this.props.backArrowUrl ?? ""} />
							</div>
						)}
						{this.props.mobileBackText && (
							<div className={classes["back-arrow-mobile"]}>
								<Button
									leftIcon={<ChevronLeftIcon />}
									variant={EButtonVariant.PRIMARY}
									styletype={EButtonstyletype.TEXT}
									onClick={this.onOpenLeftSide}>
									{this.props.mobileBackText ?? "Retour"}
								</Button>
							</div>
						)}
						{this.props.children}
					</div>
					{this.props.image && (
						<div className={classes["background-image-container"]}>
							<Image alt={"right side image"} src={this.props.image} className={classes["background-image"]} priority />
						</div>
					)}
				</div>
				<Version />
			</div>
		);
	}

	public override async componentDidMount() {
		this.onWindowResize = WindowStore.getInstance().onResize((window) => this.onResize(window));
		let targetedStatus: EFolderStatus = EFolderStatus["LIVE" as keyof typeof EFolderStatus];
		if (this.props.isArchived) targetedStatus = EFolderStatus.ARCHIVED;
		const query: IGetFoldersParams = {
			q: {
				where: { status: targetedStatus },
				include: {
					deed: { include: { deed_type: true } },
					office: true,
					customers: {
						include: {
							contact: true,
							documents: {
								include: {
									folder: true,
									document_type: true,
									files: true,
								},
							},
						},
					},
					documents: {
						include: {
							depositor: {
								include: {
									contact: true,
								},
							},
						},
					},
				},
			},
		};

		const folders = await Folders.getInstance().get(query);
		if (folders.length > 0)
			this.props.isArchived
				? router.push(
						Module.getInstance()
							.get()
							.modules.pages.Folder.pages.FolderArchived.pages.FolderInformation.props.path.replace(
								"[folderUid]",
								folders[0]?.uid ?? "",
							),
				  )
				: router.push(
						Module.getInstance()
							.get()
							.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", folders[0]?.uid ?? ""),
				  );
		this.setState({ folders: folders });
	}
	public override componentWillUnmount() {
		this.onWindowResize();
	}

	private onOpenLeftSide() {
		this.setState({ isLeftSideOpen: true });
	}

	private onCloseLeftSide() {
		if (!this.state.leftSideCanBeClosed) return;
		this.setState({ isLeftSideOpen: false });
	}

	private onResize(window: Window) {
		if (window.innerWidth > 1023) {
			if (!this.state.leftSideCanBeClosed) return;
			this.setState({ leftSideCanBeClosed: false });
		}
		this.setState({ leftSideCanBeClosed: true });
	}
}
