import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_cropper/image_cropper.dart';

class CropImage {
  Future<XFile?> getCroppedFile(
      {required final BuildContext context,
      required final String sourcePath,
      final CropAspectRatioPresetData? aspectRatioPresets}) async {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.black,
      statusBarIconBrightness: Brightness.light,
    ));

    CroppedFile? croppedFile = await ImageCropper().cropImage(
      sourcePath: sourcePath,
      compressFormat: ImageCompressFormat.jpg,
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: 'برش عکس',
            activeControlsWidgetColor: Theme.of(context).colorScheme.primary,
            toolbarColor: Theme.of(context).colorScheme.primary,
            statusBarColor: Colors.black,
            toolbarWidgetColor: Colors.white,
            backgroundColor: Colors.black,
            dimmedLayerColor: Colors.black,
            lockAspectRatio: true,
            hideBottomControls: false,
            showCropGrid: true,
            initAspectRatio: aspectRatioPresets,
            aspectRatioPresets: aspectRatioPresets != null
                ? [aspectRatioPresets]
                : const [
                    CropAspectRatioPreset.original,
                    CropAspectRatioPreset.square,
                    CropAspectRatioPreset.ratio3x2,
                    CropAspectRatioPreset.ratio4x3,
                    CropAspectRatioPreset.ratio16x9
                  ]),
        IOSUiSettings(
          title: 'برش عکس',
          hidesNavigationBar: false,
        ),
        WebUiSettings(
          context: context,
        ),
      ],
    );

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark
          ? Brightness.light
          : Brightness.dark,
    ));

    XFile? result;
    if (croppedFile != null) {
      result = XFile(croppedFile.path);

      final fileSize = await result.length();
      final fileSizeMb = (fileSize / 1024 / 1024);

      if (fileSizeMb > 2) {
        result = await FlutterImageCompress.compressAndGetFile(
            result.path, result.path,
            format: CompressFormat.jpeg, quality: 20);
      } else if (fileSizeMb > 5) {
        result = await FlutterImageCompress.compressAndGetFile(
            result.path, result.path,
            format: CompressFormat.jpeg, quality: 40);
      } else if (fileSizeMb > 8) {
        result = await FlutterImageCompress.compressAndGetFile(
            result.path, result.path,
            format: CompressFormat.jpeg, quality: 60);
      }
    }
    return result;
  }
}

class CropAspectRatioPresetCustom implements CropAspectRatioPresetData {
  @override
  (int, int)? get data => (1, 1);

  @override
  String get name => '1x1 (customized)';
}
