import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/routes/app_routes.dart';
import 'package:tm_app/view_models/auth_view_model.dart';

import '../../../core/constants/assets.dart';

class DesktopSplashPage extends StatefulWidget {
  const DesktopSplashPage({super.key});

  @override
  State<DesktopSplashPage> createState() => _DesktopSplashPageState();
}

class _DesktopSplashPageState extends State<DesktopSplashPage> {
  late final AuthViewModel viewModel;
  @override
  void initState() {
    super.initState();
    viewModel = context.read<AuthViewModel>();
    Future.delayed(const Duration(seconds: 1), () async {
      await viewModel.checkIsLoggedIn();
    });
    viewModel.addListener(_viewModelListener);
  }

  @override
  void dispose() {
    viewModel.removeListener(_viewModelListener);
    super.dispose();
  }

  void _viewModelListener() {
    if (viewModel.isLoggedIn) {
      Router.neglect(context, () => const HomeRouteData().go(context));
    } else {
      Router.neglect(context, () => const LoginScreenRoute().go(context));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 100),
            child: Image.asset(AssetsManager.logoBigPng),
          ),
        ),
      ),
    );
  }
}
