import type { HostingProvider, ProviderType } from "@site/src/types/providers";
import ReactMarkdown from "react-markdown";
import Admonition from '@theme/Admonition';
import React, { useState } from 'react';
import Translate, { translate } from "@docusaurus/Translate";
import { providersData } from "../data/providers";

export const noP = (props: { children: any; }) => {
    const { children } = props;
    return children;
}

export const Provider = ({ type }) => {
    const hostingProviders: HostingProvider[] = providersData[type as ProviderType]

    return (
        <table>
            <thead>
                <tr>
                    <th>Provider</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                {hostingProviders.map((provider: HostingProvider) => (
                    <tr>
                        <td>
                            <a href={provider.url}>{provider.name}</a>
                        </td>
                        <td>
                            {provider.description != null ? (
                                <ReactMarkdown children={provider.description} components={{ p: noP }} />
                            ) : (
                                ''
                            )}
                        </td>
                    </tr>
                ))}
            </tbody>
        </table>
    )
}

export const ProviderSelector = () => {
    const providers: HostingProvider[] = [
        ...Object.values(providersData.built_in),
        ...Object.values(providersData.support),
        ...Object.values(providersData.no_support)
    ].flat().sort((a, b) => a.name.localeCompare(b.name));

    providers.unshift({
        name: 'Not listed',
        description: translate({
            id: 'providers.provider.not_listed.description',
            message: "If your hosting provider is not listed, try enabling the `clone-remote-port` option in the config. Then, restart the server, and try connecting with the same IP and port as on Java Edition. <br> If this does not work, ask your server hosting provider for a UDP port, and use that. For VPS/KVM servers please follow the self-hosting steps."
        })
    } as HostingProvider);


    const [selectedProvider, setSelectedProvider] = useState(null);

    const handleSelectionChange = (event) => {
        const selectedName = event.target.value;
        const provider = providers.find(p => p.name === selectedName);
        setSelectedProvider(provider);
    }

    return (
        <div className="host-select">
            <select onChange={handleSelectionChange}>
                <option value="none">Select a provider</option>
                {providers.map((provider) => (
                    <option key={provider.name} value={provider.name}>
                        {provider.name}
                    </option>
                ))}
            </select>
            <Admonition type="tip" title={<Translate id='components.provider.instructions'>Provider Instructions</Translate>}>
                {selectedProvider ? (
                    <ReactMarkdown>{selectedProvider.description}</ReactMarkdown>
                ) : (
                    <p>
                        <Translate id='components.provider.select'>Select a provider to see specific installation instructions</Translate>
                    </p>
                )}
            </Admonition>
        </div>
    );
}
