import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marquee/marquee.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/tools/sc_string_utils.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/shared/business_logic/models/res/sc_user_level_exp_res.dart';
import 'package:yumi/modules/store/store_route.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';

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

class WealthLevelPage extends StatefulWidget {
  @override
  _WealthLevelPageState createState() => _WealthLevelPageState();
}

class _WealthLevelPageState extends State<WealthLevelPage> {
  SCUserLevelExpRes? res;

  @override
  void initState() {
    super.initState();
    SCAccountRepository()
        .userLevelConsumptionExp(
          AccountStorage().getCurrentUser()?.userProfile?.id ?? "",
          SCLevelType.WEALTH.name,
        )
        .then((value) {
          res = value;
          setState(() {});
        });
  }

  @override
  Widget build(BuildContext context) {
    return res != null
        ? SingleChildScrollView(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 15.w),
                height: 108.w,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      "sc_images/level/sc_icon_user_level_wealth_info_bg.png",
                    ),
                    fit: BoxFit.fill,
                  ),
                ),
                child: Row(
                  children: [
                    SizedBox(width: 11.w),
                    head(
                      url:
                          AccountStorage()
                              .getCurrentUser()
                              ?.userProfile
                              ?.userAvatar ??
                          "",
                      width: 80.w,
                    ),
                    SizedBox(width: 18.w),
                    SizedBox(
                      width: 200.w,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              socialchatNickNameText(
                                maxWidth: 125.w,
                                fontWeight: FontWeight.bold,
                                AccountStorage()
                                    .getCurrentUser()
                                    ?.userProfile
                                    ?.userNickname ??
                                    "",
                                fontSize: 15.sp,
                                type:
                                AccountStorage()
                                    .getCurrentUser()
                                    ?.userProfile
                                    ?.getVIP()
                                    ?.name ??
                                    "",
                                needScroll:
                                (AccountStorage()
                                    .getCurrentUser()
                                    ?.userProfile
                                    ?.userNickname
                                    ?.characters
                                    .length ??
                                    0) >
                                    10,
                              ),
                              SizedBox(width: 3.w),
                              getWealthLevel(
                                res?.level ?? 0,
                                width: 58.w,
                                height: 30.w,
                              ),
                            ],
                          ),
                          SizedBox(height: 3.w),
                          Stack(
                            children: [
                              Container(
                                width: 185.w,
                                height: 6.w,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(12),
                                  color: Colors.white24,
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 5.w),
                          Row(
                            children: [
                              SizedBox(width: 5.w),
                              text(
                                res?.thatExperienceFormat ?? "",
                                fontWeight: FontWeight.w600,
                                textColor: Colors.white,
                                fontSize: 12.sp,
                              ),
                              Spacer(),
                              text(
                                SCStringUtils.formatNumericValue(
                                  getLevelTotalExperience(),
                                ),
                                fontWeight: FontWeight.w600,
                                textColor: Colors.white,
                                fontSize: 12.sp,
                              ),
                              SizedBox(width: 20.w),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              text(
                SCAppLocalizations.of(context)!.medalAndAvatarFrameRewards,
                fontSize: 20.sp,
                fontWeight: FontWeight.bold,
                textColor: SocialChatTheme.primaryLight,
              ),
              text(
                SCAppLocalizations.of(context)!.higherLevelFancierAvatarFrame,
                fontSize: 12.sp,
                fontWeight: FontWeight.w600,
                textColor: Colors.white,
              ),
              Image.asset("sc_images/level/sc_icon_user_wealth_center_bg_1.png"),
              Image.asset("sc_images/level/sc_icon_user_wealth_center_bg_2.png"),
              text(
                SCAppLocalizations.of(context)!.howToUpgrade,
                fontSize: 20.sp,
                fontWeight: FontWeight.bold,
                textColor:SocialChatTheme.primaryLight,
              ),
              SizedBox(height: 3),
              text(
                SCAppLocalizations.of(context)!.spendCoinsToGainExperiencePoints,
                fontSize: 12.sp,
                fontWeight: FontWeight.w600,
                textColor: Colors.white,
              ),
              SizedBox(height: 25.w),
              GestureDetector(
                child: Container(
                  height: 45.w,
                  width: 180.w,
                  alignment: AlignmentDirectional.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(99.w),
                    color: SocialChatTheme.primaryLight,
                  ),
                  child: text(
                    SCAppLocalizations.of(context)!.toConsume,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                onTap: () {
                  SCNavigatorUtils.push(context, StoreRoute.list);
                },
              ),
              SizedBox(height: 20.w,)
            ],
          ),
        )
        : Container();
  }

  double getLevelExperience(double width) {
    int thatExp = SCStringUtils.convertToInteger(res?.thatExperience ?? "0");
    int nexExp = SCStringUtils.convertToInteger(res?.nextExperience ?? "0");
    int levelExp = thatExp + nexExp;
    return (thatExp / levelExp) * width;
  }

  num getLevelTotalExperience() {
    int thatExp = SCStringUtils.convertToInteger(res?.thatExperience ?? "0");
    int nexExp = SCStringUtils.convertToInteger(res?.nextExperience ?? "0");
    int levelExp = thatExp + nexExp;
    return levelExp;
  }
}
