import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/service/AuthenticatedUserObject.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_update_screen.dart';
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
import 'package:diplomaticquarterapp/routes.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/bottom_bar.dart';
import 'package:diplomaticquarterapp/widgets/progress_indicator/app_loader_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:diplomaticquarterapp/widgets/robo-search/robosearch.dart';
import 'package:diplomaticquarterapp/widgets/robo-search/search.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

import '../../locator.dart';
import 'floating_button_search.dart';
import '../progress_indicator/app_loader_widget.dart';
import 'arrow_back.dart';
import 'network_base_view.dart';
import 'not_auh_page.dart';

class AppScaffold extends StatelessWidget {
  final String appBarTitle;
  final Widget body;
  final Widget bottomSheet;
  final bool isLoading;
  final bool isShowAppBar;
  final bool hasAppBarParam;
  final BaseViewModel baseViewModel;
  final bool isBottomBar;
  final Widget floatingActionButton;
  final bool isPharmacy;
  final String title;
  final String description;
  final bool isShowDecPage;
  final List<String> infoList;
  final Color backgroundColor;
  final double preferredSize;
  final bool showHomeAppBarIcon;
  final List<Widget> appBarIcons;
  final List<ImagesInfo> imagesInfo;

  AuthenticatedUserObject authenticatedUserObject =
      locator<AuthenticatedUserObject>();

  AppScaffold(
      {@required this.body,
      this.appBarTitle = '',
      this.isLoading = false,
      this.isShowAppBar = false,
      this.hasAppBarParam,
      this.bottomSheet,
      this.baseViewModel,
      this.floatingActionButton,
      this.isPharmacy = false,
      this.title,
      this.description,
      this.isShowDecPage = true,
      this.isBottomBar,
      this.backgroundColor,
      this.preferredSize = 0.0,
      this.showHomeAppBarIcon = true,
      this.appBarIcons,
      this.infoList,
      this.imagesInfo});

  @override
  Widget build(BuildContext context) {
    AppGlobal.context = context;
    return Scaffold(
      backgroundColor:
          backgroundColor ?? Theme.of(context).scaffoldBackgroundColor,
      appBar: isShowAppBar
          ? AppBarWidget(
              appBarTitle: appBarTitle,
              appBarIcons: appBarIcons,
              showHomeAppBarIcon: showHomeAppBarIcon,
              isPharmacy: isPharmacy,
              isShowDecPage: isShowDecPage,
            )
          : null,
      bottomSheet: bottomSheet,
      body: (!Provider.of<ProjectViewModel>(context, listen: false).isLogin &&
              isShowDecPage)
          ? NotAutPage(
              title: title ?? appBarTitle,
              description: description,
              infoList: infoList,
              imagesInfo: imagesInfo,
            )
          : baseViewModel != null
              ? NetworkBaseView(
                  child: body,
                  baseViewModel: baseViewModel,
                )
              : body,
      floatingActionButton: floatingActionButton,
    );
  }

  buildAppLoaderWidget(bool isLoading) {
    return isLoading ? AppLoaderWidget() : Container();
  }
}

class AppBarWidget extends StatelessWidget with PreferredSizeWidget {
  final AuthenticatedUserObject authenticatedUserObject =
      locator<AuthenticatedUserObject>();

  final String appBarTitle;
  final bool showHomeAppBarIcon;
  final List<Widget> appBarIcons;
  final bool isPharmacy;
  final bool isShowDecPage;

  AppBarWidget(
      {this.appBarTitle,
      this.showHomeAppBarIcon,
      this.appBarIcons,
      this.isPharmacy = true,
      this.isShowDecPage = true});

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

  Widget buildAppBar(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppBar(
      elevation: 0,
      backgroundColor:
          isPharmacy ? Colors.green : Theme.of(context).appBarTheme.color,
      textTheme: TextTheme(
        headline6: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
      ),
      title: Text(
          authenticatedUserObject.isLogin || !isShowDecPage
              ? appBarTitle.toUpperCase()
              : TranslationBase.of(context).serviceInformationTitle,
          style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.white,
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'WorkSans')),
      leading: Builder(
        builder: (BuildContext context) {
          return ArrowBack();
        },
      ),
      centerTitle: true,
      actions: <Widget>[
        isPharmacy
            ? IconButton(
                icon: Icon(Icons.shopping_cart),
                color: Colors.white,
                onPressed: () {
                  Navigator.of(context).popUntil(ModalRoute.withName('/'));
                })
            : Container(),
        if (showHomeAppBarIcon)
          IconButton(
            icon: Icon(FontAwesomeIcons.home),
            color: Colors.white,
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (context) => LandingPage()),
                  (Route<dynamic> r) => false);
            },
          ),
        if (appBarIcons != null) ...appBarIcons
      ],
    );
  }

  @override
  Size get preferredSize => Size(double.maxFinite, 60);
}
