import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';

// 无限滚动控制器
class InfiniteScrollController extends PageController {
  final int itemCount;

  InfiniteScrollController({
    required this.itemCount,
    int initialPage = 0,
  }) : super(initialPage: initialPage * 1000); // 从一个很大的中间值开始

  int getRealIndex(int page) {
    if (itemCount == 0) return 0;
    return page % itemCount;
  }
}

class MemberHeaderCycleWidget extends StatefulWidget {
  @override
  _MemberHeaderCycleWidgetState createState() => _MemberHeaderCycleWidgetState();
}

class _MemberHeaderCycleWidgetState extends State<MemberHeaderCycleWidget> {
  late InfiniteScrollController _controller;
  final List<ImageProvider> _images = [
    Assets.images.imgMemberHeaderAd1.provider(),
    Assets.images.imgMemberHeaderAd2.provider(),
    Assets.images.imgMemberHeaderAd3.provider(),
    Assets.images.imgMemberHeaderAd4.provider(),
  ];

  @override
  void initState() {
    super.initState();
    _controller = InfiniteScrollController(itemCount: _images.length);
    _startAutoPlay();
  }

  void _startAutoPlay() {
    Future.delayed(Duration(seconds: 5), () {
      if (_controller.hasClients) {
        _controller.nextPage(
          duration: Duration(milliseconds: 500),
          curve: Curves.ease,
        );
        _startAutoPlay();
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          height: 291.w + MediaQuery.of(Get.context!).padding.top - 36.5.w,
          width: double.infinity,
          child: PageView.builder(
            controller: _controller,
            itemCount: null, // 无限滚动
            itemBuilder: (context, index) {
              final realIndex = _controller.getRealIndex(index);
              return Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                      image: _images[realIndex],
                      //fit: BoxFit.cover,
                    )
                ),
              );
            },
          ),
        ),
        Positioned(
            bottom: 38.w,
            left: 0,
            right: 0,
            height: 5.w,
            child: Container(
              alignment: Alignment.center,
              child: SmoothPageIndicator(
                controller: _controller,
                count: _images.length,
                effect: ExpandingDotsEffect(
                  expansionFactor: 2,
                  dotWidth: 5.0,
                  dotHeight: 5.w,
                  spacing: 4.0,
                  dotColor: Colors.grey,
                  activeDotColor: Colors.deepPurple,
                ),
                onDotClicked: (index) {
                  _controller.animateToPage(
                    index + _controller.initialPage, // 跳转到对应的位置
                    duration: Duration(milliseconds: 500),
                    curve: Curves.ease,
                  );
                },
              ),
            )),
      ],
    );
  }
}