import 'package:flutter/cupertino.dart';

import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../widget/avatar/avatar_image_widget.dart';
import '../../user_info/enums/gender_type.dart';

/// 亲密用户组件，包含2个头像和中间一个心形图标
class IntimacyUserWidget extends StatelessWidget {
  /// 组件的宽度
  final double width;

  /// 组件的高度
  final double height;

  /// 头像的大小
  final double avatarSize;

  /// 头像的边框宽度
  final double avatarBorderWidth;

  /// 爱心的大小
  final double loveSize;

  /// 我的头像
  final String myAvatar;

  /// Ta的头像
  final String taAvatar;

  /// 我的性别
  final GenderType? myGender;

  /// 他的性别
  final GenderType? taGender;

  const IntimacyUserWidget({
    super.key,
    required this.width,
    required this.height,
    required this.avatarSize,
    required this.avatarBorderWidth,
    required this.loveSize,
    required this.myAvatar,
    required this.taAvatar,
    required this.myGender,
    required this.taGender,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: Stack(
        // 默认内容都居中
        alignment: Alignment.center,
        children: [
          // 用户1
          Positioned(
            left: 0,
            child: _buildAvatarWithGender(myAvatar, myGender),
          ),
          // 用户2
          Positioned(
            // 距离左侧的距离
            left: avatarSize - (avatarBorderWidth * 2),
            child: _buildAvatarWithGender(taAvatar, taGender),
          ),
          // 爱心
          Positioned(
            // 位置，在2个头像的中间
            left: avatarSize - (avatarBorderWidth * 2) - (loveSize / 2),
            child: Assets.images.iconIntimacyAnalyseLove.image(
              width: loveSize,
              height: loveSize,
            ),
          ),
        ],
      ),
    );
  }

  /// 用户头像
  /// [gender] 性别
  Widget _buildAvatarWithGender(String? imageUrl, GenderType? gender) {
    ImageProvider defaultImage;
    if (gender == GenderType.male) {
      // 男
      defaultImage = Assets.images.iconDefaultAvatarMale.image().image;
    } else if (gender == GenderType.female) {
      // 女
      defaultImage = Assets.images.iconDefaultAvatarFemale.image().image;
    } else {
      // 未设置
      defaultImage = Assets.images.iconDefaultAvatarUnknown.image().image;
    }
    return _buildAvatar(imageUrl, defaultImage: defaultImage);
  }

  /// 圆形头像
  Widget _buildAvatar(String? imageUrl, {ImageProvider? defaultImage}) {
    return CircleAvatarWidget(
      imageUrl: imageUrl,
      placeholderImage: defaultImage,
      borderColor: ColorName.white,
      borderWidth: avatarBorderWidth,
      backgroundColor: ColorName.white,
      size: avatarSize,
    );
  }
}
