import { lazy, Suspense } from 'react';

import { useAlbumArtistListFilters } from '/@/renderer/features/artists/hooks/use-album-artist-list-filters';
import { ItemListSettings, useCurrentServer, useListSettings } from '/@/renderer/store';
import { Spinner } from '/@/shared/components/spinner/spinner';
import { ItemListKey, ListDisplayType, ListPaginationType } from '/@/shared/types/types';

const AlbumArtistListInfiniteGrid = lazy(() =>
    import('/@/renderer/features/artists/components/album-artist-list-infinite-grid').then(
        (module) => ({
            default: module.AlbumArtistListInfiniteGrid,
        }),
    ),
);

const AlbumArtistListPaginatedGrid = lazy(() =>
    import('/@/renderer/features/artists/components/album-artist-list-paginated-grid').then(
        (module) => ({
            default: module.AlbumArtistListPaginatedGrid,
        }),
    ),
);

const AlbumArtistListInfiniteTable = lazy(() =>
    import('/@/renderer/features/artists/components/album-artist-list-infinite-table').then(
        (module) => ({
            default: module.AlbumArtistListInfiniteTable,
        }),
    ),
);

const AlbumArtistListPaginatedTable = lazy(() =>
    import('/@/renderer/features/artists/components/album-artist-list-paginated-table').then(
        (module) => ({
            default: module.AlbumArtistListPaginatedTable,
        }),
    ),
);

export const AlbumArtistListContent = () => {
    const { display, grid, itemsPerPage, pagination, table } = useListSettings(
        ItemListKey.ALBUM_ARTIST,
    );

    return (
        <Suspense fallback={<Spinner container />}>
            <AlbumArtistListView
                display={display}
                grid={grid}
                itemsPerPage={itemsPerPage}
                pagination={pagination}
                table={table}
            />
        </Suspense>
    );
};

export const AlbumArtistListView = ({
    display,
    grid,
    itemsPerPage,
    pagination,
    table,
}: ItemListSettings) => {
    const server = useCurrentServer();

    const { query } = useAlbumArtistListFilters();

    switch (display) {
        case ListDisplayType.GRID: {
            switch (pagination) {
                case ListPaginationType.INFINITE: {
                    return (
                        <AlbumArtistListInfiniteGrid
                            gap={grid.itemGap}
                            itemsPerPage={itemsPerPage}
                            itemsPerRow={grid.itemsPerRowEnabled ? grid.itemsPerRow : undefined}
                            query={query}
                            serverId={server.id}
                        />
                    );
                }
                case ListPaginationType.PAGINATED: {
                    return (
                        <AlbumArtistListPaginatedGrid
                            gap={grid.itemGap}
                            itemsPerPage={itemsPerPage}
                            itemsPerRow={grid.itemsPerRowEnabled ? grid.itemsPerRow : undefined}
                            query={query}
                            serverId={server.id}
                        />
                    );
                }
                default:
                    return null;
            }
        }
        case ListDisplayType.TABLE: {
            switch (pagination) {
                case ListPaginationType.INFINITE: {
                    return (
                        <AlbumArtistListInfiniteTable
                            autoFitColumns={table.autoFitColumns}
                            columns={table.columns}
                            enableAlternateRowColors={table.enableAlternateRowColors}
                            enableHorizontalBorders={table.enableHorizontalBorders}
                            enableRowHoverHighlight={table.enableRowHoverHighlight}
                            enableVerticalBorders={table.enableVerticalBorders}
                            itemsPerPage={itemsPerPage}
                            query={query}
                            serverId={server.id}
                            size={table.size}
                        />
                    );
                }
                case ListPaginationType.PAGINATED: {
                    return (
                        <AlbumArtistListPaginatedTable
                            autoFitColumns={table.autoFitColumns}
                            columns={table.columns}
                            enableAlternateRowColors={table.enableAlternateRowColors}
                            enableHorizontalBorders={table.enableHorizontalBorders}
                            enableRowHoverHighlight={table.enableRowHoverHighlight}
                            enableVerticalBorders={table.enableVerticalBorders}
                            itemsPerPage={itemsPerPage}
                            query={query}
                            serverId={server.id}
                            size={table.size}
                        />
                    );
                }
                default:
                    return null;
            }
        }
    }

    return null;
};
