import 'package:flutter/material.dart';

/// Responsive utility class for all screens
class ResponsiveUtils {
  final BuildContext context;

  ResponsiveUtils(this.context);

  // Get screen width
  double get screenWidth => MediaQuery.of(context).size.width;

  // Get screen height
  double get screenHeight => MediaQuery.of(context).size.height;

  // Device type checks
  bool get isMobile => screenWidth < 600;
  bool get isTablet => screenWidth >= 600 && screenWidth < 900;
  bool get isDesktop => screenWidth >= 900;

  // Responsive width percentage
  double widthPercent(double percent) => screenWidth * (percent / 100);

  // Responsive height percentage
  double heightPercent(double percent) => screenHeight * (percent / 100);

  // Responsive font size
  double fontSize({required double mobile, double? tablet, double? desktop}) {
    if (isDesktop && desktop != null) return desktop;
    if (isTablet && tablet != null) return tablet;
    return mobile;
  }

  // Responsive spacing
  double spacing({required double mobile, double? tablet, double? desktop}) {
    if (isDesktop && desktop != null) return desktop;
    if (isTablet && tablet != null) return tablet;
    return mobile;
  }

  // Responsive padding
  EdgeInsets padding({
    required double mobile,
    double? tablet,
    double? desktop,
  }) {
    final value = spacing(mobile: mobile, tablet: tablet, desktop: desktop);
    return EdgeInsets.all(value);
  }

  // Responsive horizontal padding
  EdgeInsets horizontalPadding({
    required double mobile,
    double? tablet,
    double? desktop,
  }) {
    final value = spacing(mobile: mobile, tablet: tablet, desktop: desktop);
    return EdgeInsets.symmetric(horizontal: value);
  }

  // Responsive vertical padding
  EdgeInsets verticalPadding({
    required double mobile,
    double? tablet,
    double? desktop,
  }) {
    final value = spacing(mobile: mobile, tablet: tablet, desktop: desktop);
    return EdgeInsets.symmetric(vertical: value);
  }

  // Responsive border radius
  double borderRadius({
    required double mobile,
    double? tablet,
    double? desktop,
  }) {
    if (isDesktop && desktop != null) return desktop;
    if (isTablet && tablet != null) return tablet;
    return mobile;
  }

  // Card width for list items
  double cardWidth({
    double mobilePercent = 70,
    double tabletPercent = 45,
    double desktopPercent = 30,
  }) {
    if (isDesktop) return widthPercent(desktopPercent);
    if (isTablet) return widthPercent(tabletPercent);
    return widthPercent(mobilePercent);
  }

  // Get value based on screen size
  T getValue<T>({required T mobile, T? tablet, T? desktop}) {
    if (isDesktop && desktop != null) return desktop;
    if (isTablet && tablet != null) return tablet;
    return mobile;
  }
}

/// Extension on BuildContext for easy access
extension ResponsiveExtension on BuildContext {
  ResponsiveUtils get responsive => ResponsiveUtils(this);

  // Quick access methods
  double get screenWidth => MediaQuery.of(this).size.width;
  double get screenHeight => MediaQuery.of(this).size.height;
  bool get isMobile => screenWidth < 600;
  bool get isTablet => screenWidth >= 600 && screenWidth < 900;
  bool get isDesktop => screenWidth >= 900;
}
