import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/utils/common_style.dart';

import '../../data/bean/user_info.dart';
import '../../data/consts/constants.dart';
import '../../resource/assets.gen.dart';
import '../../resource/colors.gen.dart';
import '../../utils/date_util.dart';

Widget buildFriendItem(UserInfo userInfo, bool isMemberExpired,
    {VoidCallback? onEditClick, VoidCallback? viewTraceClick}) {
  return Container(
    margin: EdgeInsets.only(left: 12.w, right: 12.w, bottom: 10.w),
    width: double.infinity,
    decoration: BoxDecoration(boxShadow: [
      BoxShadow(
        color: ColorName.black.withOpacity(0.04),
        offset: Offset(2, 2),
        blurRadius: 8,
      )
    ], color: ColorName.white, borderRadius: BorderRadius.circular(14.w)),
    child: Stack(
      children: [
        Positioned(
            top: 0,
            right: 0,
            child: Assets.images.bgFriendItem.image(width: 190.w)),
        Column(
          children: [
            SizedBox(height: 14.w),
            Row(
              children: [
                SizedBox(width: 12.w),
                buildCustomAvatarOrDefaultAvatarView(
                    size: 48.w,
                    avatar: userInfo.avatar,
                    isMine: userInfo.isMine == true),
                SizedBox(width: 8.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text((userInfo.getUserNickName()),
                              style: TextStyle(
                                  fontSize: 16.sp,
                                  color: '#202020'.color,
                                  fontWeight: FontWeight.bold)),
                          SizedBox(width: 6.w),
                          Visibility(
                            visible: userInfo.virtual == true,
                            child: Container(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 5.w, vertical: 2.w),
                              decoration: BoxDecoration(
                                  gradient: LinearGradient(colors: [
                                    '#FF4A4F'.color,
                                    '#FF554A'.color
                                  ]),
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(8.w),
                                      bottomRight: Radius.circular(10.w),
                                      topRight: Radius.circular(10.w),
                                      bottomLeft: Radius.circular(2.w))),
                              child: Text(StringName.traceFreeExperience,
                                  style: TextStyle(
                                    height: 1,
                                    fontSize: 12.sp,
                                    color: ColorName.white,
                                  )),
                            ),
                          ),
                          Spacer(),
                          Visibility(
                            visible: userInfo.isMine != true &&
                                userInfo.virtual != true,
                            child: GestureDetector(
                              onTap: () {
                                onEditClick?.call();
                              },
                              child: Assets.images.iconFriendEdit
                                  .image(width: 22.w, height: 22.w),
                            ),
                          ),
                          SizedBox(width: 16.w),
                        ],
                      ),
                      Text(
                          userInfo.lastLocation.value?.lastUpdateTime == null
                              ? StringName.friendLocationTimeUnknown
                              : DateUtil.fromMillisecondsSinceEpoch(
                                  "yyyy-MM-dd HH:mm",
                                  userInfo.lastLocation.value?.lastUpdateTime ??
                                      0),
                          style: TextStyle(
                              fontSize: 13.sp, color: '#A7A7A7'.color)),
                    ],
                  ),
                )
              ],
            ),
            SizedBox(height: 12.w),
            Row(
              children: [
                SizedBox(width: 12.w),
                Expanded(
                  child: Container(
                    constraints: BoxConstraints(minHeight: 34.w),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: ImageFiltered(
                        enabled: userInfo.blockedMe == true ||
                            (isMemberExpired && !(userInfo.isMine == true)),
                        imageFilter: ImageFilter.blur(
                          sigmaX: Constants.blurredX,
                          sigmaY: Constants.blurredY,
                        ),
                        child: Text(
                            userInfo.lastLocation.value?.address ??
                                StringName.unopenedPositioning,
                            style: TextStyle(
                                fontSize: 13.sp, color: '#404040'.color)),
                      ),
                    ),
                  ),
                ),
                SizedBox(width: 20.w),
                GestureDetector(
                  onTap: () {
                    viewTraceClick?.call();
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 12.w),
                    decoration: BoxDecoration(
                        color: ColorName.colorPrimary,
                        borderRadius: BorderRadius.circular(36.w)),
                    padding:
                        EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.w),
                    child: Text(
                      StringName.examineTrace,
                      style: TextStyle(fontSize: 12.sp, color: ColorName.white),
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.w)
          ],
        )
      ],
    ),
  );
}
