import 'dart:async';

import 'package:sefer_driver/controller/auth/captin/login_captin_controller.dart';
import 'package:sefer_driver/views/home/on_boarding_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../constant/box_name.dart';
import '../../main.dart';
import '../../print.dart';
import '../../views/auth/captin/login_captin.dart';

class SplashScreenController extends GetxController
    with GetTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> animation;
  final progress = 0.0.obs;
  Timer? _progressTimer;

  String packageInfo = '';

  Future<void> _getPackageInfo() async {
    final info = await PackageInfo.fromPlatform();
    packageInfo = info.version;
    box.write(BoxName.packagInfo, packageInfo);
    update();
  }

  @override
  void onInit() {
    super.onInit();
    _getPackageInfo();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500), // Reduced duration
    )..forward();

    animation =
        CurvedAnimation(parent: _animationController, curve: Curves.easeOut);

    startTimer();
    _startProgressTimer();
  }

  void _startProgressTimer() {
    Log.print(
        'box.read(BoxName.phoneDriver): ${box.read(BoxName.phoneDriver)}');
    Log.print(
        'box.read(BoxName.phoneVerified): ${box.read(BoxName.phoneVerified)}');
    const totalTime = 3000; // 5 seconds in milliseconds
    const interval = 50; // Update every 50ms
    int elapsed = 0;

    _progressTimer =
        Timer.periodic(const Duration(milliseconds: interval), (timer) async {
      elapsed += interval;
      progress.value = (elapsed / totalTime).clamp(0.0, 1.0);

      if (elapsed >= totalTime) {
        timer.cancel();
        // Get.off(SyrianCardAI());
        box.read(BoxName.onBoarding) == null
            ? Get.off(() => OnBoardingPage())
            : box.read(BoxName.phoneDriver) != null &&
                    box.read(BoxName.phoneVerified).toString() == '1'
                ? await Get.put(LoginDriverController())
                    .loginWithGoogleCredential(
                        box.read(BoxName.driverID).toString(),
                        box.read(BoxName.emailDriver).toString())
                : Get.off(() => LoginCaptin());
      }
    });
  }

  void startTimer() async {
    Timer(const Duration(seconds: 5), () async {
      // box.read(BoxName.onBoarding) == null
      //     ? Get.off(() => OnBoardingPage())
      //     : box.read(BoxName.email) != null &&
      //             box.read(BoxName.phone) != null &&
      //             box.read(BoxName.isVerified) == '1'
      //         // ? Get.off(() => const MapPagePassenger())
      //         ? await Get.put(LoginController()).loginUsingCredentials(
      //             box.read(BoxName.passengerID).toString(),
      //             box.read(BoxName.email).toString(),
      //           )
      //         : Get.off(() => LoginPage());
    });
  }

  @override
  void onClose() {
    _progressTimer?.cancel();
    _animationController.dispose();
    super.onClose();
  }
}
