import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mc_common_app/main.dart';
import 'package:mc_common_app/utils/app_permission_handler.dart';

abstract class CommonAppServices {
  Future<List<File>> pickMultipleImages();

  Future<File?> pickImageFromPhone(int sourceFlag);

  Future<List<File>?> pickMultipleFiles(BuildContext context, {bool allowMultiple = true});

  Future<File?> pickFile(BuildContext context, {required FileType fileType, List<String>? allowedExtensions});

  double getDistanceBetween({
    required double curLatitude,
    required double curLongitude,
    required double destLatitude,
    required double destLongitude,
  });
}

class CommonServicesImp implements CommonAppServices {
  @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<List<File>?> pickMultipleFiles(BuildContext context, {bool allowMultiple = true}) async {
    FilePickerResult? result;

    final status = await AppPermissions.checkStoragePermissions(context);
    if (status) {
      result = await FilePicker.platform.pickFiles(allowMultiple: allowMultiple, type: FileType.custom, allowedExtensions: ['pdf']);
    }

    List<File> pickedFiles = [];
    if (result != null) {
      for (var element in result.files) {
        if (element.path != null) {
          pickedFiles.add(File(element.path!));
        }
      }
    }
    return pickedFiles;
  }

  @override
  Future<List<File>> pickMultipleImages() async {
    final picker = ImagePicker();
    final pickedImagesXFiles = await picker.pickMultiImage();

    List<File> pickedImages = [];
    if (pickedImagesXFiles == null) {
      return [];
    }
    if (pickedImagesXFiles.isEmpty) {
      return [];
    }
    for (var element in pickedImagesXFiles) {
      pickedImages.add(File(element.path));
    }
    return pickedImages;
  }

  @override
  Future<File?> pickFile(BuildContext context, {required FileType fileType, List<String>? allowedExtensions}) async {
    FilePickerResult? result;

    final status = await AppPermissions.checkStoragePermissions(context);
    if (status) {
      result = await FilePicker.platform.pickFiles(allowMultiple: false, type: fileType, allowedExtensions: allowedExtensions);
    }

    List<File> pickedFiles = [];
    if (result != null) {
      for (var element in result.files) {
        if (element.path != null) {
          pickedFiles.add(File(element.path!));
        }
      }
    }
    return pickedFiles.isNotEmpty ? pickedFiles.first : null;
  }

  @override
  double getDistanceBetween({required double curLatitude, required double curLongitude, required double destLatitude, required double destLongitude}) {
    try {
      double distance = Geolocator.distanceBetween(
        curLatitude,
        curLongitude,
        destLatitude,
        destLongitude,
      );

      return (distance / 1000) ?? 0.0;
    } catch (e) {
      logger.e(e.toString());
      return 0.0;
    }
  }
}
