import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class BasePage extends GetView<BaseLogic> {
  const BasePage({
    super.key,
    this.routes,
    this.routesWidget,
    this.widgets,
    this.child,
    this.scrollable = false,
    this.floatingActionButtonLocation,
    this.floatingActionButton,
    this.appBar,
    this.backGroundWidget,
    this.onPopScopTaped,
    this.backId,
    this.onSearchChanged,
  });

  final List<String>? routes;
  final Breadcrumb? routesWidget;
  final List<Widget>? widgets;
  final Widget? child;
  final bool scrollable;
  final RAppBar? appBar;
  final BackGroundWidget? backGroundWidget;
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  final Widget? floatingActionButton;
  final VoidCallback? onPopScopTaped;
  final int? backId;
  final void Function(String?)? onSearchChanged;

  Widget _buildHeader() {
    if (appBar?.hasSearch == true) {
      controller.setSearchCallback(onSearchChanged);
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        routesWidget ?? ContainerBreadcrumb(routes: routes!),
        SearchWidget(),
      ],
    );
  }

  Widget _buildBody() {
    final content = [
      if (routes != null || routesWidget != null) _buildHeader(),
      if (child != null) Expanded(child: child!),
      ...?widgets,
    ];

    if (scrollable) {
      if (backGroundWidget != null) {
        return Stack(
          children: [
            ?backGroundWidget,
            SingleChildScrollView(
              physics: const BouncingScrollPhysics(),
              padding: EdgeInsets.symmetric(vertical: 8.h),
              child: Column(children: content),
            ),
          ],
        );
      }
      return SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        padding: EdgeInsets.symmetric(vertical: 8.h),
        child: Column(children: content),
      );
    }

    if (backGroundWidget != null) {
      return Stack(
        children: [
          Positioned.fill(child: backGroundWidget!),
          Column(children: content),
        ],
      );
    }
    return Column(children: content);
  }

  @override
  Widget build(BuildContext context) {
    return _pageBuilder();
  }

  Widget _pageBuilder() {
    if (onPopScopTaped != null) {
      return PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, result) {
          if (!didPop) {
            onPopScopTaped!();
          }
        },
        child: Scaffold(
          backgroundColor: AppColor.bgLight,
          appBar: appBar,
          body: _buildBody(),
          floatingActionButtonLocation: floatingActionButtonLocation,
          floatingActionButton: floatingActionButton,
        ),
      );
    } else {
      return Scaffold(
        backgroundColor: AppColor.bgLight,
        appBar: appBar,
        body: _buildBody(),
        floatingActionButtonLocation: floatingActionButtonLocation,
        floatingActionButton: floatingActionButton,
      );
    }
  }
}
