import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:yumi/ui_kit/components/sc_debounce_widget.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/business_logic/models/res/store_list_res.dart';
import 'package:yumi/main.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.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/components/sc_tts.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_store_repository_imp.dart';
import 'package:yumi/services/auth/user_profile_manager.dart';
import '../../../app/constants/sc_global_config.dart';
import '../../../app/routes/sc_fluro_navigator.dart';
import '../../../shared/data_sources/models/enum/sc_currency_type.dart';
import '../../../shared/data_sources/models/enum/sc_props_type.dart';
import '../../../modules/wallet/wallet_route.dart';

class PropsStoreHeaddressDetailDialog extends StatefulWidget {
  StoreListRes res;

  //折扣
  double disCount = 1;

  PropsStoreHeaddressDetailDialog(this.res, this.disCount);

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

class _PropsStoreHeaddressDetailDialogState
    extends State<PropsStoreHeaddressDetailDialog> {
  PropsPrices? curentPropsPrice;
  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    currentIndex = 0;
    curentPropsPrice = widget.res.propsPrices![currentIndex];
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Container(
        height: 330.w,
        width: ScreenUtil().screenWidth,
        decoration: BoxDecoration(
          color: Color(0xff03523a),
          borderRadius: BorderRadius.all(Radius.circular(10.w)),
        ),
        child: Column(
          children: [
            SizedBox(height: 20.w),
            head(
              url:
                  AccountStorage().getCurrentUser()?.userProfile?.userAvatar ??
                  "",
              width: 95.w,
              height: 95.w,
              headdress:
                  widget.res.propsResources?.sourceUrl ??
                  ""
                      "",
            ),
            SizedBox(height: 25.w),
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(width: 15.w),
                text(
                  widget.res.propsResources?.name ?? "",
                  textColor: Colors.white,
                  fontSize: 12.sp,
                ),
                Spacer(),
                Image.asset(
                  "sc_images/general/sc_icon_jb.png",
                  width: 22.w,
                  height: 22.w,
                ),
                widget.disCount < 1
                    ? text(
                      "${curentPropsPrice?.amount}",
                      textColor: SocialChatTheme.primaryLight,
                      fontWeight: FontWeight.w600,
                      decoration: TextDecoration.lineThrough,
                    )
                    : Container(),
                widget.disCount < 1 ? SizedBox(width: 5.w) : Container(),
                text(
                  "${((curentPropsPrice?.amount ?? 0) * widget.disCount).toInt()}",
                  textColor: SocialChatTheme.primaryLight,
                  fontSize: 13.sp,
                  fontWeight: FontWeight.w600,
                ),
                SizedBox(width: 15.w),
              ],
            ),
            SizedBox(height: 10.w),
            SingleChildScrollView(
              child: SizedBox(
                height: 28.w,
                child: ListView.separated(
                  scrollDirection: Axis.horizontal,
                  shrinkWrap: true,
                  itemCount: widget.res.propsPrices?.length ?? 0,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      child: Container(
                        width: 110.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5.w),
                          color: Color(0xff18F2B1).withOpacity(0.1),
                          border: Border.all(
                            color:
                                index == currentIndex
                                    ? SocialChatTheme.primaryLight
                                    : Colors.transparent,
                            width: 1.w,
                          ),
                        ),
                        child: text(
                          "${widget.res.propsPrices?[index].days} ${SCAppLocalizations.of(context)!.day}",
                          textColor: SocialChatTheme.primaryLight,
                        ),
                      ),
                      onTap: () {
                        currentIndex = index;
                        curentPropsPrice = widget.res.propsPrices?[index];
                        setState(() {});
                      },
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return SizedBox(width: 5.w);
                  },
                ),
              ),
            ),
            SizedBox(height: 20.w),
            Container(
              color: Colors.black12,
              width: ScreenUtil().screenWidth,
              height: 0.5.w,
            ),
            SizedBox(height: 20.w),
            Container(
              alignment: Alignment.center,
              child: Row(
                children: [
                  Consumer<SocialChatUserProfileManager>(
                    builder: (context, ref, child) {
                      return GestureDetector(
                        child: Row(
                          children: [
                            SizedBox(width: 10.w),
                            Image.asset(
                              SCGlobalConfig.businessLogicStrategy
                                  .getStorePageGoldIcon(),
                              width: 20.w,
                              height: 20.w,
                            ),
                            SizedBox(width: 5.w),
                            text(
                              "${ref.myBalance}",
                              fontSize: 13.sp,
                              textColor: SocialChatTheme.primaryLight,
                            ),
                            Icon(
                              Icons.chevron_right,
                              size: 20.w,
                              color: SocialChatTheme.primaryLight,
                            ),
                          ],
                        ),
                        onTap: () {
                          SmartDialog.dismiss(tag: "showPropsDetail");
                          SCNavigatorUtils.push(
                            navigatorKey.currentState!.context,
                            WalletRoute.recharge,
                            replace: false,
                          );
                        },
                      );
                    },
                  ),
                  Spacer(),
                  SCDebounceWidget(
                    child: Container(
                      alignment: Alignment.center,
                      width: 130.w,
                      padding: EdgeInsets.symmetric(vertical: 10.w),
                      decoration: BoxDecoration(
                        color: SocialChatTheme.primaryLight,
                        borderRadius: BorderRadius.circular(12.w),
                      ),
                      child: text(
                        SCAppLocalizations.of(context)!.purchase,
                        fontSize: 16.sp,
                        textColor: Colors.white,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    onTap: () {
                      SCLoadingManager.show();
                      SCStoreRepositoryImp()
                          .storePurchasing(
                            widget.res.id ?? "",
                            SCPropsType.AVATAR_FRAME.name,
                            SCCurrencyType.GOLD.name,
                            "${curentPropsPrice?.days}",
                          )
                          .then((value) {
                            SmartDialog.dismiss(tag: "showPropsDetail");
                            SCLoadingManager.hide();
                            SCTts.show(
                              SCAppLocalizations.of(
                                context,
                              )!.purchaseIsSuccessful,
                            );
                            Provider.of<SocialChatUserProfileManager>(
                              context,
                              listen: false,
                            ).updateBalance(value);
                          })
                          .catchError((e) {
                            SCLoadingManager.hide();
                          });
                    },
                  ),
                  SizedBox(width: 10.w),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
