// Author: fengshengxiong
// Date: 2024/5/7
// Description: MusicAppbar

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/generated/assets.dart';

class MusicAppbar extends StatelessWidget {
  const MusicAppbar({
    super.key,
    this.title,
    this.backgroundColor,
    this.titleWidget,
    this.isBackBorder = false,
    this.showBackWidget = true,
    this.isDownBack = false,
    this.onBackTap,
    this.action,
    this.actionOnTap,
  });

  final String? title;
  final Color? backgroundColor;
  final Widget? titleWidget;
  final bool isBackBorder;
  final bool showBackWidget;
  final bool isDownBack;
  final Function()? onBackTap;
  final Widget? action;
  final Function()? actionOnTap;

  @override
  Widget build(BuildContext context) {
    double topPadding = MediaQuery.of(context).padding.top;
    return Container(
      height: topPadding + 50.h,
      padding: EdgeInsets.only(top: topPadding, left: 16.w, right: 8.w),
      color: backgroundColor ?? Colors.transparent,
      child: Stack(
        children: [
          if (showBackWidget) ...[
            Align(
              alignment: Alignment.centerLeft,
              child: _buildBackWidget(),
            ),
          ],
          if (action != null) ...[
            Align(
              alignment: Alignment.centerRight,
              child: _buildActionWidget(),
            ),
          ],
          Center(
            child: _buildTitle(),
          ),
        ],
      ),
    );
  }

  /// 返回按钮
  Widget _buildBackWidget() {
    return ClipOval(
      child: BackdropFilter(
        filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
        child: Container(
          width: 42.w,
          height: 42.w,
          decoration: BoxDecoration(
            color: const Color(0x26FFFFFF),
            borderRadius: BorderRadius.circular(42.w),
            border: isBackBorder ? Border.all(color: const Color(0x4DFFFFFF), width: 1.w) : null,
          ),
          child: Material(
            color: Colors.transparent,
            child: InkWell(
              onTap: onBackTap ?? Get.back,
              child: FittedBox(
                fit: BoxFit.none,
                child: Image.asset(
                  isDownBack
                      ? Assets.sideBArrowDownBack
                      : Assets.sideBArrowLeftBack,
                  width: 20.w,
                  height: 20.w,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  /// 标题
  Widget _buildTitle() {
    return Padding(
      padding: EdgeInsets.only(left: 56.w, right: 56.w),
      child: titleWidget == null ? Text(
        title ?? '',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18.sp,
        ),
      ) : titleWidget!,
    );
  }

  /// 右边按钮
  Widget _buildActionWidget() {
    return ClipOval(
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: actionOnTap,
          child: Padding(
            padding: const EdgeInsets.all(6).w,
            child: action,
          ),
        ),
      ),
    );
  }
}
