import ValidIcon from "@Assets/Icons/check-valid.svg";
import TrashIcon from "@Assets/Icons/trash.svg";
import classNames from "classnames";
import { Document } from "le-coffre-resources/dist/Customer";
import Image from "next/image";
import { NextRouter, useRouter } from "next/router";
import React from "react";

import Typography, { ITypo } from "../../Typography";
import WarningBadge from "../../WarningBadge";
import classes from "./classes.module.scss";
import { EDocumentStatus } from "le-coffre-resources/dist/Customer/Document";
import Rules, { RulesMode } from "@Front/Components/Elements/Rules";
import { AppRuleActions, AppRuleNames } from "@Front/Api/Entities/rule";

type IProps = {
	folderUid: string;
	document: {
		uid?: Document["uid"];
		document_type?: Document["document_type"];
		document_status: Document["document_status"];
		folder?: Document["folder"];
		files?: Document["files"];
	};
	openDeletionModal?: (uid: Document["uid"]) => void;
};

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

type IState = {};

class DocumentNotaryClass extends React.Component<IPropsClass, IState> {
	public constructor(props: IPropsClass) {
		super(props);
		this.onOpenDeletionModal = this.onOpenDeletionModal.bind(this);
		this.onClick = this.onClick.bind(this);
	}
	public override render(): JSX.Element {
		return (
			<div className={classNames(classes["root"], classes[this.props.document.document_status])} onClick={this.onClick}>
				<div>
					<Typography typo={ITypo.P_SB_16}>{this.props.document?.document_type?.name}</Typography>
					<Typography typo={ITypo.CAPTION_14}>{this.getDocumentsTitle()}</Typography>
				</div>
				{this.renderIcon()}
			</div>
		);
	}

	private getDocumentsTitle() {
		const documentFiles = this.props.document.files?.filter((file) => !file.archived_at);
		if (documentFiles) {
			if (
				documentFiles.length === 1 &&
				(this.props.document.document_status === EDocumentStatus.VALIDATED ||
					this.props.document.document_status === EDocumentStatus.DEPOSITED)
			) {
				const fileName = documentFiles[0]?.file_path?.split("/").pop();
				if (fileName && fileName.length > 20) {
					return `Nombre de documents : 1`;
				} else {
					return fileName;
				}
			} else {
				const archivedFilesLength = documentFiles.filter((file) => file.archived_at).length;
				const documentFileLength = documentFiles.length - archivedFilesLength;
				if (
					this.props.document.document_status === EDocumentStatus.ASKED ||
					this.props.document.document_status === EDocumentStatus.REFUSED
				) {
					return "Aucun document déposé";
				}

				return `Nombre de documents : ${documentFileLength}`;
			}
		} else {
			return "Aucun document déposé";
		}
	}

	private onClick() {
		if (
			this.props.document.document_status !== EDocumentStatus.VALIDATED &&
			this.props.document.document_status !== EDocumentStatus.DEPOSITED
		)
			return;
		this.props.router.push(`/folders/${this.props.folderUid}/documents/${this.props.document.uid}`);
	}

	private renderIcon(): JSX.Element {
		switch (this.props.document.document_status) {
			case "VALIDATED":
				return (
					<div className={classes["valid-radius"]}>
						<Image src={ValidIcon} alt="valid icon" />
					</div>
				);
			case EDocumentStatus.DEPOSITED:
				return <WarningBadge />;
			default:
				return (
					<Rules
						mode={RulesMode.NECESSARY}
						rules={[
							{
								action: AppRuleActions.delete,
								name: AppRuleNames.documents,
							},
						]}>
						<Image src={TrashIcon} alt="trash icon" className={classes["trash"]} onClick={this.onOpenDeletionModal} />
					</Rules>
				);
		}
	}

	private onOpenDeletionModal(): void {
		if (!this.props.openDeletionModal) return;
		this.props.openDeletionModal(this.props.document.uid ?? "");
	}
}

export default function DocumentNotary(props: IProps): JSX.Element {
	const router = useRouter();
	return <DocumentNotaryClass {...props} router={router} />;
}
