import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart' hide BaseLogic, SearchWidget;
import 'package:rasadyar_inspection/presentation/widget/app_bar/i_app_bar.dart';
import 'package:rasadyar_inspection/presentation/widget/search.dart';

import 'logic.dart';

class BasePage extends StatefulWidget {
  const BasePage({
    super.key,
    this.routes,
    required this.widgets,
    this.routesWidget,
    this.floatingActionButtonLocation,
    this.floatingActionButton,
    this.onSearchChanged,
    this.hasBack = true,
    this.hasFilter = true,
    this.hasSearch = true,
    this.isBase = false,
    this.defaultSearch = true,
    this.onBackPressed,
    this.onFilterTap,
    this.onSearchTap,
    this.filteringWidget,

  });

  final List<String>? routes;
  final Widget? routesWidget;
  final bool defaultSearch;
  final List<Widget> widgets;
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  final Widget? floatingActionButton;
  final Widget? filteringWidget;
  final void Function(String?)? onSearchChanged;
  final bool hasBack;
  final bool hasFilter;
  final bool hasSearch;
  final bool isBase;
  final VoidCallback? onBackPressed;
  final GestureTapCallback? onFilterTap;
  final GestureTapCallback? onSearchTap;

  @override
  State<BasePage> createState() => _BasePageState();
}

class _BasePageState extends State<BasePage> {
  BaseLogic get controller => Get.find<BaseLogic>();
  Worker? filterWorker;
  bool _isBottomSheetOpen = false;

  @override
  void initState() {
    super.initState();
    /*  filterWorker = ever(controller.isFilterSelected, (bool isSelected) {
      if (!mounted) return;

      if (isSelected && widget.filteringWidget != null) {
        // بررسی اینکه آیا bottomSheet از قبل باز است یا نه
        if (_isBottomSheetOpen) {
          controller.isFilterSelected.value = false;
          return;
        }

        // بررسی اینکه آیا route فعلی current است یا نه
        if (ModalRoute.of(context)?.isCurrent != true) {
          controller.isFilterSelected.value = false;
          return;
        }

        _isBottomSheetOpen = true;
        Get.bottomSheet(
          widget.filteringWidget!,
          isScrollControlled: true,
          isDismissible: true,
          enableDrag: true,
        ).then((_) {
          // تنظیم مقدار به false بعد از بسته شدن bottomSheet
          if (mounted) {
            _isBottomSheetOpen = false;
            controller.isFilterSelected.value = false;
          }
        });
      }
    });*/
  }

  @override
  void dispose() {
    filterWorker?.dispose();
    super.dispose();
  }

  void _onFilterTap() {
    if (widget.hasFilter && widget.filteringWidget != null) {

      final currentRoute = ModalRoute.of(context);
      if (currentRoute?.isCurrent != true) {
        return;
      }


      Get.bottomSheet(
        widget.filteringWidget!,
        isScrollControlled: true,
        isDismissible: true,
        enableDrag: true,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, result) => widget.onBackPressed,
      child: Scaffold(
        backgroundColor: AppColor.bgLight,
        appBar: inspectionAppBar(
          hasBack: widget.isBase ? false : widget.hasBack,
          onBackPressed: widget.onBackPressed,
          hasFilter: widget.hasFilter,
          hasSearch: widget.hasSearch,
          isBase: widget.isBase,
          onFilterTap: widget.hasFilter ? _onFilterTap : null,
          onSearchTap: widget.hasSearch ? () => controller.toggleSearch() : null,
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //widget.routesWidget != null ? widget.routesWidget! : buildPageRoute(widget.routes!),
            if (!widget.isBase && widget.hasSearch && widget.defaultSearch) ...{
              SearchWidget2(onSearchChanged: widget.onSearchChanged),
            },
            ...widget.widgets,
          ],
        ),
        floatingActionButtonLocation:
            widget.floatingActionButtonLocation ?? FloatingActionButtonLocation.startFloat,
        floatingActionButton: widget.floatingActionButton,
      ),
    );
  }
}
