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

import 'fileUploadService.dart';
import 'multi_fileUploadService.dart';

class ThemedMultiFileUploadField extends StatefulWidget {
  final String? hintText;
  final double? txtwidth;
  final double? txtheight;
  final Color? backgroundColor;
  final Color? borderColor;
  final Color? errorBorderColor;
  final Function(String fileName, PlatformFile file)? onFileSelected;
  FormFieldValidator<String>? validator;
  final List<String>? allowedExtensions;
  final bool isTxtBtnCase;
  final String? txtName;
  final double? padVertical;
  final double? padHorizontal;

  ThemedMultiFileUploadField({
    super.key,
    this.hintText,
    this.txtwidth,
    this.txtheight,
    this.backgroundColor,
    this.borderColor,
    this.errorBorderColor,
    this.onFileSelected,
    this.validator,
    this.allowedExtensions,
    this.isTxtBtnCase = false,
    this.txtName,
    this.padVertical,
    this.padHorizontal,
  });

  @override
  State<ThemedMultiFileUploadField> createState() =>
      _ThemedMultiFileUploadFieldState();
}

class _ThemedMultiFileUploadFieldState
    extends State<ThemedMultiFileUploadField> {
  final fileService = MultiFileUploadService();
  String? selectedFileName;
  String? errorMessage;

  Future<void> _pickFile() async {
    final error = await fileService.pickFiles(
      maxFileSizeInMB: 3,
      // allowedExtensions: widget.allowedExtensions,
    );

    if (error != null) {
      setState(() => errorMessage = error);
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text(error)));
    } else if (fileService.pickFiles != null) {
      print('Files Picked ');
      print('Files Picked fileService - ${fileService.pickFiles}');

      // setState(() {
      //   selectedFileName = fileService.pickFiles!.name;
      //   errorMessage = null;
      // });

      // widget.onFileSelected?.call(
      //   fileService.pickFiles!.name,
      //   fileService.pickFiles!,
      // );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: widget.txtwidth ?? MediaQuery.of(context).size.width,
          height: widget.txtheight,
          child: DecoratedBox(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
            child: InkWell(
              onTap: _pickFile,

              borderRadius: BorderRadius.circular(10),
              child: Container(
                padding: EdgeInsets.symmetric(
                  vertical: widget.padVertical ?? 10,
                  horizontal: widget.padHorizontal ?? 15,
                ),
                decoration: BoxDecoration(
                  color: widget.backgroundColor ?? Colors.white,
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(
                    color: errorMessage != null
                        ? (widget.errorBorderColor ?? Colors.red)
                        : (widget.borderColor ??
                              widget.backgroundColor ??
                              Colors.grey.shade50),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    if (!widget.isTxtBtnCase) ...[
                      Expanded(
                        child: Text(
                          selectedFileName ??
                              widget.hintText ??
                              "Upload Document",
                          style: TextStyle(
                            fontSize: 12,
                            color: errorMessage != null
                                ? Colors.red
                                : Colors.black,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      const Icon(
                        Icons.file_upload_outlined,
                        color: Colors.black,
                      ),
                    ],

                    if (widget.isTxtBtnCase) ...[
                      Center(
                        child: Tooltip(
                          message: 'Upload Documents',
                          child: Text(
                            widget.txtName!,
                            style: GoogleFonts.inter(fontSize: 12),
                          ),
                        ),
                      ),
                      // const Icon(
                      //   Icons.file_upload_outlined,
                      //   color: Colors.black,
                      // ),
                    ],
                  ],
                ),
              ),
            ),
          ),
        ),
        if (errorMessage != null)
          Padding(
            padding: const EdgeInsets.only(left: 8, top: 4),
            child: Text(
              errorMessage!,
              style: const TextStyle(color: Colors.red, fontSize: 10),
            ),
          ),
      ],
    );
  }
}
