import Button, { EButtonstyletype, EButtonVariant } from "@Front/Components/DesignSystem/Button";
import Form from "@Front/Components/DesignSystem/Form";
import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField";
import Typography, { ETypo } from "@Front/Components/DesignSystem/Typography";
import BackArrow from "@Front/Components/Elements/BackArrow";
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import Link from "next/link";
import { NextRouter, useRouter } from "next/router";

import BasePage from "../../Base";
import classes from "./classes.module.scss";
import Note from "le-coffre-resources/dist/Customer/Note";

import NoteService from "src/common/Api/LeCoffreApi/sdk/NoteService";
import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService";

type IProps = {};

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

type IState = {
	note: Note | null;
	backwardPath: string;
};
class UpdateCustomerNoteClass extends BasePage<IPropsClass, IState> {
	constructor(props: IPropsClass) {
		super(props);
		this.state = {
			note: null,
			backwardPath: "",
		};
		this.onFormSubmit = this.onFormSubmit.bind(this);
	}
	public override render(): JSX.Element {
		return (
			<DefaultNotaryDashboard title={"Modifier la note client"}>
				<div className={classes["root"]}>
					<div className={classes["back-arrow"]}>
						<BackArrow url={this.state.backwardPath} />
					</div>
					<Typography typo={ETypo.TITLE_H1}>Modifier la note du client</Typography>

					<Form className={classes["form"]} onSubmit={this.onFormSubmit}>
						<div className={classes["content"]}>
							<TextAreaField
								name="content"
								placeholder="Note à l'attention du client"
								defaultValue={this.state.note?.content ?? ""}
							/>
						</div>

						<div className={classes["button-container"]}>
							<Link href={this.state.backwardPath} className={classes["cancel-button"]}>
								<Button variant={EButtonVariant.PRIMARY} styletype={EButtonstyletype.OUTLINED}>
									Annuler
								</Button>
							</Link>
							<Button type="submit">Enregistrer</Button>
						</div>
					</Form>
				</div>
			</DefaultNotaryDashboard>
		);
	}

	public override async componentDidMount() {
		LoaderService.getInstance().show();
		NoteService.getNoteByUid(this.props.noteUid).then((process: any) => {
			if (process) {
				const note: any = process.processData;

				// const folder = await Folders.getInstance().getByUid(this.props.folderUid, { note: true });
				//get the note of the folder that has customer_uid = this.props.customer.uid
				// const folderNote = folder.notes?.find((note) => note.customer?.uid === this.props.customerUid);
				this.setState({
					note,
					backwardPath: Module.getInstance()
						.get()
						.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", note.folder?.uid!),
				});

				LoaderService.getInstance().hide();
			}
		});
	}

	private async onFormSubmit(e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) {
		try {
			LoaderService.getInstance().show();
			NoteService.getNoteByUid(this.props.noteUid).then((process: any) => {
				if (process) {
					NoteService.updateNote(process, { content: values["content"] }).then(() => {
						this.props.router.push(this.state.backwardPath);
					});
				}
			});
		} catch (error) {
			console.error(error);
		}
	}
}

export default function UpdateCustomerNote(props: IProps) {
	const router = useRouter();
	let { noteUid } = router.query;

	noteUid = noteUid as string;

	return <UpdateCustomerNoteClass noteUid={noteUid} router={router} />;
}
