import 'dart:io';

import 'package:flutter/material.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/enums/user_role.dart';
import 'package:niogu_app/features/goods/products/presentation/widgets/product_variant_model.dart';
import 'package:sizer/sizer.dart';

class ImageUploaderProductVariant extends StatelessWidget {
  final UserRole userRole;
  final bool isMainOutlet;
  final ProductVariantModel variant;
  final VoidCallback onTapVariantImageAdd;
  final VoidCallback onTapVariantImageRemove;

  const ImageUploaderProductVariant({
    super.key,
    required this.userRole,
    required this.isMainOutlet,
    required this.variant,
    required this.onTapVariantImageAdd,
    required this.onTapVariantImageRemove,
  });

  @override
  Widget build(BuildContext context) {
    final bool hasImage =
        variant.imagePath != null && variant.imagePath!.isNotEmpty;

    if (hasImage) {
      final File imageFile = File(variant.imagePath ?? "image not found");
      final bool imageFileExists = imageFile.existsSync();

      return Stack(
        clipBehavior: Clip.none,
        children: [
          Container(
            width: 25.w,
            height: 25.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(2.5.w),
              border: Border.all(color: Colors.grey.shade300),
            ),
            child: imageFileExists
                ? ClipRRect(
                    borderRadius: BorderRadius.circular(2.5.w),
                    child: Image.file(
                      imageFile,
                      fit: BoxFit.cover,
                      width: double.infinity,
                      height: double.infinity,
                      errorBuilder: (context, error, stackTrace) {
                        return Icon(
                          Icons.error,
                          color: Colors.grey[400],
                          size: 8.w,
                        );
                      },
                    ),
                  )
                : Icon(Icons.image, color: Colors.grey[400], size: 8.w),
          ),

          if (userRole == UserRole.owner && isMainOutlet)
            Positioned(
              top: 1.w,
              right: 1.w,
              child: Material(
                color: Colors.transparent,
                type: MaterialType.transparency,
                child: InkWell(
                  onTap: onTapVariantImageRemove,
                  child: Container(
                    padding: EdgeInsets.all(1.w),
                    decoration: BoxDecoration(
                      color: Colors.red[50],
                      shape: BoxShape.circle,
                      border: Border.all(color: Colors.red.withOpacity(0.2)),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.1),
                          blurRadius: 2,
                          offset: Offset(0, 1),
                        ),
                      ],
                    ),
                    child: Icon(Icons.remove, color: Colors.red, size: 4.w),
                  ),
                ),
              ),
            ),
        ],
      );
    } else {
      return GestureDetector(
        onTap: onTapVariantImageAdd,
        child: Container(
          width: 25.w,
          height: 25.w,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(2.5.w),
            border: Border.all(color: Colors.grey.shade300),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                userRole == UserRole.owner && isMainOutlet
                    ? Icons.camera_alt_outlined
                    : Icons.image_outlined,
                color: Colors.grey,
                size: 6.w,
              ),
              if (userRole == UserRole.owner && isMainOutlet) ...[
                SizedBox(height: 0.5.h),
                Text(
                  "Foto",
                  style: TextStyle(
                    fontSize: AppFontSize.small.sp,
                    color: Colors.grey,
                  ),
                ),
              ],
            ],
          ),
        ),
      );
    }
  }
}
