import { Route, Routes } from 'react-router-dom';
import { Introduction } from "./views/Introduction/Introduction";
import { CreateSeed } from './views/CreateSeed/CreateSeed';
import { Lockscreen } from './views/Lockscreen/Lockscreen';
import { ConfirmSeed } from './views/ConfirmSeed/ConfirmSeed';
import { SetPassword } from './views/SetPassword/SetPassword';
import { Main } from './views/Main/Main';
import { ExistsSeed } from './views/ExistsSeed/ExistsSeed';
import { Box } from '@mantine/core';
import './style.css'
import { Buffer } from 'buffer';
import { InformationProvider } from './providers/InformationProvider/InformationProvider';
import { BlacklistProvider } from './providers/BlacklistProvider/BlacklistProvider';
import { useAccountProvider } from './providers/AccountProvider/useAccountProvider';
import { ImageViwerProvider } from './providers/ImageViewerProvider/ImageViewerProvider';
import { AvatarProvider } from './providers/AvatarProvider/AvatarProvider';
import { Topbar } from './components/Topbar/Topbar';
import { ContextMenuProvider } from './providers/ContextMenuProvider/ContextMenuProvider';
import { SettingsProvider } from './providers/SettingsProvider/SettingsProvider';
import { DialogListProvider } from './providers/DialogListProvider/DialogListProvider';
import { DialogStateProvider } from './providers/DialogStateProvider.tsx/DialogStateProvider';
import { DeviceConfirm } from './views/DeviceConfirm/DeviceConfirm';
import { SystemAccountProvider } from './providers/SystemAccountsProvider/SystemAccountsProvider';
import { DeviceProvider } from './providers/DeviceProvider/DeviceProvider';
window.Buffer = Buffer;

export default function App() {
	const { allAccounts, accountProviderLoaded } = useAccountProvider();

	const getViewByLoginState = () => {
		if (!accountProviderLoaded) {
			return <></>
		}
		if (allAccounts.length <= 0) {
			/**
			 * Если аккаунтов нет
			 */
			return <Introduction />
		}
		if (allAccounts.length > 0) {
			/**
			 * Если есть аккаунт, но только один
			 */
			return <Lockscreen />
		}
		return <Introduction></Introduction>
	}

	return (
		<InformationProvider>
			<DialogStateProvider>
				<DeviceProvider>
					<DialogListProvider>
						<BlacklistProvider>
							<SettingsProvider>
								<SystemAccountProvider>
									<Box h={'100%'}>
										<Topbar></Topbar>
										<ContextMenuProvider>
											<ImageViwerProvider>
												<AvatarProvider>
													<Routes>
														<Route path="/" element={
															getViewByLoginState()
														} />
														<Route path="/create-seed" element={<CreateSeed />} />
														<Route path="/confirm-seed" element={<ConfirmSeed />} />
														<Route path="/set-password" element={<SetPassword />} />
														<Route path="/main/*" element={<Main />} />
														<Route path="/exists-seed" element={<ExistsSeed />} />
														<Route path="/deviceconfirm" element={<DeviceConfirm />} />
													</Routes>
												</AvatarProvider>
											</ImageViwerProvider>
										</ContextMenuProvider>
									</Box>
								</SystemAccountProvider>
							</SettingsProvider>
						</BlacklistProvider>
					</DialogListProvider>
				</DeviceProvider>
			</DialogStateProvider>
		</InformationProvider>
	);
}