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/theme/socialchat_theme.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/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/tools/sc_loading_manager.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/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 '../../../shared/data_sources/models/enum/sc_props_type.dart';

class PropsBagMountainsDetailDialog extends StatefulWidget {
  BagsListRes res;

  PropsBagMountainsDetailDialog(this.res);

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

class _PropsBagMountainsDetailDialogState
    extends State<PropsBagMountainsDetailDialog> {
  final debouncer = Debouncer();
  ///自己当前佩戴的坐骑
  PropsResources? myMountains;
  @override
  void initState() {
    super.initState();
    myMountains = AccountStorage().getMountains();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300.w,
      width: 230.w,
      decoration: BoxDecoration(
        color: Colors.white,
        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.black,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
              SizedBox(height: 20.w),
              netImage(
                url: widget.res.propsResources?.cover ?? "",
                width: 130.w,
                height: 130.w,
              ),
              SizedBox(height: 20.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
                        : myMountains?.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 (myMountains?.id != widget.res.propsResources?.id) {
                        ///佩戴
                        _use(widget.res, false);
                      } else {
                        ///卸下
                        _use(widget.res, true);
                      }
                    },
                  );
                },
              ),
            ],
          ),
          Positioned(
            right: 20.w,
            top: 18.w,
            child: GestureDetector(
              child: Icon(Icons.close, size: 20.w),
              onTap: () {
                SmartDialog.dismiss(tag: "showPropsDetail");
              },
            ),
          ),
        ],
      ),
    );
  }

  void _use(BagsListRes res, bool unload) {
    SCLoadingManager.show();
    SCStoreRepositoryImp()
        .switchPropsUse(
      SCPropsType.RIDE.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();
    });
  }

}
