import 'package:cheminova/constants/constant.dart';
import 'package:cheminova/screens/login_screen.dart';
import 'package:cheminova/services/secure__storage_service.dart';
import 'package:flutter/material.dart';

import 'home_screen.dart';

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

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

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    // Delay for 3 seconds to show the splash screen before checking user authentication
    Future.delayed(const Duration(seconds: 3), () {
      // Read the access token from secure storage
      SecureStorageService().read(key: 'access_token').then((value) {
        // Check if the access token exists
        if (value != null) {
          // If the token exists, navigate to the HomePage
          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (context) => const HomePage()));
        } else {
          // If the token doesn't exist, navigate to the LoginPage
          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (context) => const LoginPage()));
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white, // Background color of the splash screen
      body: Container(
        width: MediaQuery.sizeOf(context).width, // Set width to full screen width
        height: MediaQuery.sizeOf(context).height, // Set height to full screen height
        padding: const EdgeInsets.only(top: 110, bottom: 50), // Padding for the content
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween, // Space between children
          children: <Widget>[
            Column(
              children: [
                Image.asset('assets/cheminova_logo.png'), // App logo
                const Text(
                  'HELPING YOU GROW', // Tagline text
                  style: TextStyle(
                    color: Colors.black,
                    fontFamily: robotoFamily,
                    fontSize: 20,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
            Image.asset('assets/plant.png'), // Image displayed at the center
            const Column(
              children: [
                Text(
                  'Powered By', // Label indicating the provider
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 12,
                    fontFamily: robotoFamily,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Text(
                  'codeology.solutions', // Company name
                  style: TextStyle(
                    color: Colors.black,
                    fontFamily: robotoFamily,
                    fontSize: 14,
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
