import 'dart:async';

import 'package:clean/base/base_controller.dart';
import 'package:clean/data/consts/constants.dart';
import 'package:clean/module/main/main_view.dart';
import 'package:clean/resource/assets.gen.dart';
import 'package:clean/router/app_pages.dart';
import 'package:flutter/Material.dart';
import 'package:get/get.dart';

class IntroController extends BaseController {
  Rx<PageController> pageController = PageController().obs;
  var currentPage = 0.obs;

  final List<PageBean> pageList = [
    PageBean("One tap smart cleaning", Assets.anim.animIntroFirst),
    PageBean(
        "AI algorithm for duplicates \n detected", Assets.anim.animIntroSecond),
  ];

  Timer? _autoPageTimer;

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

  @override
  void onReady() {
    super.onReady();
    _autoPageTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
      swiperIntro();
    });
  }

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

  void swiperIntro() {
    if (currentPage.value == pageList.length - 1) {
      // 进入主页或其他操作

      Timer(const Duration(milliseconds: 500), () {
        MainTabPage.start();
        setFirstIntro(false);
      });
    } else {
      // 跳转到下一页
      pageController.value.nextPage(
        duration: const Duration(milliseconds: 500),
        curve: Curves.easeInOut,
      );
      currentPage.value++;
    }
  }

  void clickContinue() {
    Timer(const Duration(milliseconds: 500), () {
      MainTabPage.start();
      setFirstIntro(false);
    });
  }
}

class PageBean {
  String title;
  String animUrl;

  PageBean(this.title, this.animUrl);
}
