import { notify, useConfirmModal } from '@affine/component';
import { authAtom } from '@affine/core/atoms';
import { AuthService } from '@affine/core/modules/cloud';
import { WorkspaceSubPath } from '@affine/core/shared';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { Workspace } from '@toeverything/infra';
import {
  useLiveData,
  useService,
  WorkspacesService,
} from '@toeverything/infra';
import { useSetAtom } from 'jotai';
import { useCallback } from 'react';

import { useNavigateHelper } from '../use-navigate-helper';

interface ConfirmEnableCloudOptions {
  /**
   * Fired when the workspace is successfully enabled
   */
  onSuccess?: () => void;
  /**
   * Fired when workspace is successfully enabled or user cancels the operation
   */
  onFinished?: () => void;
  openPageId?: string;
}
type ConfirmEnableArgs = [Workspace, ConfirmEnableCloudOptions | undefined];

export const useEnableCloud = () => {
  const t = useAFFiNEI18N();
  const loginStatus = useLiveData(useService(AuthService).session.status$);
  const setAuthAtom = useSetAtom(authAtom);
  const { openConfirmModal, closeConfirmModal } = useConfirmModal();
  const workspacesService = useService(WorkspacesService);
  const { openPage } = useNavigateHelper();

  const enableCloud = useCallback(
    async (ws: Workspace | null, options?: ConfirmEnableCloudOptions) => {
      try {
        if (!ws) return;
        const { id: newId } = await workspacesService.transformLocalToCloud(ws);
        openPage(newId, options?.openPageId || WorkspaceSubPath.ALL);
        options?.onSuccess?.();
      } catch (e) {
        console.error(e);
        notify.error({
          title: t['com.affine.workspace.enable-cloud.failed'](),
        });
      }
    },
    [openPage, t, workspacesService]
  );

  const openSignIn = useCallback(() => {
    setAuthAtom(prev => ({ ...prev, openModal: true }));
  }, [setAuthAtom]);

  const signInOrEnableCloud = useCallback(
    async (...args: ConfirmEnableArgs) => {
      // not logged in, open login modal
      if (loginStatus === 'unauthenticated') {
        openSignIn();
      }

      if (loginStatus === 'authenticated') {
        await enableCloud(...args);
      }
    },
    [enableCloud, loginStatus, openSignIn]
  );

  const confirmEnableCloud = useCallback(
    (ws: Workspace, options?: ConfirmEnableCloudOptions) => {
      const { onSuccess, onFinished } = options ?? {};
      if (!runtimeConfig.enableCloud) return;

      const closeOnSuccess = () => {
        closeConfirmModal();
        onSuccess?.();
      };

      openConfirmModal(
        {
          title: t['Enable AFFiNE Cloud'](),
          description: t['Enable AFFiNE Cloud Description'](),
          cancelText: t['com.affine.enableAffineCloudModal.button.cancel'](),
          confirmButtonOptions: {
            type: 'primary',
            ['data-testid' as string]: 'confirm-enable-affine-cloud-button',
            children:
              loginStatus === 'authenticated'
                ? t['Enable']()
                : t['Sign in and Enable'](),
          },
          onConfirm: async () =>
            await signInOrEnableCloud(ws, {
              ...options,
              onSuccess: closeOnSuccess,
            }),
          onOpenChange: open => {
            if (!open) onFinished?.();
          },
        },
        {
          autoClose: false,
        }
      );
    },
    [closeConfirmModal, loginStatus, openConfirmModal, signInOrEnableCloud, t]
  );

  return confirmEnableCloud;
};
