// ignore_for_file: prefer_final_fields, file_names

import 'dart:async';
import 'dart:developer';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:traderscircuit/resources/routes/route_name.dart';

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

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

class _SplashScreenState extends State<SplashScreen>
    with TickerProviderStateMixin {
  late AnimationController _scaleController;
  late Animation<double> _scaleAnimation;
  late AnimationController _slideController;
  var _connectionStatus;

  Future<void> checkInternet() async {
    final connectivityResult = await (Connectivity().checkConnectivity());

    if (connectivityResult == ConnectivityResult.wifi ||
        connectivityResult == ConnectivityResult.mobile) {
      setState(() {
        _connectionStatus = connectivityResult;
      });
    } else {
      setState(() {
        _connectionStatus = connectivityResult;

        //  Navigator.pushReplacementNamed(context, "/noInternet");
      });
    }
  }

  @override
  void initState() {
    super.initState();
    checkInternet();
    log(_connectionStatus.toString());
    Future.delayed(Duration(seconds: 2), () async {
      Get.toNamed(RouteName.sliderscreen1);
      // if (_connectionStatus == ConnectivityResult.none) {
      //   var result = await Get.to(NoInternet());
      //   if (result != null && result) {
      //     Timer(const Duration(seconds: 1), () async {
      //       SharedPreferences prefs = await SharedPreferences.getInstance();
      //       token = prefs.getString('token');
      //     myusername = prefs.getString('name');
      //     phonenumber = prefs.getString('contact_number');
      //       OnBoard = prefs.getBool('OnBoard') ?? false;
      //       if (OnBoard == false) {
      //         Get.toNamed(RouteName.sliderscreen1);
      //       } else {
      //         if (token == null || token!.isEmpty) {
      //           Get.offAndToNamed(RouteName.loginScreen);
      //         } else {
      //           GetProfile().GetProfileAPI().then((value) {
      //             Get.toNamed(RouteName.mainScreen, arguments: 0);
      //           });
      //         }
      //       }
      //     });
      //   }
      // } else {
      //   Timer(const Duration(seconds: 2), () async {
      //     SharedPreferences prefs = await SharedPreferences.getInstance();

      //     token = prefs.getString('token');
      //     myusername = prefs.getString('name');
      //     phonenumber = prefs.getString('contact_number');
      //     OnBoard = prefs.getBool('OnBoard') ?? false;
      //     if (OnBoard == false) {
      //       Get.toNamed(RouteName.sliderscreen1);
      //     } else {
      //       if (token == null || token!.isEmpty) {
      //         Get.offAndToNamed(RouteName.loginScreen);
      //       } else {
      //         GetProfile().GetProfileAPI().then((value) {
      //           Get.toNamed(RouteName.mainScreen, arguments: 0);
      //         });
      //       }
      //     }
      //   });
      //   }
    });
    // for scaleTansition
    _scaleController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );
    _scaleAnimation =
        Tween<double>(begin: 0.0, end: 1.0).animate(_scaleController);
    _scaleController.forward();
    // for slideTansition
    _slideController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );
    Timer(const Duration(seconds: 3), () {
      _slideController.forward();
    });
  }

  @override
  void dispose() {
    _scaleController.dispose();
    _slideController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Center(
          child: Padding(
            padding: EdgeInsets.all(16.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ScaleTransition(
                  scale: _scaleAnimation,
                  child: Text(
                    "Trader\'s Circuit",
                    style: TextStyle(
                        fontSize: 50,
                        fontWeight: FontWeight.w600,
                        color: Colors.white),
                  ),
                  // Image.asset(
                  //   'assets/images/logo.png',
                  //   width: 200,
                  //   height: 200,
                  // ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
