import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/shared/tools/sc_banner_utils.dart';
import 'package:yumi/shared/tools/sc_string_utils.dart';
import 'package:yumi/services/general/sc_app_general_manager.dart';
import 'package:yumi/ui_kit/widgets/pop/pop_route.dart';

class RecommendBannerView extends StatefulWidget {
  @override
  _RecommendBannerViewState createState() => _RecommendBannerViewState();
}

class _RecommendBannerViewState extends State<RecommendBannerView> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Consumer<SCAppGeneralManager>(
      builder: (context, ref, child) {
        return _banner(ref);
      },
    );
  }

  _banner(SCAppGeneralManager ref) {
    if (ref.homeBanners.isEmpty) {
      return Container(
        height: 118.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12.w),
          color: Color(0xfff1f1f1),
        ),
      );
    }
    return Stack(
      alignment: AlignmentDirectional.bottomCenter,
      children: [
        CarouselSlider(
          options: CarouselOptions(
            height: 118.w,
            autoPlay: true,
            // 启用自动播放
            enlargeCenterPage: false,
            // 居中放大当前页面
            enableInfiniteScroll: true,
            // 启用无限循环
            autoPlayAnimationDuration: Duration(milliseconds: 800),
            // 自动播放动画时长
            viewportFraction: 1,
            // 视口分数
            onPageChanged: (index, reason) {
              _currentIndex = index;
              setState(() {});
            },
          ),
          items:
              ref.homeBanners.map((item) {
                return GestureDetector(
                  child: netImage(
                    url: item.cover ?? "",
                    height: 118.w,
                    fit: BoxFit.fill,
                    borderRadius: BorderRadius.circular(12.w),
                  ),
                  onTap: () {
                    print('ads:${item.toJson()}');
                    SCBannerUtils.openBanner(item,context);
                  },
                );
              }).toList(),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children:
              ref.homeBanners.asMap().entries.map((entry) {
                return Container(
                  width: 6.0,
                  height: 6.0,
                  margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color:
                        _currentIndex == entry.key ? Colors.blue : Colors.white,
                  ),
                );
              }).toList(),
        ),
      ],
    );
  }
}
