import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/routes/app_routes.dart';
import 'package:tone_snap/utils/device_info_util.dart';
import 'package:tone_snap/utils/permission_util.dart';

class UploadMethodController extends GetxController {

  Future<void> goRecordSound() async {
    Get.toNamed(AppRoutes.recordSound);
  }

  void openFilePicker() async {
    Permission permission;
    if (Platform.isAndroid) {
      int sdkInt = await DeviceInfoUtil.getAndroidSDKInt();
      if (sdkInt >= 33) {
        permission = Permission.audio;
      } else {
        permission = Permission.storage;
      }
    } else {
      permission = Permission.mediaLibrary;
    }
    bool result = await PermissionUtil.checkPermission([permission]);
    if (!result) return;

    FilePickerResult? filePickerResult = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['mp3', 'wav', 'm4a', 'flac', 'aac'],
      onFileLoading: (FilePickerStatus status) {
        if (status == FilePickerStatus.picking) BaseEasyLoading.loading();
        if (status == FilePickerStatus.done) BaseEasyLoading.dismiss();
      },
    );
    if (filePickerResult != null) {
      PlatformFile file = filePickerResult.files.first;
      Get.toNamed(AppRoutes.changeVoice, arguments: file.path);
    }
  }
}
