import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/widgets/custom_background.dart';
import 'package:qadirneyriz/widgets/loading_widget.dart';

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

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

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    checkUser();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomBackground(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Spacer(),
            LoadingWidget(
              color: config.ui.mainGreen,
              size: 30,
            ),
          ],
        ),
      ),
    );
  }

  void checkUser() async {
    String token = setting.userLocalDb.getUser().token ?? '';

    Future.delayed(const Duration(seconds: 4), () {
      if (token != '') {
        context.goNamed('navigate', pathParameters: {'tab': '0'});
      } else {
        context.goNamed('login');
      }
    });
  }
}
