import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:share_plus/share_plus.dart';

class OfferAndDiscountsFullScreenSwiperPage extends StatefulWidget {
  final List<String>? images;
  final int initialIndex;

  const OfferAndDiscountsFullScreenSwiperPage({
    super.key,
    this.images,
    this.initialIndex = 0,
  });

  @override
  State<OfferAndDiscountsFullScreenSwiperPage> createState() => _OfferAndDiscountsFullScreenSwiperPageState();
}

class _OfferAndDiscountsFullScreenSwiperPageState extends State<OfferAndDiscountsFullScreenSwiperPage> {
  late PageController _pageController;
  late int _currentPage;
  late List<String> _promoImages;

  @override
  void initState() {
    super.initState();
    // Use provided images or default demo images
    _promoImages = widget.images ??
        [
          'assets/images/offersanddiscounts/promo.jpg',
          'assets/images/offersanddiscounts/promo.jpg',
          'assets/images/offersanddiscounts/promo.jpg',
          'assets/images/offersanddiscounts/promo.jpg',
        ];
    _currentPage = widget.initialIndex;
    _pageController = PageController(initialPage: widget.initialIndex);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFADA6D0),
      body: SafeArea(
        child: Stack(
          children: [
            // Main content area
            Column(
              children: [
                SizedBox(height: 26.h),

                // Page indicators
                _buildPageIndicators(),

                SizedBox(height: 70.h),

                // Swipeable image section
                Expanded(
                  child: PageView.builder(
                    controller: _pageController,
                    onPageChanged: (index) {
                      setState(() {
                        _currentPage = index;
                      });
                    },
                    itemCount: _promoImages.length,
                    itemBuilder: (context, index) {
                      return Center(
                        child: Padding(
                          padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 0.h),
                          child: Transform.flip(
                            flipX: getIt.get<AppState>().isArabic(),
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(16.r),
                              child: Utils.buildImgWithAssets(
                                icon: _promoImages[index],
                                fit: BoxFit.contain,
                              ),
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                ),

                SizedBox(height: 70.h),
                // Bottom action buttons
                _buildBottomActions(),

                SizedBox(height: 12.h),
              ],
            ),

            // Close button (top right)
            Positioned(
                top: 26.h + 14.h, // After indicator spacing
                right: 24.w,
                child: _buildCloseButton()),
          ],
        ),
      ),
    );
  }

  Widget _buildPageIndicators() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Row(
        children: List.generate(
          _promoImages.length,
          (index) => Expanded(
            child: Container(
              margin: EdgeInsets.only(
                right: index < _promoImages.length - 1 ? 4.w : 0,
              ),
              height: 4.h,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.r),
                // Fill bar if it's current page or any previous page
                color: index <= _currentPage ? Colors.white : const Color(0x33000000), // 20% opacity
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildCloseButton() {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      child: Container(
        width: 32.w,
        height: 32.h,
        decoration: BoxDecoration(
          color: const Color(0x33FFFFFF), // 20% opacity
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Center(
          child: Icon(
            Icons.close,
            color: AppColors.textColor,
            size: 20.h,
          ),
        ),
      ),
    );
  }

  Widget _buildBottomActions() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Row(
        children: [
          // Buy Now button (takes remaining space)
          Expanded(
            child: CustomButton(
              text: 'Buy Now',
              icon: AppAssets.shoppingCart,
              iconColor: AppColors.whiteColor,
              onPressed: () {
                // Handle buy now action
              },
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              textColor: AppColors.whiteColor,
              fontSize: 16.f,
              isBold: true,
              borderRadius: 12.r,
              height: 56.h,
            ),
          ),

          SizedBox(width: 12.w),

          // Next button
          _buildIconButton(
            svgIcon: AppAssets.nextSwiper,
            onTap: () {
              if (_currentPage < _promoImages.length - 1) {
                _pageController.nextPage(
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeInOut,
                );
              }
            },
          ),

          SizedBox(width: 12.w),

          _buildIconButton(
              svgIcon: AppAssets.share,
              onTap: () async {
                await Share.share('Check out this amazing offer!');
              }),
        ],
      ),
    );
  }

  Widget _buildIconButton({
    IconData? icon,
    String? svgIcon,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 56.w,
        height: 56.h,
        decoration: BoxDecoration(
          color: const Color(0x57303957), // 34% opacity approximation
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Center(
          child: svgIcon != null
              ? Utils.buildSvgWithAssets(
                  icon: svgIcon,
                  iconColor: AppColors.whiteColor,
                  width: 24.h,
                  height: 24.h,
                )
              : Icon(
                  icon!,
                  color: AppColors.whiteColor,
                  size: 24.h,
                ),
        ),
      ),
    );
  }
}
