import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:tancent_vap/utils/constant.dart';
import 'package:tancent_vap/widgets/vap_view.dart';
import 'package:yumi/shared/data_sources/sources/local/file_cache_manager.dart';
import 'package:yumi/ui_kit/widgets/headdress/headdress_widget.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/shared/tools/sc_path_utils.dart';
import 'package:yumi/shared/tools/sc_room_profile_cache.dart';
import 'package:yumi/shared/tools/sc_network_image_utils.dart';
import 'package:yumi/ui_kit/components/sc_rotating_dots_loading.dart';

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

const String kRoomCoverDefaultImg = "sc_images/general/sc_no_data.png";

String resolveRoomCoverUrl(String? roomId, String? roomCover) {
  final cache = SCRoomProfileCache.getRoomProfile(roomId ?? "");
  return SCRoomProfileCache.preferCachedValue(cache["roomCover"], roomCover) ??
      "";
}

Widget head({
  required String url,
  required double width,
  double? height,
  Border? border,
  String? headdress,
  BoxShape shape = BoxShape.circle,
  BoxFit fit = BoxFit.cover,
  BorderRadius? borderRadius,
  bool showDefault = true,
  bool isRoom = false,
}) {
  final bool hasPictureHeaddress =
      headdress != null &&
      headdress.isNotEmpty &&
      SCPathUtils.fileTypeIsPic2(headdress);
  final double avatarScale =
      hasPictureHeaddress
          ? 0.64
          : (headdress != null && headdress.isNotEmpty ? 0.72 : 1);
  final double avatarWidth = width * avatarScale;
  final double avatarHeight = (height ?? width) * avatarScale;

  Widget avatar = SizedBox(
    width: avatarWidth,
    height: avatarHeight,
    child: netImage(
      url: url,
      width: avatarWidth,
      height: avatarHeight,
      fit: fit,
      noDefaultImg: !showDefault,
      defaultImg: "sc_images/general/sc_icon_avar_defalt.png",
    ),
  );

  if (shape == BoxShape.circle) {
    avatar = ClipOval(child: avatar);
    if (border != null) {
      avatar = Container(
        width: avatarWidth,
        height: avatarHeight,
        decoration: ShapeDecoration(shape: CircleBorder(side: border.top)),
        clipBehavior: Clip.antiAlias,
        child: avatar,
      );
    }
  } else if (borderRadius != null) {
    avatar = ClipRRect(borderRadius: borderRadius, child: avatar);
    if (border != null) {
      avatar = Container(
        width: avatarWidth,
        height: avatarHeight,
        decoration: BoxDecoration(border: border, borderRadius: borderRadius),
        clipBehavior: Clip.antiAlias,
        child: avatar,
      );
    }
  } else if (border != null) {
    avatar = Container(
      width: avatarWidth,
      height: avatarHeight,
      decoration: BoxDecoration(border: border),
      clipBehavior: Clip.antiAlias,
      child: avatar,
    );
  }

  return RepaintBoundary(
    child: Stack(
      alignment: Alignment.center,
      children: [
        avatar,
        hasPictureHeaddress
            ? ColorFiltered(
              colorFilter: const ColorFilter.matrix([
                1,
                0,
                0,
                0,
                0,
                0,
                1,
                0,
                0,
                0,
                0,
                0,
                1,
                0,
                0,
                -1,
                -1,
                -1,
                3,
                0,
              ]),
              child: netImage(
                url: headdress,
                width: width,
                height: height ?? width,
                fit: BoxFit.contain,
                noDefaultImg: true,
              ),
            )
            : Container(),
        headdress != null &&
                SCPathUtils.getFileExtension(headdress).toLowerCase() ==
                    ".svga" &&
                SCGlobalConfig.sdkInt > SCGlobalConfig.maxSdkNoAnim
            ? IgnorePointer(
              child: SVGAHeadwearWidget(
                width: width,
                height: width,
                resource: headdress,
              ),
            )
            : Container(),
        headdress != null &&
                SCPathUtils.getFileExtension(headdress).toLowerCase() ==
                    ".mp4" &&
                SCGlobalConfig.sdkInt > SCGlobalConfig.maxSdkNoAnim
            ? SizedBox(
              width: width,
              height: width,
              child: IgnorePointer(
                child: VapView(
                  scaleType: ScaleType.centerCrop,
                  repeat: 90000000,
                  onViewCreated: (controller) {
                    FileCacheManager.getInstance().getFile(url: headdress).then(
                      (file) {
                        controller.playFile(file.path);
                      },
                    );
                  },
                ),
              ),
            )
            : Container(),
      ],
    ),
  );
}

