import 'dart:io';

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

class CustomSelectableItem extends StatelessWidget {
  final String id;
  final String? imagePath;
  final String title;
  final String subtitle;
  final bool isSelected;
  final VoidCallback onTap;
  const CustomSelectableItem({
    super.key,
    required this.id,
    this.imagePath,
    required this.title,
    required this.subtitle,
    required this.isSelected,
    required this.onTap,
  });

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

    final bool imagePathAvailable = imagePath != null && imagePath!.isNotEmpty;

    final File imageFile = File(
      imagePathAvailable ? imagePath! : "image not found",
    );

    final bool imageFileExists = imageFile.existsSync();
    return Container(
      margin: EdgeInsets.only(bottom: 1.5.h),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(2.5.w),
        border: Border.all(color: Colors.grey.shade200, width: 1),
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(2.5.w),
        child: Padding(
          padding: EdgeInsets.all(4.w),
          child: Row(
            children: [
              Icon(
                isSelected
                    ? Icons.radio_button_checked
                    : Icons.radio_button_off,
                color: Colors.grey.shade700,
                size: 5.5.w,
              ),
              SizedBox(width: 4.w),
              Container(
                width: 12.w,
                height: 12.w,
                decoration: BoxDecoration(
                  color: Colors.grey[100],
                  borderRadius: BorderRadius.circular(2.w),
                ),
                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_outlined,
                        size: 8.w,
                        color: Colors.grey[400],
                      ),
              ),
              SizedBox(width: 3.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: isTablet
                            ? AppFontSize.medium.sp
                            : AppFontSize.small.sp,
                      ),
                    ),
                    SizedBox(height: 0.75.h),
                    Text(
                      subtitle,
                      style: TextStyle(
                        color: Colors.grey.shade700,
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1.25).sp
                            : (AppFontSize.small - 1.25).sp,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
