import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../hi_edge_swipe_detector.dart';
import 'swipe_config.dart';
import 'has_swipe_config.dart';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';

class SwipeWrapper extends StatelessWidget {
  final Widget child;
  final SwipeConfig config;
  const SwipeWrapper({super.key, required this.child, required this.config});

  static Widget detect(Widget Function() builder) {
    final w = builder();
    SwipeConfig effective = const SwipeConfig();
    if (w is HasSwipeConfig) {
      effective = (w as HasSwipeConfig).swipeConfig;
    } else if (GetPlatform.isIOS) {
      effective = SwipeConfig(enableLeft: true, onLeft: () => Get.back());
    }
    final enabled = effective.enableLeft ||
        effective.enableRight ||
        effective.onLeft != null ||
        effective.onRight != null;
    KRLogUtil.kr_d(
        'SwipeWrapper.detect enabled=$enabled left=${effective.enableLeft} right=${effective.enableRight}',
        tag: 'SwipeWrapper');
    if (!enabled) return w;
    return HIEdgeSwipeDetector(child: w, config: effective);
  }

  @override
  Widget build(BuildContext context) {
    final effective = config;
    final enabled = effective.enableLeft ||
        effective.enableRight ||
        effective.onLeft != null ||
        effective.onRight != null;
    KRLogUtil.kr_d(
        'SwipeWrapper.build enabled=$enabled left=${effective.enableLeft} right=${effective.enableRight}',
        tag: 'SwipeWrapper');
    if (!enabled) return child;
    return HIEdgeSwipeDetector(child: child, config: effective);
  }
}