Widget netImage({
  required String url,
  String? defaultImg,
  bool noDefaultImg = false,
  double? width,
  double? height,
  BorderRadius? borderRadius,
  BoxFit? fit,
  BoxShape? shape,
  Border? border,
  Widget? loadingWidget,
  Widget? errorWidget,
}) {
  // print('${SCGlobalConfig.imgHost}$image?imageslim');
  return ExtendedImage.network(
    url,
    width: width,
    height: height ?? width,
    headers: buildNetworkImageHeaders(url),
    fit: fit ?? BoxFit.cover,
    cache: true,
    shape: shape ?? BoxShape.rectangle,
    borderRadius: borderRadius,
    clearMemoryCacheWhenDispose: false,
    clearMemoryCacheIfFailed: true,
    border: border,
    gaplessPlayback: true,
    loadStateChanged: (ExtendedImageState state) {
      if (state.extendedImageLoadState == LoadState.completed) {
        return ExtendedRawImage(
          image: state.extendedImageInfo?.image,
          fit: fit ?? BoxFit.cover,
        );
      } else if (state.extendedImageLoadState == LoadState.failed) {
        if (errorWidget != null) {
          return errorWidget;
        }
        return noDefaultImg
            ? Container()
            : Image.asset(
              defaultImg ?? "sc_images/general/sc_icon_loading.png",
              fit: BoxFit.cover,
            );
      } else {
        if (loadingWidget != null) {
          return loadingWidget;
        }
        return noDefaultImg ? Container() : const SCRotatingDotsLoading();
      }
    },
  );
}

/// 空页面
///默认空页面
mainEmpty({
  String msg = "No data",
  Widget? image,
  bool center = true,
  Color textColor = const Color(0xffffffff),
}) {
  List<Widget> list = [];
  list.add(SizedBox(height: height(center ? 40.w : 0.w)));
  list.add(
    image ??
        Image.asset(
          'sc_images/general/sc_icon_loading.png',
          width: 121.w,
          fit: BoxFit.fitWidth,
        ),
  );
  list.add(SizedBox(height: height(10.w)));
  list.add(
    Text(
      msg,
      style: TextStyle(
        fontSize: sp(12),
        color: textColor,
        fontWeight: FontWeight.w400,
        decoration: TextDecoration.none,
        height: 1,
      ),
    ),
  );
  return Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment:
            center ? MainAxisAlignment.center : MainAxisAlignment.start,
        children: list,
      ),
    ],
  );
}

msgRoleTag(String role, {double? width, double? height}) {
  String image = "";
  if (SCRoomRolesType.HOMEOWNER.name == role) {
    image = 'fz';
  } else if (SCRoomRolesType.ADMIN.name == role) {
    image = "gly";
  } else if (SCRoomRolesType.MEMBER.name == role) {
    image = "hy";
  } else if (SCRoomRolesType.TOURIST.name == role) {
    image = "guest";
  }
  if (image.isEmpty) return SizedBox.shrink();
  return Container(
    alignment: Alignment.center,
    width: width ?? 22.w,
    height: height ?? 22.w,
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage("sc_images/room/sc_icon_room_$image.png"),
      ),
    ),
  );
}

///性别 0女 1男
xb(num? sex, {double? height, Color? color = Colors.white}) {
  //  print('sex：$sex');
  // if (sex == null) return Container();
  String image = "sc_images/login/sc_icon_sex_man.png";
  if (sex == 0) {
    image = "sc_images/login/sc_icon_sex_woman.png";
  }
  return Image.asset(
    image,
    height: height ?? 14.w,
    fit: BoxFit.fill,
    color: color,
  );
}

xb2(num? sex, {double? width, double? height}) {
  //  print('sex：$sex');
  if (sex == null) return Container();
  String image = "sc_images/login/sc_icon_sex_man_bg.png";
  if (sex == 0) {
    image = "sc_images/login/sc_icon_sex_woman_bg.png";
  }
  return Image.asset(
    image,
    width: width ?? 28.w,
    height: height ?? 14.w,
    fit: BoxFit.fill,
  );
}

