import React, { useContext, useEffect, useState } from 'react';
import getServers, { Context as ServersContext, Filters } from '@/api/admin/servers/getServers';
import AdminCheckbox from '@/components/admin/AdminCheckbox';
import AdminTable, {
    ContentWrapper,
    Loading,
    NoItems,
    Pagination,
    TableBody,
    TableHead,
    TableHeader,
    useTableHooks
} from '@/components/admin/AdminTable';
import Button from '@/components/elements/Button';
import CopyOnClick from '@/components/elements/CopyOnClick';
import FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { AdminContext } from '@/state/admin';
import { NavLink, useRouteMatch } from 'react-router-dom';
import tw from 'twin.macro';
import AdminContentBlock from '@/components/admin/AdminContentBlock';

const RowCheckbox = ({ id }: { id: number }) => {
    const isChecked = AdminContext.useStoreState(state => state.servers.selectedServers.indexOf(id) >= 0);
    const appendSelectedServer = AdminContext.useStoreActions(actions => actions.servers.appendSelectedServer);
    const removeSelectedServer = AdminContext.useStoreActions(actions => actions.servers.removeSelectedServer);

    return (
        <AdminCheckbox
            name={id.toString()}
            checked={isChecked}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
                if (e.currentTarget.checked) {
                    appendSelectedServer(id);
                } else {
                    removeSelectedServer(id);
                }
            }}
        />
    );
};

const ServersContainer = () => {
    const match = useRouteMatch();

    const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(ServersContext);
    const { clearFlashes, clearAndAddHttpError } = useFlash();
    const { data: servers, error, isValidating } = getServers([ 'node', 'user' ]);

    const length = servers?.items?.length || 0;

    const setSelectedServers = AdminContext.useStoreActions(actions => actions.servers.setSelectedServers);
    const selectedServerLength = AdminContext.useStoreState(state => state.servers.selectedServers.length);

    const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
        setSelectedServers(e.currentTarget.checked ? (servers?.items?.map(server => server.id) || []) : []);
    };

    const onSearch = (query: string): Promise<void> => {
        return new Promise((resolve) => {
            if (query.length < 2) {
                setFilters(null);
            } else {
                setFilters({ name: query });
            }
            return resolve();
        });
    };

    useEffect(() => {
        setSelectedServers([]);
    }, [ page ]);

    useEffect(() => {
        if (!error) {
            clearFlashes('servers');
            return;
        }

        clearAndAddHttpError({ key: 'servers', error });
    }, [ error ]);

    return (
        <AdminContentBlock title={'Servers'}>
            <div css={tw`w-full flex flex-row items-center mb-8`}>
                <div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
                    <h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Servers</h2>
                    <p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>All servers available on the system.</p>
                </div>

                <div css={tw`flex ml-auto pl-4`}>
                    <NavLink to={`${match.url}/new`}>
                        <Button type={'button'} size={'large'} css={tw`h-10 px-4 py-0 whitespace-nowrap`}>
                            New Server
                        </Button>
                    </NavLink>
                </div>
            </div>

            <FlashMessageRender byKey={'servers'} css={tw`mb-4`}/>

            <AdminTable>
                <ContentWrapper
                    checked={selectedServerLength === (length === 0 ? -1 : length)}
                    onSelectAllClick={onSelectAllClick}
                    onSearch={onSearch}
                >
                    <Pagination data={servers} onPageSelect={setPage}>
                        <div css={tw`overflow-x-auto`}>
                            <table css={tw`w-full table-auto`}>
                                <TableHead>
                                    <TableHeader name={'Identifier'} direction={sort === 'uuidShort' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('uuidShort')}/>
                                    <TableHeader name={'Name'} direction={sort === 'name' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('name')}/>
                                    <TableHeader name={'Owner'} direction={sort === 'owner_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('owner_id')}/>
                                    <TableHeader name={'Node'} direction={sort === 'node_id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('node_id')}/>
                                    <TableHeader name={'Status'} direction={sort === 'status' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('status')}/>
                                </TableHead>

                                <TableBody>
                                    { servers !== undefined && !error && !isValidating && length > 0 &&
                                        servers.items.map(server => (
                                            <tr key={server.id} css={tw`h-14 hover:bg-neutral-600`}>
                                                <td css={tw`pl-6`}>
                                                    <RowCheckbox id={server.id}/>
                                                </td>

                                                <td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
                                                    <CopyOnClick text={server.identifier}>
                                                        <code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{server.identifier}</code>
                                                    </CopyOnClick>
                                                </td>

                                                <td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
                                                    <NavLink to={`${match.url}/${server.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
                                                        {server.name}
                                                    </NavLink>
                                                </td>

                                                {/* TODO: Have permission check for displaying user information. */}
                                                <td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
                                                    <NavLink to={`/admin/users/${server.relations.user?.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
                                                        <div css={tw`text-sm text-neutral-200`}>
                                                            {server.relations.user?.firstName} {server.relations.user?.lastName}
                                                        </div>

                                                        <div css={tw`text-sm text-neutral-400`}>
                                                            {server.relations.user?.email}
                                                        </div>
                                                    </NavLink>
                                                </td>

                                                {/* TODO: Have permission check for displaying node information. */}
                                                <td css={tw`px-6 text-sm text-left whitespace-nowrap`}>
                                                    <NavLink to={`/admin/nodes/${server.relations.node?.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
                                                        <div css={tw`text-sm text-neutral-200`}>
                                                            {server.relations.node?.name}
                                                        </div>

                                                        <div css={tw`text-sm text-neutral-400`}>
                                                            {server.relations.node?.fqdn}
                                                        </div>
                                                    </NavLink>
                                                </td>

                                                <td css={tw`px-6 whitespace-nowrap`}>
                                                    {server.status === 'installing' ?
                                                        <span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}>
                                                            Installing
                                                        </span>
                                                        :
                                                        server.status === 'transferring' ?
                                                            <span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-yellow-200 text-yellow-800`}>
                                                                Transferring
                                                            </span>
                                                            : server.status === 'suspended' ?
                                                                <span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-red-200 text-red-800`}>
                                                                    Suspended
                                                                </span>
                                                                :
                                                                <span css={tw`px-2 inline-flex text-xs leading-5 font-medium rounded-full bg-green-100 text-green-800`}>
                                                                    Active
                                                                </span>
                                                    }
                                                </td>
                                            </tr>
                                        ))
                                    }
                                </TableBody>
                            </table>

                            { servers === undefined || (error && isValidating) ?
                                <Loading/>
                                :
                                length < 1 ?
                                    <NoItems/>
                                    :
                                    null
                            }
                        </div>
                    </Pagination>
                </ContentWrapper>
            </AdminTable>
        </AdminContentBlock>
    );
};

export default () => {
    const hooks = useTableHooks<Filters>();

    return (
        <ServersContext.Provider value={hooks}>
            <ServersContainer/>
        </ServersContext.Provider>
    );
};
