import 'dart:convert';
import 'dart:io';

import 'package:SEFER/constant/api_key.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';

import '../../constant/box_name.dart';
import '../../constant/colors.dart';
import '../../main.dart';

class ImageController extends GetxController {
  File? myImage;
  bool isloading = false;
  CroppedFile? croppedFile;
  final picker = ImagePicker();
  var image;
  choosImage(String link, String imageType) async {
    final pickedImage = await picker.pickImage(source: ImageSource.gallery);
    image = File(pickedImage!.path);
    croppedFile = await ImageCropper().cropImage(
      sourcePath: image!.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.square,
        CropAspectRatioPreset.ratio3x2,
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio4x3,
        CropAspectRatioPreset.ratio16x9
      ],
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: 'Cropper'.tr,
            toolbarColor: AppColor.blueColor,
            toolbarWidgetColor: AppColor.yellowColor,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        IOSUiSettings(
          title: 'Cropper'.tr,
        ),
      ],
    );
    myImage = File(pickedImage.path);
    isloading = true;
    update();
// Save the cropped image
    File savedCroppedImage = File(croppedFile!.path);
    print('link =$link');
    try {
      await uploadImage(
        savedCroppedImage,
        {
          'driverID':
              box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
          'imageType': imageType
        },
        link,
      );
    } catch (e) {
      Get.snackbar('Image Upload Failed'.tr, e.toString(),
          backgroundColor: AppColor.redColor);
      print(e);
    } finally {
      isloading = false;
      update();
    }
  }

  uploadImage(File file, Map data, String link) async {
    var request = http.MultipartRequest(
      'POST',
      Uri.parse(link), //'https://ride.mobile-app.store/uploadImage1.php'
    );

    var length = await file.length();
    var stream = http.ByteStream(file.openRead());
    var multipartFile = http.MultipartFile(
      'image',
      stream,
      length,
      filename: basename(file.path),
    );
    request.headers.addAll({
      'Authorization':
          'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
    });
    // Set the file name to the driverID
    request.files.add(
      http.MultipartFile(
        'image',
        stream,
        length,
        filename: '${box.read(BoxName.driverID)}.jpg',
      ),
    );
    data.forEach((key, value) {
      request.fields[key] = value;
    });
    var myrequest = await request.send();
    var res = await http.Response.fromStream(myrequest);
    if (res.statusCode == 200) {
      return jsonDecode(res.body);
    } else {
      throw Exception(
          'Failed to upload image: ${res.statusCode} - ${res.body}');
    }
  }
}
i want you create method to upload image of face from this id document using image package to determine the face from id document in the above method if it has face .i want to save face image to the server like above