import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_debouncer/flutter_debouncer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_store_repository_imp.dart';
import 'package:yumi/shared/business_logic/models/res/store_list_res.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/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/business_logic/models/res/bags_list_res.dart';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
import 'package:yumi/services/auth/user_profile_manager.dart';
import 'package:yumi/modules/store/store_route.dart';
import 'package:yumi/ui_kit/widgets/headdress/headdress_widget.dart';

import '../../../shared/data_sources/models/enum/sc_props_type.dart';

class PropsBagHeaddressDetailDialog extends StatefulWidget {
  BagsListRes res;

  PropsBagHeaddressDetailDialog(this.res);

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

class _PropsBagHeaddressDetailDialogState
    extends State<PropsBagHeaddressDetailDialog> {
  final debouncer = Debouncer();

  ///自己当前佩戴的头饰
  PropsResources? myHeaddress;

  @override
  void initState() {
    super.initState();
    myHeaddress = AccountStorage().getHeaddress();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300.w,
      width: 230.w,
      decoration: BoxDecoration(
        color: Color(0xff03523a),
        borderRadius: BorderRadius.all(Radius.circular(10.w)),
      ),
      child: Stack(
        children: [
          Column(
            children: [
              SizedBox(height: 20.w),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  text(
                    SCAppLocalizations.of(context)!.viewFrame,
                    fontSize: 16.w,
                    textColor: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
              SizedBox(height: 25.w),
              head(
                  url:
                  AccountStorage()
                      .getCurrentUser()
                      ?.userProfile
                      ?.userAvatar ??
                      "",
                  width: 95.w,
                  height: 95.w,
                  headdress: widget.res.propsResources?.sourceUrl
              ),
              SizedBox(height: 30.w),
              GestureDetector(
                child: Container(
                  alignment: Alignment.center,
                  width: 130.w,
                  padding: EdgeInsets.symmetric(vertical: 10.w),
                  decoration: BoxDecoration(
                    color: SocialChatTheme.primaryLight,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: text(
                    (widget.res.expireTime ?? 0) <
                            DateTime.now().millisecondsSinceEpoch
                        ? SCAppLocalizations.of(context)!.renewal
                        : myHeaddress?.id == widget.res.propsResources?.id
                        ? SCAppLocalizations.of(context)!.unUse
                        : SCAppLocalizations.of(context)!.use,
                    fontSize: 16.sp,
                    textColor: Colors.white,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                onTap: () {
                  debouncer.debounce(
                    duration: Duration(milliseconds: 350),
                    onDebounce: () {
                      if ((widget.res.expireTime ?? 0) <
                          DateTime.now().millisecondsSinceEpoch) {
                        ///过期 续费操作
                        SCNavigatorUtils.push(
                          context,
                          StoreRoute.list,
                          replace: false,
                        );
                        return;
                      }
                      if (myHeaddress?.id != widget.res.propsResources?.id) {
                        ///佩戴
                        _use(widget.res, false);
                      } else {
                        ///卸下
                        _use(widget.res, true);
                      }
                    },
                  );
                },
              ),
            ],
          ),
          Positioned(
            right: 20.w,
            top: 18.w,
            child: GestureDetector(
              child: Image.asset(
                "sc_images/general/sc_icon_msg_tips_close.png",
                height: 20.w,
                color: Colors.white,
              ),
              onTap: () {
                SmartDialog.dismiss(tag: "showPropsDetail");
              },
            ),
          ),
        ],
      ),
    );
  }

  void _use(BagsListRes res, bool unload) {
    SCLoadingManager.show();
    SCStoreRepositoryImp()
        .switchPropsUse(
          SCPropsType.AVATAR_FRAME.name,
          res.propsResources?.id ?? "",
          unload,
        )
        .then((value) async {
          await Provider.of<SocialChatUserProfileManager>(context, listen: false).fetchUserProfileData(loadGuardCount: false);
          Future.delayed(Duration(milliseconds: 400),(){
            SCLoadingManager.hide();
            SmartDialog.dismiss(tag: "showPropsDetail");
          });
          if (!unload) {
            SCTts.show(SCAppLocalizations.of(context)!.successfulWear);
          } else {
            SCTts.show(SCAppLocalizations.of(context)!.successfullyUnloaded);
          }
        }).catchError((e){
      SCLoadingManager.hide();
    });
  }
}
