import 'package:cached_network_image/cached_network_image.dart';
import 'package:didvan/config/design_config.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';

class SkeletonImage extends StatelessWidget {
  final String imageUrl;
  final double? width;
  final double? height;
  final BorderRadius? borderRadius;
  final double? aspectRatio;
  const SkeletonImage({
    Key? key,
    required this.imageUrl,
    this.borderRadius = DesignConfig.lowBorderRadius,
    this.aspectRatio,
    this.width,
    this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _aspectRatioGenerator(
      child: CachedNetworkImage(
        imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
        httpHeaders: {'Authorization': 'Bearer ${RequestService.token}'},
        width: width,
        height: height,
        imageUrl: RequestHelper.baseUrl + imageUrl,
        imageBuilder: (context, imageProvider) => ClipRRect(
          borderRadius: borderRadius ?? DesignConfig.lowBorderRadius,
          child: Image(
            image: imageProvider,
            fit: BoxFit.cover,
          ),
        ),
        progressIndicatorBuilder: (context, url, progress) =>
            ShimmerPlaceholder(
          borderRadius: borderRadius,
        ),
      ),
    );
  }

  Widget _aspectRatioGenerator({required Widget child}) => aspectRatio == null
      ? SizedBox(key: ValueKey(imageUrl), child: child)
      : AspectRatio(
          key: ValueKey(imageUrl),
          aspectRatio: aspectRatio!,
          child: child,
        );
}
