'use client';

import { App, Button, Skeleton } from 'antd';
import { createStyles } from 'antd-style';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useFetchInstalledPlugins } from '@/hooks/useFetchInstalledPlugins';
import { useToolStore } from '@/store/tool';
import { pluginSelectors, pluginStoreSelectors } from '@/store/tool/selectors';

const useStyles = createStyles(({ css }) => ({
  button: css`
    background-color: #fff;
    border: 1px solid #2E62FF;
    border-radius: 20px;
    color: #2E62FF !important;
  `,
}));

const InstallPlugin = memo<{ identifier: string }>(({ identifier }) => {
  const { styles } = useStyles();
  const { t } = useTranslation(['discover', 'plugin']);
  const [loading, setLoading] = useState(false);
  const { modal } = App.useApp();
  const [useFetchPluginStore, installed, installing, installPlugin, unInstallPlugin] = useToolStore(
    (s) => [
      s.useFetchPluginStore,
      pluginSelectors.isPluginInstalled(identifier)(s),
      pluginStoreSelectors.isPluginInstallLoading(identifier)(s),
      s.installPlugin,
      s.uninstallPlugin,
    ],
  );

  const { isLoading } = useFetchPluginStore();
  const { isLoading: installedPluginLoading } = useFetchInstalledPlugins();
  const loadingState = installedPluginLoading || loading;

  const handleInstall = async () => {
    setLoading(true);
    await installPlugin(identifier);
    setLoading(false);
  };

  const handleUninstall = async () => {
    modal.confirm({
      centered: true,
      okButtonProps: { danger: true },
      onOk: async () => {
        setLoading(true);
        await unInstallPlugin(identifier);
        setLoading(false);
      },
      title: t('store.actions.confirmUninstall', { ns: 'plugin' }),
      type: 'error',
    });
  };

  if (isLoading) return <Skeleton.Button block size={'large'} />;

  return (
    <Button
      className={styles.button}
      loading={loadingState}
      onClick={() => {
        if (loading || installing) return;
        if (installed) {
          handleUninstall();
        } else {
          handleInstall();
        }
      }}
      size={'large'}
      style={{ flex: 1, width: 'unset' }}
    >
      {t(installed ? 'plugins.installed' : 'plugins.install')}
    </Button>
  );
});

export default InstallPlugin;
