import React, { useEffect } from "react";

import { useRouter } from "next/router";
import Module from "@Front/Config/Module";
import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import DefaultDashboardWithList, { IPropsDashboardWithList } from "../DefaultDashboardWithList";

import { v4 as uuidv4 } from 'uuid';

import RoleService from "src/common/Api/LeCoffreApi/sdk/RoleService";

type IProps = IPropsDashboardWithList;

export default function DefaultRoleDashboard(props: IProps) {
	const [roles, setRoles] = React.useState<any[] | null>(null);
	const router = useRouter();
	const { roleUid } = router.query;
	useEffect(() => {
		/* TODO: review
		const query: IGetRolesParams = {
			include: { rules: true },
		};

		OfficeRoles.getInstance()
			.get(query)
			.then((roles) => setRoles(roles));
			*/

		const roles: any[] = [
			{
				uid: uuidv4(),
				name: 'Notaire',
				office: {
					name: '',
					crpcen: '',
					created_at: new Date(),
					updated_at: new Date()
				},
				rules: [
					{
						uid: uuidv4(),
						name: "Gestion de l'abonnement",
						label: "Gestion de l'abonnement",
						namespace: "Gestion de l'abonnement",
						created_at: new Date(),
						updated_at: new Date()
					},
					{
						uid: uuidv4(),
						name: "Gestion des matrices d'actes et des documents",
						label: "Gestion des matrices d'actes et des documents",
						namespace: "Gestion des matrices d'actes et des documents",
						created_at: new Date(),
						updated_at: new Date()
					},
					{
						uid: uuidv4(),
						name: "Intégration du RIB",
						label: "Intégration du RIB",
						namespace: "Intégration du RIB",
						created_at: new Date(),
						updated_at: new Date()
					}
				],
				created_at: new Date(),
				updated_at: new Date(),
			},
			{
				uid: uuidv4(),
				name: 'Collaborateur',
				office: {
					name: '',
					crpcen: '',
					created_at: new Date(),
					updated_at: new Date()
				},
				rules: [
					{
						uid: uuidv4(),
						name: "Gestion de l'abonnement",
						label: "Gestion de l'abonnement",
						namespace: "Gestion de l'abonnement",
						created_at: new Date(),
						updated_at: new Date()
					},
					{
						uid: uuidv4(),
						name: "Gestion des matrices d'actes et des documents",
						label: "Gestion des matrices d'actes et des documents",
						namespace: "Gestion des matrices d'actes et des documents",
						created_at: new Date(),
						updated_at: new Date()
					},
					{
						uid: uuidv4(),
						name: "Intégration du RIB",
						label: "Intégration du RIB",
						namespace: "Intégration du RIB",
						created_at: new Date(),
						updated_at: new Date()
					}
				],
				created_at: new Date(),
				updated_at: new Date(),
			}
		];

		RoleService.getRoles().then(async (processes: any[]) => {
			if (processes.length > 0) {
				const roles: any[] = processes.map((process: any) => process.processData);
				setRoles(roles);
			} else {
				/*
				for (let role of roles) {
					const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0';

					await RoleService.createRole(role, validatorId);
				}
				setRoles(roles);
				*/
			}
		});
	}, []);

	const onSelectedBlock = (block: IBlock) => {
		router.push(Module.getInstance().get().modules.pages.Roles.pages.RolesInformations.props.path.replace("[uid]", block.id));
	};

	return (
		<DefaultDashboardWithList
			{...props}
			onSelectedBlock={onSelectedBlock}
			blocks={
				roles
					? roles.map((role) => ({
						id: role.uid!,
						primaryText: role.label,
						isActive: role.uid === roleUid,
					}))
					: []
			}
			bottomButton={{
				link: Module.getInstance().get().modules.pages.Roles.pages.Create.props.path,
				text: "Créer un rôle",
			}}
		/>
	);
}
