import 'dart:io';
import 'package:design/design.dart';
import 'package:guru_widgets/common/flexible_container.dart';
import 'package:guru_widgets/theme/guru_theme.dart';
import 'package:guru_utils/router/router.dart';

/// Created by Haoyi on 2023/6/5
///
part 'guru_app_bar.g.dart';

@DesignSpec(width: 750, height: 94, specMode: SpecMode.nested)
abstract class GuruAppBarDesignSpec implements BasicDesignSpec {
  @SpecStatusBarHeight(50)
  double get appBarMarginTop;

  @SpecHeight(10)
  double get appBarLargTopSpcing;

  @SpecHeight(88)
  double get appBarHeight;

  @SpecHeight(94)
  double get appBarLargeHeight;

  @SpecHorizontal(24)
  double get backIconLeftSpacing;

  @SpecHorizontal(16)
  double get backIconMarginStart;

  @SpecHeight(48)
  double get leadingIconSize;

  @SpecAbsoluteFontSize(34)
  double get titleTextSmallSize;

  @SpecAbsoluteFontSize(40)
  double get titleTextMiddleSize;

  @SpecAbsoluteFontSize(50)
  double get titleTextLargeSize;

  @SpecHeight(48)
  double get actionIconSize;

  static GuruAppBarDesignSpec create(Size size,
          {Offset offset = Offset.zero}) =>
      _GuruAppBarDesignSpec.from(size, offset: offset);
}

enum LeadingType { none, back, close }

enum GuruAppBarSize { small, middle, large }

class GuruAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final Color? color;
  final Color? backgroundColor;
  final LeadingType leadingType;
  final List<Widget>? actions;
  final GuruAppBarSize size;
  final bool subpage;

  const GuruAppBar(
      {super.key,
      this.title,
      this.color,
      this.backgroundColor,
      this.leadingType = LeadingType.back,
      this.size = GuruAppBarSize.small,
      this.subpage = true,
      this.actions});

  @override
  Widget build(BuildContext context) {
    final designSpec = GuruThemeDesignSpec.get().appBarDesignSpec;
    final guruTheme = GuruTheme.of(context);
    final colorScheme = guruTheme.colorScheme;
    final appBarTheme = guruTheme.appBarTheme;
    final iconScheme = guruTheme.iconScheme;
    final fillColor = backgroundColor ??
        appBarTheme.backgroundColor ??
        colorScheme.containerColor ??
        Colors.transparent;
    final mainColor = color ?? colorScheme.primaryContentColor ?? Colors.white;

    Widget buildLeadingIconButton(String? imageIcon, IconData defaultIcon) {
      return IconButton(
        padding:
            EdgeInsets.symmetric(horizontal: designSpec.backIconMarginStart),
        splashRadius: designSpec.leadingIconSize,
        icon: imageIcon != null
            ? ImageIcon(
                AssetImage(imageIcon),
                size: designSpec.leadingIconSize,
                color: mainColor,
              )
            : Icon(
                defaultIcon,
                size: designSpec.leadingIconSize,
                color: mainColor,
              ),
        onPressed: () {
          RouteCenter.instance.back();
        },
      );
    }

    Widget? leading;

    switch (leadingType) {
      case LeadingType.close:
        leading = buildLeadingIconButton(iconScheme.closeIcon, Icons.close);
        break;
      case LeadingType.back:
        leading = buildLeadingIconButton(
            iconScheme.backIcon, Icons.arrow_back_ios_new);
        break;
      default:
        leading = null;
    }

    double titleFontSize;

    switch (size) {
      case GuruAppBarSize.large:
        titleFontSize = designSpec.titleTextLargeSize;
        break;
      case GuruAppBarSize.middle:
        titleFontSize = designSpec.titleTextMiddleSize;
        break;
      case GuruAppBarSize.small:
        titleFontSize = designSpec.titleTextSmallSize;
        break;
      default:
        titleFontSize = designSpec.titleTextSmallSize;
    }

    double topSpcing;
    topSpcing = (Platform.isAndroid && !appBarTheme.hasStatusBar) ? designSpec.appBarMarginTop : 0;
    if (size == GuruAppBarSize.large) {
      topSpcing += designSpec.appBarLargTopSpcing;
    }

    return Container(
      color: fillColor,
      padding: EdgeInsets.only(top: topSpcing, left: designSpec.backIconLeftSpacing),
      child: AppBar(
        elevation: 0,
        centerTitle: size != GuruAppBarSize.large,
        backgroundColor: fillColor,
        leading: size == GuruAppBarSize.large ? Container() : leading,
        leadingWidth: size == GuruAppBarSize.large
            ? designSpec.backIconMarginStart
            : designSpec.backIconMarginStart * 2 + designSpec.leadingIconSize,
        title: Text(
          title ?? "",
          style: TextStyle(
            fontSize: titleFontSize,
            color: mainColor,
            fontWeight: GuruTheme.fwBold,
          ),
        ),
        titleSpacing: 0,
        actions: [...?actions, const SizedBox(width: 10)],
      ),
    );
  }

  @override
  Size get preferredSize{
    final designSpec = GuruThemeDesignSpec.get().appBarDesignSpec;
    final appBarTheme = GuruTheme.of(Get.context!).appBarTheme;
    double height;
    if (size == GuruAppBarSize.large) {
      height = designSpec.appBarLargeHeight + designSpec.appBarLargTopSpcing;
    } else {
      height = designSpec.appBarHeight;
    }
    if (!appBarTheme.hasStatusBar && Platform.isAndroid) {
      height += designSpec.appBarMarginTop;
    }
    return Size.fromHeight(height);
  }
}
