import 'dart:io';
import 'package:flutter/material.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:sizer/sizer.dart';

class ImageUploaderProductCategory extends StatelessWidget {
  final String? categoryImage;
  final VoidCallback onTapCategoryImageAdd;
  final VoidCallback onTapCategoryImageRemove;

  const ImageUploaderProductCategory({
    super.key,
    this.categoryImage,
    required this.onTapCategoryImageAdd,
    required this.onTapCategoryImageRemove,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;

    final bool hasImage = categoryImage != null && categoryImage!.isNotEmpty;

    if (hasImage) {
      final File imageFile = File(categoryImage ?? "image not found");

      final bool imageFileExists = imageFile.existsSync();

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

          Positioned(
            top: 1.w,
            right: 1.w,
            child: Material(
              color: Colors.transparent,
              type: MaterialType.transparency,
              child: InkWell(
                onTap: onTapCategoryImageRemove,
                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 Material(
        type: MaterialType.transparency,
        color: Colors.white,
        child: GestureDetector(
          onTap: onTapCategoryImageAdd,
          child: Container(
            width: 15.h,
            height: 15.h,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(2.5.w),
              border: Border.all(color: Colors.grey.shade300),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.add_a_photo_rounded,
                  color: AppColor.primaryColor,
                  size: 7.w,
                ),
                SizedBox(height: 0.5.h),
                Text(
                  "Tambah\nFoto",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                    color: Colors.grey[600],
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }
  }
}
