import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/core/styles/my_font.dart';
import 'package:boilerplate/presentation/screen/landing/landing_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

import '../../core/utils/navigator_util.dart';
import 'loading.dart';

class BaseScaffold extends StatelessWidget {
  final String? title;
  final Widget body;
  final Widget? floatingActionButton;
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  final Widget? bottomNavigationBar;
  final Color? backgroundColor;
  final Color? appBarBackgroundColor;
  final Color? titleColor;
  final bool isLoading;
  final List<Widget>? actions;
  final bool hideAppBar;
  final Widget? leading;
  final Widget? drawer;
  final bool? resizeToAvoidBottomInset;
  final bool? onBack;

  const BaseScaffold({
    Key? key,
    this.title,
    required this.body,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.bottomNavigationBar,
    this.backgroundColor,
    this.appBarBackgroundColor,
    this.titleColor,
    this.isLoading = false,
    this.actions,
    this.hideAppBar = false,
    this.leading,
    this.drawer,
    this.resizeToAvoidBottomInset,
    this.onBack = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        GestureDetector(
          onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
          child: Scaffold(
            backgroundColor: backgroundColor ?? Colors.white,
            drawer: this.drawer,
            appBar: !this.hideAppBar
                ? AppBar(
                    backgroundColor: this.appBarBackgroundColor ?? Colors.white,
                    elevation: 0,
                    toolbarHeight: MyDimens.dimen60,
                    flexibleSpace: Image(
                      image: AssetImage('assets/images/img_list_search_bg.png'),
                      fit: BoxFit.cover,
                    ),
                    leading: leading ??
                        (ModalRoute.of(context)?.canPop ?? false
                            ? IconButton(
                                onPressed: () {
                                  if (ModalRoute.of(context)?.canPop ?? false) {
                                    onBack == false
                                        ? Navigator.pop(context)
                                        : NavigatorUtil.pushReplaceToPage(
                                            LandingScreem(),
                                          );
                                  }
                                },
                                icon: SvgPicture.asset(
                                  'assets/icons/ic_back_button.svg',
                                  color: titleColor ?? Colors.white,
                                  width: MyDimens.dimen24,
                                  height: MyDimens.dimen24,
                                ),
                              )
                            : null),
                    centerTitle: true,
                    title: title != null
                        ? Text(
                            this.title!,
                            style: MyFont.medium16.copyWith(
                              color: titleColor ?? Colors.white,
                            ),
                          )
                        : null,
                    actions: this.actions,
                  )
                : null,
            body: this.body,
            floatingActionButton: this.floatingActionButton,
            floatingActionButtonLocation: this.floatingActionButtonLocation,
            bottomNavigationBar: this.bottomNavigationBar,
            resizeToAvoidBottomInset: resizeToAvoidBottomInset,
          ),
        ),
        if (this.isLoading) LoadingScreen(),
      ],
    );
  }
}
