import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/retrofit/data/rank.dart';
import 'package:huixiang/retrofit/data/user_info.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/login_tips_dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MineVipView extends StatelessWidget {
  int vipLevel;
  int curLevel;
  final int rankMax;
  final int rank;
  final String createTime;
  final String tag;
  final double padding;
  final UserInfo userInfo;
  final List<Rank> ranks;
  final bool showRank;
  final String price;

  MineVipView({
    this.vipLevel = 1,
    this.ranks,
    this.tag,
    this.userInfo,
    this.padding = 16,
    this.curLevel = 1,
    this.rankMax = 0,
    this.rank = 0,
    this.createTime = "",
    this.showRank,
    this.price
  });

  String topLeft = "";
  String levelText = "普通用户";

  @override
  Widget build(BuildContext context) {
    List<Color> linearColor = [
      Color(0xFFD6F6F3),
      Color(0xFF86BEBA),
    ];
    Color levelColor = Color(0xFF558B87);
    Color textColor = Colors.white;

    if (userInfo != null &&
        userInfo.memberRankVo != null &&
        ranks != null &&
        ranks.length > 0) {
      curLevel = (ranks.indexWhere((element) => element.id == userInfo.memberRankVo.id) + 1);
      vipLevel = curLevel;
    }

    if (curLevel == vipLevel) {
      topLeft = S.of(context).dangqiandengji;
    } else if (vipLevel < curLevel) {
      topLeft = S.of(context).shangyidengji;
    } else {
      topLeft = S.of(context).zanweikaitong;
    }
    switch (vipLevel) {
      case 1:
        {
          linearColor = [
            Color(0xFFF2F2F2),
            Color(0xFFCCCCCC),
          ];
          levelColor = Color(0xFF575757);
          textColor = Color(0xFF575757);
          levelText = "VIP ${S.of(context).yinkahuiyuan}";
          break;
        }
      case 2:
        {
          linearColor = [
            Color(0xFFFEF5DC),
            Color(0xFFD1B97D),
          ];
          levelColor = Color(0xFFAE9B6D);
          textColor = Color(0xFF93723B);
          levelText = "VIP ${S.of(context).jinkahuiyuan}";
          break;
        }
      case 3:
        {
          linearColor = [
            Color(0xFFEEEEEF),
            Color(0xFF000000),
          ];
          levelColor = Color(0xFF000000);
          levelText = "VIP ${S.of(context).gongchuanghuiyuan}";
          break;
        }
      // case 4:
      //   {
      //     linearColor = [
      //       Color(0xFFEEEEEF),
      //       Color(0xFFA999DB),
      //     ];
      //     levelColor = Color(0xFF887CB4);
      //     levelText = "LV4 ${S.of(context).zuanshihuiyuan}";
      //     break;
      //   }
      // case 5:
      //   {
      //     linearColor = [
      //       Color(0xFFEEEEEF),
      //       Color(0xFF000000),
      //     ];
      //     levelColor = Color(0xFF000000);
      //     levelText = "LV5 ${S.of(context).zhizunhuiyuan}";
      //     break;
      //   }
    }

    bool isUserVip = (userInfo == null);

    Widget widget = Container(
      margin: isUserVip
          ? EdgeInsets.fromLTRB(padding.w, 16.h, padding.w, 8.h)
          : EdgeInsets.fromLTRB(padding.w, 16.h, padding.w, 0),
      width: MediaQuery.of(context).size.width - 32.w,
      height: ((MediaQuery.of(context).size.width - 32.w) /
          (isUserVip ? 1.99 : 2.11) *
          AppUtils.textScale(context)),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: linearColor,
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderRadius: isUserVip
            ? BorderRadius.circular(8)
            : BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(12),
            offset: Offset(0, 3),
            blurRadius: 14,
            spreadRadius: 0,
          ),
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // Row(
          //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //   crossAxisAlignment: CrossAxisAlignment.start,
          //   children: [
          //     Container(
          //       padding: EdgeInsets.fromLTRB(9.w, 4.h, 8.w, 4.h),
          //       decoration: BoxDecoration(
          //         color: levelColor,
          //         borderRadius: BorderRadius.only(
          //           topLeft: Radius.circular(8),
          //           bottomRight: Radius.circular(8),
          //         ),
          //       ),
          //       child: Text(
          //         topLeft,
          //         style: TextStyle(
          //           fontSize: 12.sp,
          //           fontWeight: MyFontWeight.semi_bold,
          //           color: Colors.white,
          //         ),
          //       ),
          //     ),
          //     Container(
          //       padding: EdgeInsets.fromLTRB(9.w, 4.h, 8.w, 4.h),
          //       margin: EdgeInsets.only(top: 10,right: 10),
          //       decoration: BoxDecoration(
          //         color: levelColor,
          //         borderRadius: BorderRadius.circular(10),
          //       ),
          //       child: Text(
          //         S.of(context).chakanquanyi,
          //         style: TextStyle(
          //           fontSize: 12.sp,
          //           fontWeight: MyFontWeight.semi_bold,
          //           color: Color(0xFFF5EEE9),
          //         ),
          //       ),
          //     )
          //   ],
          // ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                height: 86.w,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: EdgeInsets.fromLTRB(9.w, 4.h, 8.w, 4.h),
                      decoration: BoxDecoration(
                        color: levelColor,
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(8),
                          bottomRight: Radius.circular(8),
                        ),
                      ),
                      child: Text(
                        topLeft,
                        style: TextStyle(
                          fontSize: 12.sp,
                          fontWeight: MyFontWeight.semi_bold,
                          color: Colors.white,
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 13.w),
                      child: Text(
                        levelText,
                        style: TextStyle(
                          color: textColor,
                          fontWeight: MyFontWeight.semi_bold,
                          fontSize:22.sp,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(right: 11.w, bottom: 9.h),
                child: Image.asset(
                  "assets/image/icon_mine_huixiang_logo.webp",
                  width: 86.w,
                  height: 86.w,
                  fit: BoxFit.contain,
                ),
              ),
            ],
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(
                left: 13.w,
                right: 12.w,
                bottom: 12.h,
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.baseline,
                    textBaseline: TextBaseline.alphabetic,
                    children: [
                      Expanded(
                        child: Text(
                          // rank > rankMax
                          //     ? S.of(context).dangqiandengji
                          //     : "${S.of(context).jifen_(rankMax - rank)} ${S.of(context).daoxiayidengji}",
                          !(showRank??true)?"":((vipLevel < curLevel)?"":(((rank ?? 0) > rankMax || curLevel == 3 || curLevel ==2)
                              ? S.of(context).dangqiandengji
                              : "消费¥${rankMax - (rank ?? 0)}元 ${S.of(context).daoxiayidengji}")),
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: textColor,
                            fontWeight: MyFontWeight.regular,
                            fontSize: 12.sp,
                          ),
                        ),
                        flex: 1,
                      ),
                      Text.rich(
                        TextSpan(
                          children: [
                            TextSpan(
                              text: !(showRank??true)?(vipLevel == 3?"":rankMax.toString()):((vipLevel < curLevel)?rankMax.toString():rank.toString()),
                              style: TextStyle(
                                fontSize: 20.sp,
                                fontWeight: MyFontWeight.semi_bold,
                                color: textColor,
                              ),
                            ),
                            if(vipLevel == 3 && !(showRank??true))
                            TextSpan(
                              text: "¥${price??""}/永久" ,
                              style: TextStyle(
                                      fontSize: 14.sp,
                                      fontWeight: MyFontWeight.regular,
                                      color: textColor,
                              ),
                            ),
                            if ((rank ?? 0) <= rankMax && (showRank??true))
                              TextSpan(
                                text: rankMax > 0 ? "/$rankMax" : "/0",
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  fontWeight: MyFontWeight.regular,
                                  color: textColor,
                                ),
                              ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  if(vipLevel <= curLevel && (showRank??true))
                  Container(
                    height: 8.h,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(6.5),
                      child: LinearProgressIndicator(
                        value: rankMax > (rank ?? 0) ? ((vipLevel < curLevel) ? rankMax/rankMax: (rank ?? 0) / rankMax) : 0,
                        backgroundColor: Colors.white,
                        color: levelColor,
                      ),
                    ),
                  ),
                  // if (isUserVip)
                  Expanded(child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        S.of(context).kaitongriqi(
                            (createTime != null && createTime != "")
                                ? createTime.split(" ")[0]
                                : "$createTime"
                        ),
                        style: TextStyle(
                          color: textColor,
                          fontWeight: MyFontWeight.regular,
                          fontSize: 12.sp,
                        ),
                      ),
                      Icon(
                        Icons.qr_code,
                        size: 24,
                        color:textColor,
                      ),
                    ],
                  ),),
                ],
              ),
            ),
            flex: 1,
          ),
        ],
      ),
    );

    return GestureDetector(
      onTap: () {
        if (userInfo != null) {
          SharedPreferences.getInstance().then((value) {
            if (value.getString("token") == null ||
                value.getString("token") == "") {
              LoginTipsDialog().show(context);
              return;
            }
            // Navigator.of(context)
            //     .pushNamed('/router/mine_vip_level_page', arguments: {
            //   "rankLevel": curLevel,
            //   "createTime": (userInfo != null) ? "${userInfo.createTime}" : "",
            //   "points": (userInfo != null) ? int.tryParse(userInfo.points) : 0,
            // });
            Navigator.of(context)
                .pushNamed('/router/mine_vip_core', arguments: {
              "rankLevel": curLevel,
              "userInfo":userInfo.masterCardRankName,
              "createTime": (userInfo != null) ? "${userInfo.createTime}" : "",
              "expendAmount": double.tryParse(userInfo?.expendAmount??"0").toInt(),
            });
          });
        }
      },
      child: widget,
      //(tag == null || tag == "")
      //           ? widget
      //           : Hero(tag: tag, child: widget)
    );
  }
}