import 'dart:io';

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

class PaymentProof extends StatelessWidget {
  final String imagePath;
  const PaymentProof({super.key, required this.imagePath});

  @override
  Widget build(BuildContext context) {
    final File imageFile = File(imagePath);

    final bool imageFileExists = imageFile.existsSync();

    return Padding(
      padding: EdgeInsets.all(4.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "Bukti Pembayaran",
            style: TextStyle(
              fontSize: AppFontSize.small.sp,
              fontWeight: FontWeight.bold,
              color: Colors.grey[700],
            ),
          ),
          SizedBox(height: 1.5.h),
          GestureDetector(
            onTap: () {
              if (!imageFileExists) {
                CustomSnackbar.showError(context, "Ups, terjadi kesalahan");
                return;
              }

              showDialog(
                context: context,
                useRootNavigator: true,
                builder: (context) => Dialog(
                  insetPadding: EdgeInsets.symmetric(
                    horizontal: 2.w,
                    vertical: 2.h,
                  ),
                  backgroundColor: Colors.transparent,
                  child: GestureDetector(
                    onTap: () => Navigator.pop(context),
                    child: Container(
                      width: MediaQuery.of(context).size.width,
                      constraints: BoxConstraints(
                        maxWidth: 85.w,
                        maxHeight: 85.h,
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(4.w),
                        child: InteractiveViewer(
                          child: Image.file(
                            File(imagePath),
                            fit: BoxFit.contain,
                            width: double.infinity,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              );
            },
            child: imageFileExists
                ? Container(
                    height: 20.h,
                    width: double.infinity,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.w),
                      image: DecorationImage(
                        image: FileImage(File(imagePath)),
                        fit: BoxFit.cover,
                      ),
                    ),
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(2.w),
                        color: Colors.black.withOpacity(0.2),
                      ),
                      child: const Center(
                        child: Icon(
                          Icons.fullscreen_rounded,
                          color: Colors.white,
                          size: 30,
                        ),
                      ),
                    ),
                  )
                : Container(
                    height: 20.h,
                    width: double.infinity,
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.grey.shade300,
                        style: BorderStyle.solid,
                      ),
                      borderRadius: BorderRadius.circular(2.w),
                      color: Colors.white,
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.error, color: Colors.grey, size: 8.w),
                        SizedBox(height: 0.5.h),
                        Text(
                          "Ups, Terjadi Kesalahan",
                          style: TextStyle(
                            color: Colors.grey.shade600,
                            fontSize: AppFontSize.medium.sp,
                          ),
                        ),
                      ],
                    ),
                  ),
          ),
        ],
      ),
    );
  }
}
