import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/rank.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/mine_vip_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MineVipLevelPage extends StatefulWidget {
  final Map<String, dynamic> arguments;

  MineVipLevelPage({this.arguments});

  @override
  State<StatefulWidget> createState() {
    return _MineVipLevelPage();
  }
}

class _MineVipLevelPage extends State<MineVipLevelPage> {
  ApiService apiService;
  List<Rank> ranks = [];

  final SwiperController controller = SwiperController();

  @override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((value) {
      apiService =
          ApiService(Dio(), context: context, token: value.getString("token"));
      queryVipLevel();
    });
  }

  queryVipLevel() async {
    BaseData<List<Rank>> rankData = await apiService.rankList().catchError((onError) {});
    if (rankData != null && rankData.isSuccess) {
      ranks.clear();
      ranks.addAll(rankData.data);
      setState(() {
        controller.move((widget.arguments["rankLevel"] - 1), animation: false);
      });
    } else {
      SmartDialog.showToast(rankData.msg, alignment: Alignment.center);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: S.of(context).wodehuiyuandengji,
        titleColor: Colors.black,
        titleSize: 18.sp,
        background: Color(0xFFFFFFFF),
        leadingColor: Colors.black,
      ),
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Column(
          children: [
            Container(
              height: (MediaQuery.of(context).size.width) / 1.78 * AppUtils.textScale(context),
              child: Swiper(
                viewportFraction: 0.95,
                loop: false,
                physics: BouncingScrollPhysics(),
                controller: controller,
                itemBuilder: (context, position) {
                  return MineVipView(
                    vipLevel: !(ranks != null && ranks.isNotEmpty) ? widget.arguments["rankLevel"] : position + 1,
                    curLevel: widget.arguments["rankLevel"],
                    tag: (widget.arguments["rankLevel"] == (position + 1) || !(ranks != null && ranks.isNotEmpty)) ? "vip" : "",
                    padding: 6.w,
                    rank: !(ranks != null && ranks.isNotEmpty) ? 0 : widget.arguments["points"]??0,
                    rankMax: !(ranks != null && ranks.isNotEmpty) ? 0 : (position < (ranks.length - 1))
                        ? ranks[position + 1].rankOrigin
                        : ranks[position].rankOrigin,
                    createTime: widget.arguments["createTime"],
                  );
                },
                itemCount: (ranks != null && ranks.isNotEmpty) ? ranks.length : 1,
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(0, 16.h, 0, 0),
              padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(8),
                  topRight: Radius.circular(8),
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    S.of(context).huiyuandengjishuoming,
                    style: TextStyle(
                      fontWeight: MyFontWeight.semi_bold,
                      fontSize: 16.sp,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(
                    height: 12.h,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: 6.w,
                        height: 6.h,
                        margin: EdgeInsets.fromLTRB(4.w, 7.h, 4.w, 7.h),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(3),
                          color: Color(0xFF32A060),
                        ),
                      ),
                      Expanded(
                        child: Text(
                          S.of(context).chenggongdengluzhuce,
                          style: TextStyle(
                            textBaseline: TextBaseline.alphabetic,
                            fontSize: 14.sp,
                            fontWeight: MyFontWeight.regular,
                            color: Color(0xFF353535),
                          ),
                        ),
                        flex: 1,
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 20.h,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Text(
                          S.of(context).huiyuandengji,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontWeight: MyFontWeight.semi_bold,
                            fontSize: 14.sp,
                            color: Color(0xFF353535),
                          ),
                        ),
                        flex: 1,
                      ),
                      Expanded(
                        child: Text(
                          S.of(context).jifen,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontWeight: MyFontWeight.semi_bold,
                            fontSize: 14.sp,
                            color: Color(0xFF353535),
                          ),
                        ),
                        flex: 1,
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 8.h,
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: ranks != null
                        ? ranks.map((e) {
                            return levelItem(
                                e,
                                ranks[ranks.indexOf(e)].rankOrigin,
                                ranks.indexOf(e) == (ranks.length - 1)
                                    ? 0
                                    : ranks[ranks.indexOf(e) + 1].rankOrigin);
                          }).toList()
                        : [],
                  ),
                  SizedBox(
                    height: 32.h,
                  ),
                  Text(
                    S.of(context).jifendejisuanshuoming,
                    style: TextStyle(
                      fontWeight: MyFontWeight.semi_bold,
                      fontSize: 16.sp,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(
                    height: 12.h,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: 6.w,
                        height: 6.h,
                        margin: EdgeInsets.fromLTRB(4.w, 7.h, 4.w, 7.h),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(3),
                          color: Color(0xFF32A060),
                        ),
                      ),
                      Text(
                        S.of(context).yonghuxiaofeijifen,
                        style: TextStyle(
                          textBaseline: TextBaseline.alphabetic,
                          fontSize: 14.sp,
                          fontWeight: MyFontWeight.semi_bold,
                          color: Color(0xFF353535),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 12.h,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: 6.w,
                        height: 6.h,
                        margin: EdgeInsets.fromLTRB(4.w, 7.h, 4.w, 7.h),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(3),
                          color: Color(0xFF32A060),
                        ),
                      ),
                      Expanded(
                        child: Text(
                          S.of(context).youhuiquanwufajileijifen,
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: MyFontWeight.semi_bold,
                            color: Color(0xFF353535),
                          ),
                        ),
                        flex: 1,
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget levelItem(Rank rank, rankOrigin, rankOriginMax) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 8.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Text(
              rank?.rankName ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: MyFontWeight.regular,
                color: Color(0xFF353535),
              ),
            ),
            flex: 1,
          ),
          Expanded(
            child: Text(
              "$rankOrigin - ${rankOriginMax == 0 ? "无限" : rankOriginMax}",
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: MyFontWeight.regular,
                color: Color(0xFF353535),
              ),
            ),
            flex: 1,
          ),
        ],
      ),
    );
  }
}