/*
 * Vencord, a Discord client mod
 * Copyright (c) 2024 Vendicated and contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

import { Alerts, Button, ChannelStore } from "@webpack/common";

import { CLONE_LINK_REGEX, Native, plugins } from ".";

const WHITELISTED_SHARE_CHANNELS = ["1256395889354997771", "1032200195582197831", "1301947896601509900", "1322935137591365683"];

export default function UserpluginInstallButton({ props }: any) {
    const { message } = props;
    if (!WHITELISTED_SHARE_CHANNELS.includes(ChannelStore.getChannel(message.channel_id).parent_id) && !WHITELISTED_SHARE_CHANNELS.includes(message.channel_id))
        return;
    const gitLink = (props.message.content as string).match(CLONE_LINK_REGEX);
    if (!gitLink) return;
    const installed = plugins.includes(gitLink[3]);
    return <>
        <div style={{ display: "flex" }}>
            <Button style={{
                marginTop: "5px"
            }}
                onClick={async () => {
                    try {
                        const pluginToEnable = await Native.initPluginInstall(gitLink[0], gitLink[1], gitLink[2], gitLink[3]);
                        Alerts.show({
                            title: "Done!",
                            body: `${pluginToEnable} has been successfully installed. What now?`,
                            confirmText: "Enable & refresh",
                            cancelText: "Refresh",
                            secondaryConfirmText: "I'll refresh later",
                            onConfirm() {
                                !Vencord.Plugins.plugins[pluginToEnable] ? Vencord.Settings.plugins[pluginToEnable] = { enabled: true } : Vencord.Settings.plugins[pluginToEnable].enabled = true;
                                window.location.reload();
                            },
                            onCancel: window.location.reload
                        });
                    }
                    catch (e: any) {
                        if (e.toString().includes("silentStop")) return;
                        Alerts.show({
                            title: "Install error",
                            body: e.toString()
                        });
                    }
                }}>
                {installed ? "Update" : "Install"} plugin
            </Button>
            {
                installed && <Button style={{
                    marginTop: "5px",
                    marginLeft: "10px"
                }}
                    color={Button.Colors.RED}
                    onClick={async () => {
                        try {
                            await Native.rmPlugin(gitLink[3]);
                            window.location.reload();
                        }
                        catch (e: any) {
                            if (e.toString().includes("silentStop")) return;
                            Alerts.show({
                                title: "Uninstall error",
                                body: e.toString()
                            });
                        }

                    }}>
                    Uninstall plugin
                </Button>
            }
        </div>
    </>;
}
