import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_image_animated_placeholder.dart';

class UserAvatarWidget extends StatelessWidget {
  final String? imageUrl;
  final String userName;
  final double? rating;

  const UserAvatarWidget({
    super.key,
    this.imageUrl,
    required this.userName,
    required this.rating,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      clipBehavior: Clip.none,
      children: [
        _buildUserPhoto(imageUrl, userName),
        _buildEditButton(context),
        if(rating!=null && rating! > 0)
          _buildUserRating(),
      ],
    );
  }

  Container _buildUserPhoto(String? imageUrl, String? userName) {
    return Container(
      width: 96,
      height: 96,
      decoration: const BoxDecoration(
        shape: BoxShape.circle,
        color: AppColors.darkBgPrimaryFrame,
      ),
      child: ClipOval(
        child: Image.network(
          imageUrl ?? '',
          fit: BoxFit.cover,
          width: 96,
          height: 96,
          loadingBuilder: (context, child, chunkEvent) {
            if (chunkEvent?.expectedTotalBytes ==
                chunkEvent?.cumulativeBytesLoaded) {
              return child;
            }

            return const KwAnimatedImagePlaceholder();
          },
          errorBuilder: (context, error, trace) {
            return Center(
              child: Text(
                getInitials(userName),
                style: AppTextStyles.headingH1.copyWith(
                  color: Colors.white,
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Positioned _buildEditButton(BuildContext context) {
    return Positioned(
      bottom: 0,
      left: -5,
      child: GestureDetector(
        onTap: () {
          context.router.push(PersonalInfoRoute(isInEditMode: true));
        },
        child: Container(
          width: 28,
          height: 28,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: Colors.white,
            border: Border.all(color: AppColors.bgColorDark, width: 2),
          ),
          child: Center(
            child: Assets.images.userProfile.editPhoto.svg(),
          ),
        ),
      ),
    );
  }

  _buildUserRating() {
    return Positioned(
      bottom: 0,
      right: -20,
      child: Container(
        height: 28,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(14),
          color: Colors.white,
          border: Border.all(color: AppColors.bgColorDark, width: 2),
        ),
        child: Row(
          children: [
            const SizedBox(width: 8),
            Assets.images.userProfile.star.svg(width: 16, height: 16),
            const SizedBox(width: 4),
            Text(
              rating.toString(),
              style: AppTextStyles.bodyTinyMed.copyWith(
                color: AppColors.bgColorDark,
              ),
            ),
            const SizedBox(width: 8),
          ],
        ),
      ),
    );
  }

  String getInitials(String? name) {
    try {
      if (name == null || name.isEmpty) return ' ';
      List<String> nameParts = name.split(' ');
      if (nameParts.length == 1) {
        return nameParts[0].substring(0, 1).toUpperCase();
      }
      return (nameParts[0][0] + nameParts[1][0]).toUpperCase();
    } catch (e) {
      return ' ';
    }
  }
}
