import 'dart:io';

import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/new_views/pages/land_page/land_page.dart';
import 'package:test_sa/new_views/swipe_module/dialoge/acknowledge_work_dialog.dart';

import '../app_style/app_color.dart';

class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final List<Widget>? actions;
  final Function? onBackPress;
  final Color? backgroundColor;
  final bool showHomeActionButton;
  final bool showBackButton;
  final VoidCallback? onWillPopScope;
  final TextStyle? titleStyle;
  final bool showBackButtonAsCancel;

  const DefaultAppBar(
      {this.title, this.onBackPress, this.onWillPopScope, this.actions, this.titleStyle, this.showHomeActionButton = false, this.showBackButton = true,this.backgroundColor, this.showBackButtonAsCancel = false, Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> _actions = [];

    if (showHomeActionButton) {
      _actions.add(
        IconButton(
          icon: const Icon(Icons.home),
          onPressed: () {
            // stopTimer();
            Navigator.pop(context);
            Navigator.popUntil(context, (ModalRoute.withName(LandPage.routeName)));
          },
        ),
      );
    }
    if (actions?.isNotEmpty ?? false) {
      _actions.addAll(actions!);
    }
    return AppBar(
      automaticallyImplyLeading: false,
      titleSpacing: 16,
      backgroundColor:backgroundColor,
      title: Row(
        children: [
          if (showBackButton)
            Icon(showBackButtonAsCancel ? Icons.clear : (Platform.isIOS ? Icons.arrow_back_ios : Icons.arrow_back)).onPress(() {
              if (onWillPopScope != null) {
                showDialog(
                  context: context,
                  builder: (BuildContext cxt) => AcknowledgeWorkDialog(
                    onSave: onWillPopScope!,
                    onDiscard: () {
                      Navigator.of(cxt).pop();
                    },
                  ),
                );
                return;
              }
              if (onBackPress != null) {
                onBackPress!();
              } else {
                Navigator.of(context).pop();
              }
            }),
          12.width,
          Text(
            title ?? "",
            style: titleStyle ?? (AppTextStyles.heading5).copyWith(fontWeight: FontWeight.w500, color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
          ).expanded,
        ],
      ),
      actions: _actions,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(60.toScreenHeight);
}
