import 'dart:math' as math;

import 'package:flutter/material.dart';

/// Padding di sistema (status bar, notch, barra navigazione) con minimo configurabile.
class ScreenInsets {
  ScreenInsets._();

  static EdgeInsets of(BuildContext context, {double minimum = 8}) {
    final mq = MediaQuery.of(context);
    final padding = mq.padding;
    final view = mq.viewPadding;
    return EdgeInsets.only(
      left: math.max(math.max(padding.left, view.left), minimum),
      top: math.max(math.max(padding.top, view.top), minimum),
      right: math.max(math.max(padding.right, view.right), minimum),
      bottom: math.max(math.max(padding.bottom, view.bottom), minimum),
    );
  }

  /// Padding per contenuti scrollabili (login, wizard, elenco partite).
  /// Overlay camera: in landscape su Android la barra nav può stare a sinistra.
  static EdgeInsets cameraOverlay(BuildContext context, {double minimum = 12}) {
    final base = of(context, minimum: minimum);
    final landscape =
        MediaQuery.orientationOf(context) == Orientation.landscape;
    if (landscape && base.left < 48) {
      return base.copyWith(left: 48);
    }
    return base;
  }

  static EdgeInsets contentPadding(
    BuildContext context, {
    double horizontal = 20,
    double vertical = 16,
    double minimum = 8,
  }) {
    final inset = of(context, minimum: minimum);
    return EdgeInsets.fromLTRB(
      math.max(inset.left, horizontal),
      math.max(inset.top, vertical),
      math.max(inset.right, horizontal),
      math.max(inset.bottom, vertical),
    );
  }
}

/// SafeArea con padding minimo su tutti i lati (gestisce edge-to-edge Android).
class AppSafeArea extends StatelessWidget {
  const AppSafeArea({
    super.key,
    required this.child,
    this.minimum = 8,
    this.top = true,
    this.bottom = true,
    this.left = true,
    this.right = true,
  });

  final Widget child;
  final double minimum;
  final bool top;
  final bool bottom;
  final bool left;
  final bool right;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      minimum: EdgeInsets.only(
        left: left ? minimum : 0,
        top: top ? minimum : 0,
        right: right ? minimum : 0,
        bottom: bottom ? minimum : 0,
      ),
      top: top,
      bottom: bottom,
      left: left,
      right: right,
      child: child,
    );
  }
}
