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/app_localizations.dart';
import 'package:yumi/ui_kit/components/custom_cached_image.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/shared/business_logic/models/res/gift_res.dart';
import 'package:yumi/services/general/sc_app_general_manager.dart';
import 'package:yumi/app/config/business_logic_strategy.dart';
import 'package:yumi/app/constants/sc_global_config.dart';

class GiftBagPage extends StatefulWidget {
  const GiftBagPage({Key? key}) : super(key: key);

  @override
  _GiftBagPageState createState() => _GiftBagPageState();
}

class _GiftBagPageState extends State<GiftBagPage> with TickerProviderStateMixin {
  PageController _giftPageController = PageController();
  int _index = 0;

  int checkedIndex = 0;

  BusinessLogicStrategy get _strategy => SCGlobalConfig.businessLogicStrategy;

  @override
  Widget build(BuildContext context) {
    return Consumer<SCAppGeneralManager>(
      builder: (context, ref, child) {
        return ref.giftResList.isEmpty
            ? mainEmpty(textColor: Colors.white54,msg: SCAppLocalizations.of(context)!.noData)
            : Column(
          children: [
            Expanded(
              child: PageView.builder(
                controller: _giftPageController,
                onPageChanged: (i) {
                  setState(() {
                    _index = i;
                  });
                },
                itemBuilder: (c, i) {
                  var current = (ref.giftResList.length - (i * 8));
                  int size =
                  current > 8
                      ? 8
                      : current % 8 == 0
                      ? 8
                      : current % 8;
                  return GridView.builder(
                    shrinkWrap: true,
                    padding: EdgeInsets.symmetric(horizontal: 12.w),
                    physics: NeverScrollableScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 4,
                      childAspectRatio: 0.80,
                      mainAxisSpacing: 8.w,
                      crossAxisSpacing: 8.w,
                    ),
                    itemCount: size,
                    itemBuilder: (BuildContext context, int index) {
                      return _bagItem(
                        ref.giftResList[(i * 8) + index],
                        ref,
                      );
                    },
                  );
                },
                itemCount: (ref.giftResList.length / 8).ceil(),
              ),
            ),
            _indicator(ref),
            SizedBox(height: 10.w),
          ],
        );
      },
    );
  }

  _indicator(SCAppGeneralManager ref) {
    var size = (ref.giftResList.length) / 8;
    var list = <Widget>[];
    for (int i = 0; i < size; i++) {
      list.add(
        Container(
          width: 6.5.w,
          height: 6.5.w,
          margin: EdgeInsets.symmetric(horizontal: 4.w),
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: _index == i ? SocialChatTheme.primaryColor : Color(0xffDADADA),
          ),
        ),
      );
    }
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: list);
  }

  Widget _bagItem(SocialChatGiftRes gift, SCAppGeneralManager ref) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12.w),
          color: Colors.white10,
          border: Border.all(
            color:
            checkedIndex == ref.giftResList.indexOf(gift)
                ? SocialChatTheme.primaryColor
                : Colors.transparent,
            width: 1.w,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CustomCachedImage(
              imageUrl: gift.giftPhoto ?? "",
              fit: BoxFit.cover,
              width: 48.w,
              height: 48.w,
            ),
            SizedBox(height: 10.w),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  _strategy.getGiftPageGoldCoinIcon(),
                  width: 14.w,
                  height: 14.w,
                ),
                SizedBox(width: 3.w),
                text("${gift.giftCandy}", fontSize: 10.sp),
              ],
            ),
          ],
        ),
      ),
      onTap: () {
        setState(() {
          checkedIndex = ref.giftResList.indexOf(gift);
          // widget.checkedCall(checkedIndex);
        });
      },
    );
  }


}