///搜索框
searchWidget({
  Function? onChange,
  Function? onTap,
  String? hint,
  double? hintSize,
  Color? borderColor,
  EdgeInsetsGeometry? padding,
  TextEditingController? controller,
  Color? textColor,
  Color? serachIconColor,
  FocusNode? focusNode,
}) {
  return GestureDetector(
    behavior: HitTestBehavior.opaque,
    onTap: () {
      if (onTap != null) {
        onTap();
      }
    },
    child: Container(
      margin:
          padding ??
          EdgeInsetsDirectional.only(start: width(15), end: width(0)),
      alignment: AlignmentDirectional.centerStart,
      height: width(36),
      //width: width(345),
      decoration: BoxDecoration(
        color: const Color(0xff18F2B1).withValues(alpha: 0.1),
        borderRadius: BorderRadius.all(Radius.circular(height(20))),
        border: Border.all(color: borderColor ?? Colors.black12, width: 1.w),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(width: width(10)),
          Image.asset(
            "sc_images/index/sc_icon_serach2.png",
            width: 20.w,
            color: serachIconColor ?? Color(0xff18F2B1),
            height: 20.w,
          ),
          SizedBox(width: width(6)),
          _buildPhoneInput(
            onChange,
            onTap == null,
            hint ?? 'Search by ID',
            controller,
            textColor,
            hintSize,
            focusNode,
          ),
        ],
      ),
    ),
  );
}

_buildPhoneInput(
  Function? onChange,
  bool enabled,
  String tint,
  TextEditingController? controller,
  Color? textColor,
  double? hintSize,
  FocusNode? focusNode,
) {
  return Expanded(
    child: TextField(
      controller: controller,
      onChanged: (text) {
        if (onChange != null) {
          onChange(text);
        }
      },
      focusNode: focusNode,
      autofocus: false,
      enabled: enabled,
      maxLength: 11,
      cursorColor: SocialChatTheme.primaryColor,
      decoration: InputDecoration(
        hintText: tint,
        hintStyle: TextStyle(
          color: SocialChatTheme.textSecondary,
          fontSize: hintSize ?? sp(14),
        ),
        counterText: '',
        isDense: true,
        filled: false,
        focusColor: Colors.transparent,
        hoverColor: Colors.transparent,
        border: InputBorder.none,
        enabledBorder: InputBorder.none,
        focusedBorder: InputBorder.none,
        disabledBorder: InputBorder.none,
        errorBorder: InputBorder.none,
        focusedErrorBorder: InputBorder.none,
        contentPadding: EdgeInsets.only(bottom: 0),
      ),
      style: TextStyle(
        textBaseline: TextBaseline.alphabetic,
        fontSize: hintSize ?? sp(14),
        color: textColor ?? Colors.black,
      ),
    ),
  );
}

///vip标识
getVIPBadge(String? type, {double? width, double? height}) {
  return Container();
}

///用户等级
getUserLevel(num level, {double? width, double? height, double? fontSize}) {
  String icon = "";
  if (level > 0 && level < 11) {
    icon = "sc_images/level/sc_icon_user_level_1_10.png";
  } else if (level > 10 && level < 21) {
    icon = "sc_images/level/sc_icon_user_level_10_20.png";
  } else if (level > 20 && level < 31) {
    icon = "sc_images/level/sc_icon_user_level_20_30.png";
  } else if (level > 30 && level < 41) {
    icon = "sc_images/level/sc_icon_user_level_30_40.png";
  } else if (level > 40 && level < 51) {
    icon = "sc_images/level/sc_icon_user_level_40_50.png";
  }
  return icon.isNotEmpty
      ? Directionality(
        textDirection: TextDirection.ltr,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Image.asset(icon, width: width, height: height, fit: BoxFit.fill),
            PositionedDirectional(
              end: level > 9 ? 10.w : 15.w,
              child: text(
                "$level",
                fontSize: fontSize ?? 12.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        ),
      )
      : Container();
}

///用户魅力等级
getWealthLevel(num level, {double? width, double? height, double? fontSize}) {
  String icon = "";
  if (level > 0 && level < 11) {
    icon = "sc_images/level/sc_icon_wealth_level_1_10.png";
  } else if (level > 10 && level < 21) {
    icon = "sc_images/level/sc_icon_wealth_level_10_20.png";
  } else if (level > 20 && level < 31) {
    icon = "sc_images/level/sc_icon_wealth_level_20_30.png";
  } else if (level > 30 && level < 41) {
    icon = "sc_images/level/sc_icon_wealth_level_30_40.png";
  } else if (level > 40 && level < 51) {
    icon = "sc_images/level/sc_icon_wealth_level_40_50.png";
  }
  return icon.isNotEmpty
      ? Directionality(
        textDirection: TextDirection.ltr,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Image.asset(icon, width: width, height: height, fit: BoxFit.fill),
            PositionedDirectional(
              end: level > 9 ? 10.w : 15.w,
              child: text(
                "$level",
                fontSize: fontSize ?? 12.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        ),
      )
      : Container();
}

String getGiftObtCoinsBg(num obtCoins) {
  String icon = "";
  if (obtCoins > -1 && obtCoins < 5000) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_0.png";
  } else if (obtCoins > 4999 && obtCoins < 50000) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_1.png";
  } else if (obtCoins > 49999 && obtCoins < 100000) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_2.png";
  } else if (obtCoins > 99999 && obtCoins < 1000000) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_3.png";
  } else if (obtCoins > 999999 && obtCoins < 10000000) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_4.png";
  } else if (obtCoins > 9999999) {
    icon = "sc_images/room/sc_icon_luck_gift_obt_coins_bg_5.png";
  }
  return icon;
}

