import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/components/extensions/state.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/router.gr.dart';
import 'package:remever/services/auth/auth_interface.dart';

@RoutePage()
class SplashScreen extends StatefulWidget {
  ///
  /// Стартовый экран
  ///
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

///
/// Состояние экрана
///
class _SplashScreenState extends State<SplashScreen> {
  /// Сервисы
  final AuthInterface _authIf = GetIt.I.get<AuthInterface>();

  /// Запуск анимации лого
  bool _launchLogo = false;

  ///
  /// Навигация на основной экран
  ///
  void _navigate() async {
    // context.replaceRoute(const HomeRoute());
    // return;

    final bool isAuth = await _authIf.isAuth;

    if (!mounted) return;

    context.replaceRoute(isAuth ? CollectionRoute() : const AuthRoute());
  }

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
      Future<void>.delayed(const Duration(milliseconds: 900), () {
        safeSetState(() => _launchLogo = !_launchLogo);
      });

      // Future<void>.delayed(const Duration(seconds: 4), _navigate);
      Future<void>.delayed(const Duration(seconds: 1), _navigate);
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg,
      body: SafeArea(
        child: Column(
          children: <Widget>[
            HSpace(148),
            AnimatedOpacity(
              opacity: _launchLogo ? 1 : 0,
              duration: const Duration(seconds: 3),
              child: Assets.images.logo.image(width: 308.w, height: 100.h),
            ),
          ],
        ),
      ),
    );
  }
}
