import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

import '../routing/app_routes.dart';
import 'details_bottom_sheet.dart';
import 'file_pick.dart';

/// Bottom sheet with "from gallery" and "take photo" actions for choosing a
/// chat background. Returns the picked image bytes **as-is** — cropping is a
/// separate, explicit step (see [cropChatBackgroundImage]) so the user isn't
/// forced through a cropper after every pick. Returns `null` if the user
/// cancelled everything.
Future<Uint8List?> showChatBackgroundPickerSheet(BuildContext context) async {
  Uint8List? result;
  await showDetailsBottomSheet(
    context,
    children: (sheetContext) => [
      ListTile(
        leading: const Icon(Icons.photo_library_outlined),
        title: const Text('Aus Galerie wählen'),
        onTap: () async {
          final bytes = await _pickRaw(FilePick.singleGalleryPick);
          if (bytes == null || !sheetContext.mounted) return;
          result = bytes;
          Navigator.of(sheetContext).pop();
        },
      ),
      ListTile(
        leading: const Icon(Icons.photo_camera_outlined),
        title: const Text('Foto aufnehmen'),
        onTap: () async {
          final bytes = await _pickRaw(FilePick.cameraPick);
          if (bytes == null || !sheetContext.mounted) return;
          result = bytes;
          Navigator.of(sheetContext).pop();
        },
      ),
    ],
  );
  return result;
}

/// Opens the free-form cropper on [bytes] and returns the cropped result, or
/// `null` if the user cancelled. Free aspect ratio since the background fills
/// the whole screen.
Future<Uint8List?> cropChatBackgroundImage(
  BuildContext context,
  Uint8List bytes,
) => AppRoutes.openAvatarCrop(context, imageBytes: bytes, aspectRatio: null);

Future<Uint8List?> _pickRaw(Future<XFile?> Function() pick) async {
  final picked = await pick();
  if (picked == null) return null;
  return picked.readAsBytes();
}
