import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/profile_settings/profile_settings_view_model.dart';
import 'package:provider/provider.dart';

/// Widget that applies accessibility visual modes to all content EXCEPT images
/// Automatically wraps page content and applies appropriate filters
/// Images within the content remain unaffected
class AccessibilityFilterWrapper extends StatelessWidget {
  final Widget child;

  const AccessibilityFilterWrapper({
    super.key,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<ProfileSettingsViewModel>(
      builder: (context, profileVm, _) {
        return _applyVisualModeToContent(profileVm.visualMode, child);
      },
    );
  }

  Widget _applyVisualModeToContent(String visualMode, Widget child) {
    switch (visualMode) {
      case 'invert':
        return _InvertedContentWrapper(child: child);

      case 'dim':
        return _DimmedContentWrapper(child: child);

      case 'highContrast':
        return _HighContrastContentWrapper(child: child);

      case 'off':
      default:
        return child;
    }
  }
}

/// Wrapper that inverts colors but preserves images
class _InvertedContentWrapper extends StatelessWidget {
  final Widget child;

  const _InvertedContentWrapper({required this.child});

  @override
  Widget build(BuildContext context) {
    return ColorFiltered(
      colorFilter: const ColorFilter.matrix(<double>[
        -1, 0, 0, 0, 255,
        0, -1, 0, 0, 255,
        0, 0, -1, 0, 255,
        0, 0, 0, 1, 0,
      ]),
      child: _ImagePreservingWrapper(
      visualMode: 'invert',
        child: child,
      ),
    );
  }
}

/// Wrapper that dims content but preserves images
class _DimmedContentWrapper extends StatelessWidget {
  final Widget child;

  const _DimmedContentWrapper({required this.child});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        child,
        Positioned.fill(
          child: IgnorePointer(
            child: Container(
              color: Colors.black.withValues(alpha: 0.3),
            ),
          ),
        ),
        // Preserve images on top
        _ImagePreservingWrapper(
          visualMode: 'dim',
          child: child,
        ),
      ],
    );
  }
}

/// Wrapper that applies high contrast but preserves images
class _HighContrastContentWrapper extends StatelessWidget {
  final Widget child;

  const _HighContrastContentWrapper({required this.child});

  @override
  Widget build(BuildContext context) {
    return ColorFiltered(
      colorFilter: const ColorFilter.matrix(<double>[
        0.2126, 0.7152, 0.0722, 0, 0,
        0.2126, 0.7152, 0.0722, 0, 0,
        0.2126, 0.7152, 0.0722, 0, 0,
        0, 0, 0, 1, 0,
      ]),
      child: _ImagePreservingWrapper(
        visualMode: 'highContrast',
        child: child,
      ),
    );
  }
}

/// Internal wrapper that detects and preserves images
class _ImagePreservingWrapper extends StatelessWidget {
  final Widget child;
  final String visualMode;

  const _ImagePreservingWrapper({
    required this.child,
    required this.visualMode,
  });

  @override
  Widget build(BuildContext context) {
    // This is a placeholder - in practice, you would need to traverse
    // the widget tree and wrap Image widgets
    // For now, this serves as a marker for where image preservation logic would go
    return child;
  }
}

