import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:keyboard/data/repository/account_repository.dart';
import 'package:keyboard/utils/toast_util.dart';
import '../../data/consts/constants.dart';
import '../../resource/assets.gen.dart';
import '../../dialog/login/login_dialog.dart';
import '../../module/new_user/new_user_page.dart';
import 'package:injectable/injectable.dart';

@injectable
class IntroController extends BaseController {
  final tag = "IntroController";

  Rx<PageController> pageController = PageController().obs;
  var currentPage = 0.obs;
  Timer? _autoPageTimer;

  IntroController();

  /// 页面数据列表
  final List<PageBean> pageList = [
    PageBean(
      title: Text.rich(
        textAlign: TextAlign.center,
        TextSpan(
          children: [
            TextSpan(
              text: '想知道\n',
              style: TextStyle(
                color: Colors.black.withAlpha(153),
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            TextSpan(
              text: 'Ta ',
              style: TextStyle(
                color: const Color(0xFF7D46FC),
                fontSize: 24.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
            TextSpan(
              text: '对你到底是‘朋友’还是‘心动’？',
              style: TextStyle(
                color: Colors.black.withAlpha(153),
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
      ),
      animUrl: Assets.anim.animIntroFirstData,
      stayDuration: const Duration(milliseconds:4100),
    ),
    PageBean(
      title: const SizedBox(),
      animUrl: Assets.anim.animIntroSecondData,
      stayDuration:const Duration(milliseconds: 5500)
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    setFirstIntro(false);
  }

  @override
  void onReady() {
    super.onReady();
    _startAutoSwitchTimer();
  }

  /// 开始自动切页计时
  void _startAutoSwitchTimer() {
    _autoPageTimer?.cancel();
    final duration = pageList[currentPage.value].stayDuration;
    _autoPageTimer = Timer(duration, swiperIntro);
  }

  /// 执行切页
  void swiperIntro() {
    if (currentPage.value == pageList.length - 1) {
      pageController.value.jumpToPage(0);
      currentPage.value = 0;
    } else {
      pageController.value.nextPage(
        duration: const Duration(milliseconds: 500),
        curve: Curves.easeInOut,
      );
      currentPage.value++;
    }
    _startAutoSwitchTimer(); // 切完后，重新根据新页面设定计时
  }

  /// 页面切换时回调
  void onPageChanged(int index) {
    currentPage.value = index;
    _startAutoSwitchTimer();
  }

  void clickLogin() {
    Timer(const Duration(milliseconds: 500), () {
      if (AccountRepository.getInstance().isLogin.value) {
        ToastUtil.show("您已登录~");
        return;
      }
      LoginDialog.show();
    });
  }

  void clickCustomButton() {
    Timer(const Duration(milliseconds: 500), () {
      NewUserPage.start();
    });
  }

  void clickBack() {
    if (GetPlatform.isAndroid) {
      SystemNavigator.pop();
    }
    exit(0);
  }

  @override
  void onClose() {
    _autoPageTimer?.cancel();
    super.onClose();
  }
}

class PageBean {
  final Widget title;
  final String animUrl;
  final Duration stayDuration; // 每页停留时间

  PageBean({
    required this.title,
    required this.animUrl,
    required this.stayDuration,
  });
}
