import 'package:cached_network_image/cached_network_image.dart';
import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.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/foundation.dart';
import 'package:flutter/material.dart';
// ignore: depend_on_referenced_packages
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 double? pWidth;
  final double? pHeight;
  final BorderRadius? borderRadius;
  final double? aspectRatio;
  const SkeletonImage({
    Key? key,
    required this.imageUrl,
    this.borderRadius = DesignConfig.lowBorderRadius,
    this.aspectRatio,
    this.width,
    this.height,
    this.pWidth,
    this.pHeight,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _aspectRatioGenerator(
      child: ClipRRect(
        borderRadius: borderRadius ?? BorderRadius.zero,
        child: CachedNetworkImage(
          errorWidget: (context, url, error) {
            if (kDebugMode) {
              print('image fetch compelete with Error: $error');
            }
            return Container(
                decoration: BoxDecoration(
                    color: Theme.of(context).colorScheme.disabledBackground),
                child: const Icon(Icons.image_not_supported_outlined));
          },
          errorListener: (value) {},
          fit: kIsWeb? BoxFit.fill:BoxFit.cover,
          imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
          httpHeaders: {'Authorization': 'Bearer ${RequestService.token}'},
          width: width,
          height: height,
          imageUrl: imageUrl.startsWith('http')
              ? imageUrl.replaceAll('\n', '')
              : RequestHelper.baseUrl + imageUrl.replaceAll('\n', ''),
          placeholder: (context, _) => ShimmerPlaceholder(
            width: pWidth,
            height: pHeight,
          ),
        ),
      ),
    );
  }

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