///数字图片
buildNum(String num, {double? size = 18}) {
  if (num.isNotEmpty) {
    List<Widget> eList = [];
    int index = 0;
    num.split("").forEach((item) {
      if (item == "k") {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.9).w,
            ),
            child: Image.asset(
              "sc_images/room/sc_icon_k.png",
              width: size,
              height: size,
            ),
          ),
        );
      } else if (item == "m") {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.9).w,
            ),
            child: Image.asset(
              "sc_images/room/sc_icon_m.png",
              width: size,
              height: size,
            ),
          ),
        );
      } else {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.8).w,
            ),
            child: Image.asset(
              "sc_images/room/sc_icon_number_$item.png",
              width: size,
              height: size,
            ),
          ),
        );
      }
      index = index + 1;
    });
    return Directionality(
      textDirection: TextDirection.ltr,
      child: Stack(children: eList),
    );
  } else {
    return Container();
  }
}

///游戏飘屏数字图片
buildNumForGame(String num, {double? size = 18}) {
  if (num.isNotEmpty) {
    List<Widget> eList = [];
    int index = 0;
    num.split("").forEach((item) {
      if (item == "k") {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.65).w,
            ),
            child: Image.asset(
              "sc_images/general/sc_icon_game_numk.png",
              width: size,
              height: size,
            ),
          ),
        );
      } else if (item == "m") {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.65).w,
            ),
            child: Image.asset(
              "sc_images/general/sc_icon_game_numm.png",
              width: size,
              height: size,
            ),
          ),
        );
      } else {
        eList.add(
          Container(
            margin: EdgeInsetsDirectional.only(
              start: (index * (size ?? 18) * 0.6).w,
            ),
            child: Image.asset(
              "sc_images/general/sc_icon_game_num$item.png",
              width: size,
              height: size,
            ),
          ),
        );
      }
      index = index + 1;
    });
    return Directionality(
      textDirection: TextDirection.ltr,
      child: Stack(children: eList),
    );
  } else {
    return Container();
  }
}

///房间幸运数字
buildNumForRoomLuckNum(String num, {double? size = 18}) {
  if (num.isNotEmpty) {
    List<Widget> eList = [];
    int index = 0;
    num.split("").forEach((item) {
      eList.add(
        Container(
          margin: EdgeInsetsDirectional.only(
            start: (index * (size ?? 18) * 0.7).w,
          ),
          child: Image.asset(
            "sc_images/room/sc_icon_lucknumber_$item.png",
            width: size,
            height: size,
          ),
        ),
      );
      index = index + 1;
    });
    return Directionality(
      textDirection: TextDirection.ltr,
      child: Stack(children: eList),
    );
  } else {
    return Container();
  }
}

buildFamilyProgressLinearGradient(int level) {
  if (level == 0) {
    return LinearGradient(colors: [Color(0xff2E4957), Color(0xffC9E7E8)]);
  } else if (level == 1) {
    return LinearGradient(colors: [Color(0xff5A3F31), Color(0xffF8CD85)]);
  } else if (level == 2) {
    return LinearGradient(colors: [Color(0xff0D5ACE), Color(0xffCBD9E8)]);
  } else if (level == 3) {
    return LinearGradient(colors: [Color(0xffC57E1C), Color(0xffCBD9E8)]);
  } else if (level == 10) {
    return LinearGradient(colors: [Color(0xff3C7FBE), Color(0xffADE1F1)]);
  } else if (level == 11) {
    return LinearGradient(colors: [Color(0xff300A67), Color(0xffA53EFF)]);
  }
  return LinearGradient(colors: [Color(0xff2E4957), Color(0xffC9E7E8)]);
}

buildVipNameColor(String type) {
  return null;
}
