import 'dart:io';

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

class CustomProofImageUploader extends StatelessWidget {
  final String? imagePath;
  final String title;
  final VoidCallback onTapImageAdd;
  final VoidCallback onTapImageRemove;
  const CustomProofImageUploader({
    super.key,
    required this.imagePath,
    required this.title,
    required this.onTapImageAdd,
    required this.onTapImageRemove,
  });

  @override
  Widget build(BuildContext context) {
    final File imageFile = File(imagePath ?? "image not found");

    final bool imageFileExists = imageFile.existsSync();

    return Stack(
      clipBehavior: Clip.none,
      children: [
        Container(
          width: double.infinity,
          height: 25.h,
          decoration: BoxDecoration(
            border: Border.all(
              color: Colors.grey.shade300,
              style: BorderStyle.solid,
            ),
            borderRadius: BorderRadius.circular(2.5.w),
            color: Colors.white,
          ),
          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,
                      );
                    },
                  ),
                )
              : GestureDetector(
                  onTap: onTapImageAdd,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.camera_alt_outlined,
                        color: Colors.grey,
                        size: 8.w,
                      ),
                      SizedBox(height: 0.5.h),
                      Text(
                        title,
                        style: TextStyle(
                          color: Colors.grey[600],
                          fontSize: AppFontSize.medium.sp,
                        ),
                      ),
                    ],
                  ),
                ),
        ),

        if (imagePath != null)
          Positioned(
            top: 1.w,
            right: 1.w,
            child: Material(
              color: Colors.transparent,
              type: MaterialType.transparency,
              child: InkWell(
                onTap: onTapImageRemove,
                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),
                ),
              ),
            ),
          ),
      ],
    );
  }
}
