import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/utils/common_expand.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.viewportFraction,
  }) : super(
          initialPage: initialPage * 1000,
        );

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

class MemberActivityBannerWidget extends StatefulWidget {
  final int itemCount;
  final IndexedWidgetBuilder itemBuilder;
  final double bannerHeight;
  final double viewportFraction;

  const MemberActivityBannerWidget({
    super.key,
    required this.itemCount,
    required this.itemBuilder,
    this.bannerHeight = 100.0,
    this.viewportFraction = 1,
  });

  @override
  _MemberActivityBannerWidgetState createState() =>
      _MemberActivityBannerWidgetState();
}

class _MemberActivityBannerWidgetState
    extends State<MemberActivityBannerWidget> {
  late InfiniteScrollController _controller;

  @override
  void initState() {
    super.initState();
    _controller = InfiniteScrollController(
        initialPage: widget.itemCount * 500,
        itemCount: widget.itemCount,
        viewportFraction: widget.viewportFraction);

    _startAutoPlay();
  }

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

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

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Column(
        children: [
          SizedBox(
            height: widget.bannerHeight,
            child: PageView.builder(
              controller: _controller,
              itemBuilder: (context, index) {
                final realIndex = _controller.getRealIndex(index);
                return widget.itemBuilder(context, realIndex);
              },
            ),
          ),
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.only(top: 16.w),
            child: SmoothPageIndicator(
              controller: _controller,
              count: widget.itemCount,
              effect: ExpandingDotsEffect(
                expansionFactor: 2,
                dotWidth: 5.0,
                dotHeight: 5.w,
                spacing: 4.0,
                dotColor: '#D7D7E8'.color,
                activeDotColor: ColorName.colorPrimary,
              ),
              onDotClicked: (index) {
                _controller.animateToPage(
                  index + _controller.initialPage,
                  duration: const Duration(milliseconds: 500),
                  curve: Curves.ease,
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
