// ignore_for_file: deprecated_member_use

import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';

class DidvanAppBar extends StatelessWidget implements PreferredSizeWidget {
  final AppBarData appBarData;
  final bool hasBorder;
  final Color? backgroundColor;
  const DidvanAppBar({
    Key? key,
    required this.appBarData,
    this.hasBorder = false,
    this.backgroundColor = Colors.transparent,
  }) : super(key: key);

  @override
  Size get preferredSize => const Size(double.infinity, kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: appBarData.isSmall ? 56 : 72,
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.only(right: 4),
      decoration: BoxDecoration(
        border: hasBorder
            ? Border(
                bottom: BorderSide(
                  color: Theme.of(context).colorScheme.cardBorder,
                ),
              )
            : null,
        color: backgroundColor ?? Theme.of(context).colorScheme.background,
      ),
      child: Row(
        children: [
          appBarData.hasBack
              ? Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                      onPressed: () {
                        appBarData.backClick?.call();
                        Navigator.of(context).pop();
                      },
                      color: Theme.of(context).colorScheme.title,
                      icon: const Icon(
                        DidvanIcons.back_regular,
                      ),
                    ),
                  ],
                )
              : const SizedBox(),
          const SizedBox(width: 16),
          Expanded(
            child: Align(
              alignment: Alignment.centerRight,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (appBarData.title != null)
                    DidvanText(
                      appBarData.title!,
                      style: Theme.of(context).textTheme.displaySmall,
                      color: Theme.of(context).colorScheme.title,
                      overflow: TextOverflow.ellipsis,
                    ),
                  if (appBarData.subtitle != null)
                    DidvanText(
                      appBarData.subtitle!,
                      maxLines: 1,
                      overflow: TextOverflow.clip,
                      style: Theme.of(context).textTheme.labelSmall,
                      color: Theme.of(context).colorScheme.caption,
                    ),
                ],
              ),
            ),
          ),
          if (appBarData.trailing != null) appBarData.trailing!,
        ],
      ),
    );
  }
}
