import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/presentation/offers_and_discounts/offer_and_discounts_full_screen_swiper_page.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:smooth_corner/smooth_corner.dart';

import '../../../core/utils/utils.dart';

class OffersAndDiscountsCarousel extends StatefulWidget {
  const OffersAndDiscountsCarousel({super.key});

  @override
  State<OffersAndDiscountsCarousel> createState() => _OffersAndDiscountsCarouselState();
}

class _OffersAndDiscountsCarouselState extends State<OffersAndDiscountsCarousel> {
  final ScrollController _scrollController = ScrollController();
  Timer? _autoScrollTimer;

  // List of offer images - you can add more images here
  final List<String> offerImages = [
    'assets/images/offersanddiscounts/img1.png',
    'assets/images/offersanddiscounts/img2.png',
    'assets/images/offersanddiscounts/img3.png',
    'assets/images/offersanddiscounts/img1.png',
    'assets/images/offersanddiscounts/img2.png',
    'assets/images/offersanddiscounts/img3.png',
    'assets/images/offersanddiscounts/img1.png',
  ];

  @override
  void initState() {
    super.initState();
    // _startAutoScroll();
  }

  void _startAutoScroll() {
    _autoScrollTimer = Timer.periodic(const Duration(milliseconds: 50), (timer) {
      if (_scrollController.hasClients) {
        final maxScroll = _scrollController.position.maxScrollExtent;
        final currentScroll = _scrollController.position.pixels;
        final delta = 1.0; // Scroll speed

        if (currentScroll >= maxScroll) {
          // Reset to beginning for infinite scroll effect
          _scrollController.jumpTo(0);
        } else {
          _scrollController.animateTo(
            currentScroll + delta,
            duration: const Duration(milliseconds: 50),
            curve: Curves.linear,
          );
        }
      }
    });
  }

  @override
  void dispose() {
    _autoScrollTimer?.cancel();
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 79.h,
      child: ListView.separated(
        controller: _scrollController,
        scrollDirection: Axis.horizontal,
        padding: EdgeInsets.symmetric(horizontal: 0.w),
        itemCount: offerImages.length * 100,
        itemBuilder: (context, index) {
          final imageIndex = index % offerImages.length;
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => const OfferAndDiscountsFullScreenSwiperPage()),
              );
            },
            child: _buildOfferItem(offerImages[imageIndex]),
          );
        },
        separatorBuilder: (context, index) => SizedBox(width: 12.w),
      ),
    );
  }

  Widget _buildOfferItem(String imagePath) {
    return Container(
      width: 79.w,
      height: 79.h,
      decoration: ShapeDecoration(
        shape: SmoothRectangleBorder(
          borderRadius: BorderRadius.circular(13.r),
          smoothness: 0.6,
          side: BorderSide(
            color: Color(0xff2E3039),
            width: 2.w,
          ),
        ),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(13.r),
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(color: AppColors.textColor, width: 2.w),
            borderRadius: BorderRadius.circular(11.r),
          ),
          child: Padding(
            padding: const EdgeInsets.all(2.0),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(11.r),
              child: Utils.buildImgWithAssets(
                icon: imagePath,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
