import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

import '../../layouts/responsive_layout.dart';
import 'multi_fileUploadService.dart';

class MultiFileUploadWidget extends StatefulWidget {
  const MultiFileUploadWidget({super.key});

  @override
  State<MultiFileUploadWidget> createState() => _MultiFileUploadWidgetState();

  static bool hasFiles = false; // ✅ Static variable to check from parent
}

class _MultiFileUploadWidgetState extends State<MultiFileUploadWidget> {
  final fileService = MultiFileUploadService();
  String? errorMessage;

  void _pickFiles() async {
    final error = await fileService.pickFiles(
      maxFileSizeInMB: 10,
    ); // 5 MB limit
    if (error != null) {
      if (mounted) {
        setState(() {
          errorMessage = error;
        });

        // also show alert dialog for big error messages
        // showDialog(
        //   context: context,
        //   builder: (ctx) => AlertDialog(
        //     title: const Text("File Upload Error"),
        //     content: Text(error),
        //     actions: [
        //       TextButton(
        //         onPressed: () => Navigator.pop(ctx),
        //         child: const Text("OK"),
        //       ),
        //     ],
        //   ),
        // );
      }
    } else {
      setState(() {
        errorMessage = null;
        MultiFileUploadWidget.hasFiles = fileService.files.isNotEmpty;
      });
    }
  }

  void _removeFile(int index) {
    fileService.removeFileAt(index);
    setState(() {
      MultiFileUploadWidget.hasFiles = fileService.files.isNotEmpty;
    });
  }

  @override
  Widget build(BuildContext context) {
    final files = fileService.files;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // if (ResponsiveLayout.isMobile(context)) ...[
        OutlinedButton.icon(
          onPressed: _pickFiles,
          icon: const Icon(
            Icons.file_upload_outlined,
            color: Color(0xFF2E7D6E),
            size: 18,
          ),
          label: const Text(
            "Upload Documents",
            style: TextStyle(fontSize: 11, color: Colors.black),
            overflow: TextOverflow.ellipsis,
          ),
          style: OutlinedButton.styleFrom(
            side: const BorderSide(color: Color(0xFF2E7D6E)),
          ),
        ),
        const SizedBox(height: 3),
        Text(
          "Supports only PDF, PNG, JPG, JPEG,HEIC formats (max 10 MB each)",
          style: GoogleFonts.poppins(fontSize: 8, color: Colors.grey),
        ),
        // ]
        // else ...[
        //   Row(
        //     children: [
        //       OutlinedButton.icon(
        //         onPressed: _pickFiles,
        //         icon: const Icon(
        //           Icons.file_upload_outlined,
        //           color: Color(0xFFE26728),
        //           size: 24,
        //         ),
        //         label: const Text(
        //           "Upload Documents",
        //           style: TextStyle(fontSize: 14, color: Colors.black),
        //           overflow: TextOverflow.ellipsis,
        //         ),
        //         style: OutlinedButton.styleFrom(
        //           side: const BorderSide(color: Color(0xFFE26728)),
        //         ),
        //       ),
        //       const SizedBox(width: 12),
        //       // const Expanded(
        //       //   child: Text(
        //       //     "Supports only PDF, PNG, JPG, JPEG,HEIC formats (max 10 MB each)",
        //       //     style: TextStyle(fontSize: 12, color: Colors.grey),
        //       //     overflow: TextOverflow.ellipsis,
        //       //   ),
        //       // ),
        //     ],
        //   ),
        // ],

        // if (fileService.files.isEmpty && errorMessage == null) ...[
        //   const SizedBox(height: 4),
        //   const Text(
        //     "Required",
        //     style: TextStyle(color: Colors.red, fontSize: 12),
        //   ),
        // ],
        if (errorMessage != null) ...[
          const SizedBox(height: 4),
          Text(
            errorMessage!,
            style: GoogleFonts.poppins(color: Colors.red, fontSize: 12),
          ),
        ],

        const SizedBox(height: 8),

        ...fileService.files.asMap().entries.map((entry) {
          final index = entry.key;
          final uploaded = entry.value; // UploadedFile

          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ListTile(
                dense: true,
                contentPadding: EdgeInsets.zero,
                title: Text(
                  uploaded.file.name,
                  style: GoogleFonts.poppins(fontSize: 11),
                ),
                trailing: IconButton(
                  padding: EdgeInsets.zero,
                  splashRadius: 2,
                  icon: const Icon(Icons.close, color: Colors.red, size: 14),
                  onPressed: () => _removeFile(index),
                ),
              ),
              // Padding(
              //   padding: const EdgeInsets.only(
              //     left: 8.0,
              //     bottom: 8.0,
              //     right: 8.0,
              //   ),
              //   child: TextField(
              //     controller: uploaded.controller,
              //     style: GoogleFonts.poppins(fontSize: 11),
              //     decoration: InputDecoration(
              //       labelText: 'Enter document name',
              //       labelStyle: GoogleFonts.poppins(fontSize: 11),
              //
              //       border: const OutlineInputBorder(),
              //       isDense: true,
              //     ),
              //   ),
              // ),
            ],
          );
        }),
      ],
    );
  }
}
