import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'dart:async';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';

import '../../../app/app.dart';

class CarouselWidget extends StatefulWidget {
  final List<SliderModel> sliders;

  CarouselWidget({required this.sliders});

  @override
  State<CarouselWidget> createState() => _CarouselWidgetState();
}

class _CarouselWidgetState extends State<CarouselWidget> {
  final pageController = PageController(keepPage: false);
  int _currentPage = 0;

  void _onPageChanged(int currPage) {
    _currentPage = currPage;
  }

  void _changeBanner() {
    debugPrint('changeBanner');
    Timer.periodic(Duration(seconds: 5), (Timer timer) {
      if (_currentPage < widget.sliders.length - 1) {
        _currentPage++;
      } else {
        _currentPage = 0;
      }

      if (pageController.hasClients) {
        pageController.animateToPage(
          _currentPage,
          duration: Duration(milliseconds: 350),
          curve: Curves.easeIn,
        );
      }
    });
  }

  @override
  void initState() {
    _changeBanner();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // Carousel
        Container(
          // color: Colors.amber,
          height: context.isTablet ? 0.24.sh : 0.22.sh,
          child: PageView(
            controller: pageController,
            onPageChanged: (currPage) => _onPageChanged(currPage),
            children: mapIndexed(
              widget.sliders,
              (index, SliderModel slider) => GestureDetector(
                onTap: () {
                  if (slider.path.trim().length != 0) {
                    debugPrint('slider path: ${slider.path}');
                    debugPrint('slider id: ${slider.id}');
                    final params = slider.path.split('=');
                    final args = {
                      'type': params.first,
                      'id': params.last,
                    };
                    navigateToProductListScreen(args);
                  } else
                    debugPrint('empty path');
                },
                child: cachedImageNetwork(
                  slider.imageUrl,
                  BoxFit.contain,
                  const BorderRadius.all(Radius.circular(8.0)),
                  double.infinity,
                  0.18.sh,
                ),
              ),
            ).toList(),
          ),
        ),
        SizedBox(height: 12.h),
        // Indicator
        widget.sliders.isNotEmpty
            ? SmoothPageIndicator(
                controller: pageController,
                count: widget.sliders.length,
                effect: WormEffect(
                  activeDotColor: ThemeColor.mainColor,
                  dotColor: ThemeColor.colorC4C4C4,
                  dotWidth: 18.w,
                  dotHeight: 4.h,
                ),
              )
            : SizedBox.shrink(),
      ],
    );
  }
}
