import 'package:english_learning/features/auth/provider/user_provider.dart';
import 'package:english_learning/features/auth/screens/signin/signin_screen.dart';
import 'package:english_learning/features/home/screens/home_screen.dart';
import 'package:english_learning/features/welcome/screens/welcome_screen.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

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

class _SplashScreenState extends State<SplashScreen> {
  bool _showLogo = true;

  @override
  void initState() {
    super.initState();
    _checkLoginStatus();

    Future.delayed(const Duration(seconds: 2), () {
      setState(() {
        _showLogo = false;
      });
    });
  }

  Future<void> _checkLoginStatus() async {
    final userProvider = Provider.of<UserProvider>(context, listen: false);
    final prefs = await SharedPreferences.getInstance();

    // Mengecek apakah sudah login
    bool isLoggedIn = userProvider.isLoggedIn;
    bool isFirstInstall = prefs.getBool('isFirstInstall') ?? true;

    Future.delayed(const Duration(seconds: 4), () {
      if (isLoggedIn) {
        // Jika sudah login, arahkan ke HomeScreen
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (_) => const HomeScreen()),
        );
      } else if (isFirstInstall) {
        // Jika pertama kali install, tampilkan WelcomeScreen
        prefs.setBool('isFirstInstall', false);
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (_) => const WelcomeScreen()),
        );
      } else {
        // Jika sudah pernah install tetapi belum login, arahkan ke SigninScreen
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (_) => const SigninScreen()),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        decoration: const BoxDecoration(
          color: AppColors.blueColor,
          image: DecorationImage(
            image: AssetImage('lib/features/splash/assets/images/splash.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            AnimatedSwitcher(
              duration: const Duration(seconds: 1),
              child: _showLogo
                  ? Row(
                      key: const ValueKey('logo'),
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SvgPicture.asset(
                          'lib/features/splash/assets/images/Logo.svg',
                          width: 35,
                        ),
                        const SizedBox(
                          width: 3,
                        ),
                        Text(
                          'SEALS',
                          style: AppTextStyles.logoTextStyle,
                        ),
                      ],
                    )
                  : Text(
                      'Smart\nEnglish\nAdaptive\nLearning\nSystem',
                      key: const ValueKey('text'),
                      style: AppTextStyles.logoTextStyle,
                      textAlign: TextAlign.left,
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
