import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class SliderWidget extends GetView<SliderLogic> {
  const SliderWidget({super.key, this.height = 210, this.widgetTag});

  final int height;
  final String? widgetTag;

  @override
  String? get tag => widgetTag;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height.h,
      child: controller.obx(
        (state) => Stack(
          alignment: AlignmentDirectional.bottomCenter,
          fit: StackFit.expand,
          children: [
            Positioned.fill(
              child: PageView.builder(
                controller: controller.pageController,
                itemCount: state?.length ?? 0,
                scrollDirection: Axis.horizontal,
                itemBuilder: (context, index) {
                  String? image = state?[index];
                  return Container(
                    height: height.h,
                    margin: EdgeInsets.symmetric(horizontal: 6.w),
                    decoration: BoxDecoration(borderRadius: BorderRadius.circular(8.r)),
                    clipBehavior: Clip.hardEdge,
                    child: CachedNetworkImage(
                      imageUrl: image ?? '',
                      fit: BoxFit.cover,
                      placeholder: (context, url) => const Center(
                        child: CupertinoActivityIndicator(color: AppColor.blueNormal),
                      ),
                      errorWidget: (context, url, error) => const Center(child: Icon(Icons.error)),
                    ),
                  );
                },
              ),
            ),
            Visibility(
              visible: (state?.length ?? 0) > 1,
              child: Positioned(
                bottom: 5,
                child: Container(
                  height: 13.36,
                  padding: const EdgeInsets.symmetric(horizontal: 8),
                  margin: const EdgeInsets.symmetric(vertical: 12),
                  decoration: ShapeDecoration(
                    color: Colors.white38,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(9999)),
                  ),
                  child: SmoothPageIndicator(
                    controller: controller.pageController, // PageController
                    count: state?.length ?? 0,
                    effect: const WormEffect(
                      dotWidth: 6.0,
                      dotHeight: 6.0,
                      activeDotColor: Colors.white,
                      dotColor: Colors.white38,
                    ), // your preferred effect
                    onDotClicked: (index) {
                      controller.pageController.animateToPage(
                        index,
                        duration: const Duration(milliseconds: 500),
                        curve: Curves.easeIn,
                      );
                    },
                  ),
                ),
              ),
            ),
          ],
        ),
        onLoading: const Center(child: CupertinoActivityIndicator(color: AppColor.blueNormal)),
        onError: (error) => Center(child: Text('خطا در بارگذاری اسلایدر: $error')),
      ),
    );
  }
}
