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

class FilePicker extends StatelessWidget {
  FilePicker({
    Key? key,
    this.cameraHandle,
    this.galleryHandleSingle,
    this.galleryHandleMultiple,
    this.fileHandle,
  }) : super(key: key);

  final Function(XFile image)? cameraHandle;
  final Function(XFile image)? galleryHandleSingle;
  final Function(List<XFile> images)? galleryHandleMultiple;
  // TODO: Implement. Perhaps after first release?
  final Function()? fileHandle;

  final ImagePicker _picker = ImagePicker();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 10, left: 5, right: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _filePickerSelection(
            hasHandle: cameraHandle != null,
            icon: Icons.camera_alt,
            onTap: () async {
              final XFile? image = await _picker.pickImage(source: ImageSource.camera);
              if (image == null) {
                return;
              }
              cameraHandle!(image);
            },
            context: context,
          ),
          _filePickerSelection(
            hasHandle: galleryHandleSingle != null,
            icon: Icons.image,
            onTap: () async {
              final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
              if (image == null) {
                return;
              }
              galleryHandleSingle!(image);
            },
            context: context,
          ),
          _filePickerSelection(
            hasHandle: galleryHandleMultiple != null,
            icon: Icons.image,
            onTap: () async {
              final List<XFile>? images = await _picker.pickMultiImage();
              if (images == null) {
                return;
              }
              galleryHandleMultiple!(images);
            },
            context: context,
          ),
          _filePickerSelection(
            hasHandle: fileHandle != null,
            icon: Icons.file_present_sharp,
            onTap: () {
            },
            context: context,
          ),
        ],
      )
    );
  }

  Widget _filePickerSelection({
    required bool hasHandle,
    required IconData icon,
    required Function() onTap,
    required BuildContext context
  }) {
    if (!hasHandle) {
      return const SizedBox.shrink();
    }

    return Padding(
      padding: const EdgeInsets.only(left: 5, right: 5),
      child: GestureDetector(
        onTap: onTap,
        child: Container(
          height: 75,
          width: 75,
          decoration: BoxDecoration(
            color: Theme.of(context).primaryColor,
            borderRadius: BorderRadius.circular(25),
          ),
          child: Icon(
            icon,
            size: 40,
          ),
        ),
      ),
    );
  }
}

