import { BaseForm } from './base-form';
import { useLogin, useTelegramConfirm, useTelegramLogin } from './hooks/telegram';
import { useRefreshToken } from './hooks/token';
import { ButtonLoading, ButtonLogin, ButtonTelegram, ButtonTelegramLogin } from './lib/buttons';
import { FormStateContext } from '@/context/form-state';
import { useContext } from 'react';

export function TelegramForm() {
  useRefreshToken();
  const { handleLogin } = useLogin();
  const { handleTelegramLogin } = useTelegramLogin();
  useTelegramConfirm();

  const {
    state: { step },
  } = useContext(FormStateContext);

  if (step === 'refresh-token') {
    return <ButtonLoading>Подождите...</ButtonLoading>;
  }

  if (step === 'login-success') {
    return (
      <BaseForm onSubmit={() => handleTelegramLogin()}>
        <ButtonTelegram>Войти через Telegram</ButtonTelegram>
      </BaseForm>
    );
  }

  if (step === 'telegram-notification') {
    return (
      <BaseForm onSubmit={() => {}}>
        <ButtonTelegramLogin>Ожидаем подтверждения...</ButtonTelegramLogin>
      </BaseForm>
    );
  }

  return (
    <BaseForm onSubmit={(data) => handleLogin(data)}>
      <ButtonLogin>Далее</ButtonLogin>
    </BaseForm>
  );
}
