import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mc_common_app/utils/utils.dart';

abstract class CommonServices {
  Future<File?> pickImageFromPhone(int sourceFlag);

  Future<File?> pickFile({FileType fileType = FileType.custom, List<String?>? allowedExtensions});
}

class CommonServicesImp implements CommonServices {
  @override
  Future<File?> pickImageFromPhone(int sourceFlag) async {
    final picker = ImagePicker();
    final pickedImage = await picker.pickImage(
      source: sourceFlag == 0 ? ImageSource.camera : ImageSource.gallery,
    );
    final pickedImageFile = File(pickedImage!.path);
    return pickedImageFile;
  }

  @override
  Future<File?> pickFile({FileType fileType = FileType.custom, List<String?>? allowedExtensions}) async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['png', 'pdf', 'jpeg']);

    if (result != null) {
      File file = File(result.files.single.path ?? "");
      int sizeInBytes = file.lengthSync();
      // double sizeInMb = sizeInBytes / (1024 * 1024);
      if (sizeInBytes > 1000) {
        Utils.showToast("File is larger then 1KB");
      } else {
        return file;
      }
    } else {
      // User canceled the picker
      return null;
    }
  }
}
