using Microsoft.Maui.Platform;
using UIKit;

namespace Blahblah.FlowerApp;

public static partial class PageExtensions
{
    public static UIViewController ShowBottomSheet(this Page page, IView content, bool dimDismiss = false)
    {
        var mauiContext = page.Handler?.MauiContext ?? throw new Exception("MauiContext is null");
        var viewController = page.ToUIViewController(mauiContext);

        var viewControllerToPresent = content.ToUIViewController(mauiContext);
        viewControllerToPresent.ModalInPresentation = !dimDismiss;

        var sheet = viewControllerToPresent.SheetPresentationController;
        if (sheet != null)
        {
            sheet.Detents = new[]
            {
                UISheetPresentationControllerDetent.CreateLargeDetent()
            };
            //sheet.LargestUndimmedDetentIdentifier = dimDismiss ?
            //    UISheetPresentationControllerDetentIdentifier.Unknown :
            //    UISheetPresentationControllerDetentIdentifier.Medium;
            sheet.PrefersScrollingExpandsWhenScrolledToEdge = false;
            sheet.PrefersEdgeAttachedInCompactHeight = true;
            sheet.WidthFollowsPreferredContentSizeWhenEdgeAttached = true;
        }

        viewController.PresentViewController(viewControllerToPresent, true, null);
        return viewControllerToPresent;
    }

    public static void CloseBottomSheet(this UIViewController sheet)
    {
        sheet.DismissViewController(true, null);
    }
}
