import 'dart:ui';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import '../../core/dependencies.dart';

class CollapsingListView extends StatelessWidget {
  final String title;
  Widget? child;
  VoidCallback? search;
  VoidCallback? report;
  VoidCallback? logout;
  VoidCallback? history;
  Widget? bottomChild;
  bool isClose;
  bool isLeading;

  CollapsingListView({required this.title, this.child, this.search, this.isClose = false, this.bottomChild, this.report, this.logout, this.history, this.isLeading = true});

  @override
  Widget build(BuildContext context) {
    AppState appState = getIt.get<AppState>();
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: Column(
        children: [
          CustomScrollView(
            slivers: [
              SliverAppBar(
                automaticallyImplyLeading: false,
                pinned: true,
                expandedHeight: 100,
                stretch: true,
                systemOverlayStyle: SystemUiOverlayStyle(statusBarBrightness: Brightness.light),
                surfaceTintColor: Colors.transparent,
                backgroundColor: AppColors.bgScaffoldColor,
                leading: isLeading
                    ? Transform.flip(
                        flipX: appState.isArabic() ? true : false,
                        child: IconButton(
                          icon: Utils.buildSvgWithAssets(icon: isClose ? AppAssets.closeBottomNav : AppAssets.arrow_back, width: 32.h, height: 32.h),
                          padding: EdgeInsets.only(left: 12),
                          onPressed: () => Navigator.pop(context),
                          highlightColor: Colors.transparent,
                        ),
                      )
                    : SizedBox.shrink(),
                flexibleSpace: LayoutBuilder(
                  builder: (context, constraints) {
                    final double maxHeight = 100;
                    final double minHeight = kToolbarHeight;
                    double t = (constraints.maxHeight - minHeight) / (maxHeight - minHeight);
                    t = t - 1;
                    if (t < 0.7) t = 0.7;
                    t = t.clamp(0.0, 1.0);

                    final double fontSize = lerpDouble(14, 18, t)!;
                    final double bottomPadding = lerpDouble(0, 0, t)!;
                    final double leftPadding = lerpDouble(150, 24, t)!;

                    return Stack(
                      children: [
                        Align(
                          alignment: Alignment.lerp(
                            Alignment.center,
                            Alignment.bottomLeft,
                            t,
                          )!,
                          child: Padding(
                              padding: EdgeInsets.only(left: appState.isArabic() ? 0 : leftPadding, right: appState.isArabic() ? leftPadding : 0, bottom: bottomPadding),
                              child: Row(
                                spacing: 4.h,
                                children: [
                                  Text(
                                    title,
                                    maxLines: 1,
                                    style: TextStyle(
                                        fontSize: (27 - (5 * (2 - t))).fSize,
                                        fontWeight: FontWeight.lerp(
                                          FontWeight.w300,
                                          FontWeight.w600,
                                          t,
                                        )!,
                                        color: AppColors.blackColor,
                                        letterSpacing: -0.5),
                                  ).expanded,
                                  if (logout != null) actionButton(context, t, title: "Logout".needTranslation, icon: AppAssets.logout).onPress(logout!),
                                  if (report != null) actionButton(context, t, title: "Report".needTranslation, icon: AppAssets.report_icon).onPress(report!),
                                  if (history != null) actionButton(context, t, title: "History".needTranslation, icon: AppAssets.insurance_history_icon).onPress(history!),
                                  if (search != null) Utils.buildSvgWithAssets(icon: AppAssets.search_icon).onPress(search!).paddingOnly(right: 24)
                                ],
                              )),
                        ),
                      ],
                    );
                  },
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) => child,
                  childCount: 1,
                ),
              ),
            ],
          ).expanded,
          if (bottomChild != null) bottomChild!
        ],
      ),
    );
  }

  Widget actionButton(BuildContext context, double t, {required String title, required String icon}) {
    return AnimatedSize(
      duration: Duration(milliseconds: 150),
      child: Container(
        height: 40,
        padding: EdgeInsets.all(8),
        margin: EdgeInsets.only(right: 24),
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
          color: AppColors.secondaryLightRedColor,
          borderRadius: 12,
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          spacing: 8.h,
          children: [
            Utils.buildSvgWithAssets(icon: icon, iconColor: AppColors.primaryRedColor),
            if (t == 1)
              Text(
                title,
                style: context.dynamicTextStyle(
                  color: AppColors.primaryRedColor,
                  letterSpacing: -0.4,
                  fontSize: (14 - (2 * (1 - t))).fSize,
                  fontWeight: FontWeight.lerp(
                    FontWeight.w300,
                    FontWeight.w500,
                    t,
                  )!,
                ),
              ),
          ],
        ),
      ),
    );
  }
}
