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 { OfficeFolder } from "le-coffre-resources/dist/Customer";
import Link from "next/link";
import { NextRouter, useRouter } from "next/router";

import BasePage from "../../Base";
import classes from "./classes.module.scss";
import Customer from "le-coffre-resources/dist/Customer";
import Note from "le-coffre-resources/dist/Customer/Note";
import Folders from "@Front/Api/LeCoffreApi/Customer/Folders/Folders";
import Notes from "@Front/Api/LeCoffreApi/Customer/Notes/Notes";
import Customers from "@Front/Api/LeCoffreApi/Notary/Customers/Customers";

type IProps = {};

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

type IState = {
	folder: OfficeFolder | null;
	customer: Customer | null;
	note: Note | null;
};
class CreateCustomerNoteClass extends BasePage<IPropsClass, IState> {
	private backwardPath = Module.getInstance()
		.get()
		.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.folderUid!);
	constructor(props: IPropsClass) {
		super(props);
		this.state = {
			folder: null,
			customer: null,
			note: null,
		};
		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.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.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() {
		// const note = await Notes.getInstance().getByUid(this.props.noteUid, query);
		const folder = await Folders.getInstance().getByUid(this.props.folderUid, { note: true });
		const customer = await Customers.getInstance().getByUid(this.props.customerUid);

		//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, folder: note.office_folder || null, customer: note.customer || null });
		this.setState({ note: null, folder: folder, customer: customer });
	}

	private async onFormSubmit(e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) {
		try {
			if (!this.state.folder || !this.state.customer) {
				throw new Error("Folder or customer not found");
			}
			const note = {
				content: values["content"],
				folder: this.state.folder,
				customer: this.state.customer,
			};

			await Notes.getInstance().post(note);
			this.props.router.push(this.backwardPath);
		} catch (error) {
			console.error(error);
		}
	}
}

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

	folderUid = folderUid as string;
	customerUid = customerUid as string;

	return <CreateCustomerNoteClass folderUid={folderUid} customerUid={customerUid} router={router} />;
}
