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

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

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

  @override
  State<TabletSplashPage> createState() => _TabletSplashPageState();
}

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

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

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

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