import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';
import 'package:frontend_eccp_mobile/app/core/constants/enum/appbar_type_enum.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/app_button.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/bottomsheet/appbottomsheet_controller.dart';

class AppBottomSheet extends StatefulWidget {
  const AppBottomSheet({
    required this.controller,
    super.key,
    this.icon,
    this.title,
    this.subTitle,
    this.content,
    this.primaryText,
    this.secondaryText,
    this.onPrimaryPressed,
    this.onSecondaryPressed,
    this.usePrimaryButton = true,
    this.useSecondaryButton = false,
    this.onOpened,
    this.onClosed,
    this.onDismissed,
    this.enableDrag = true,
  });

  final AppBottomSheetController controller;

  final String? icon;
  final String? title;
  final String? subTitle;
  final Widget? content;

  final String? primaryText;
  final String? secondaryText;
  final VoidCallback? onPrimaryPressed;
  final VoidCallback? onSecondaryPressed;

  final bool useSecondaryButton;
  final bool usePrimaryButton;

  final VoidCallback? onOpened;
  final VoidCallback? onClosed;
  final VoidCallback? onDismissed;

  final bool enableDrag;

  @override
  State<AppBottomSheet> createState() => _AppBottomSheetState();
}

class _AppBottomSheetState extends State<AppBottomSheet> {
  bool _dismissedBySystem = true;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      widget.onOpened?.call();
    });
  }

  @override
  void dispose() {
    if (_dismissedBySystem) {
      widget.onDismissed?.call();
    }

    widget.onClosed?.call();
    super.dispose();
  }

  void _closeByAction() {
    _dismissedBySystem = false;
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: AnimatedBuilder(
        animation: widget.controller,
        builder: (_, _) {
          return Align(
            alignment: Alignment.bottomCenter,
            child: ConstrainedBox(
              constraints: const BoxConstraints(
                maxWidth: AppSizes.maxContentWidth,
              ),
              child: Container(
                padding: EdgeInsets.fromLTRB(
                  AppSizes.s16,
                  AppSizes.s16,
                  AppSizes.s16,
                  MediaQuery.of(context).viewPadding.bottom + AppSizes.s16,
                ),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(
                    top: Radius.circular(16),
                  ),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Center(
                      child: Container(
                        width: AppSizes.bottomSheetHandleWidth,
                        height: AppSizes.bottomSheetHandleHeight,
                        margin: const EdgeInsets.only(
                          bottom: AppSizes.s16,
                        ),
                        decoration: BoxDecoration(
                          color: AppColors.neutral200,
                          borderRadius: BorderRadius.circular(AppSizes.s8),
                        ),
                      ),
                    ),

                    if (widget.icon != null) ...[
                      Center(
                        child: widget.icon!.endsWith('.svg')
                            ? SvgPicture.asset(
                                widget.icon!,
                                width:
                                    MediaQuery.of(context).size.width *
                                    AppSizes.illustrationWidthFactor,
                              )
                            : Image.asset(
                                widget.icon!,
                                width:
                                    MediaQuery.of(context).size.width *
                                    AppSizes.illustrationWidthFactor,
                              ),
                      ),
                      AppSpacing.h16,
                    ],

                    if (widget.title != null)
                      Text(
                        widget.title!,
                        style: AppTextStyles.h3,
                      ),

                    if (widget.subTitle != null) ...[
                      AppSpacing.h8,
                      Text(
                        widget.subTitle!,
                        style: AppTextStyles.small,
                      ),
                    ],

                    if (widget.content != null) ...[
                      AppSpacing.h24,
                      widget.content!,
                    ],

                    if (widget.useSecondaryButton || widget.usePrimaryButton)
                      AppSpacing.h24,

                    Row(
                      children: [
                        if (widget.useSecondaryButton) ...[
                          Expanded(
                            child: AppButton(
                              radius: AppSizes.s12,
                              paddingVertical: AppSizes.s12,
                              backgroundColor: Colors.white,
                              label: widget.secondaryText ?? 'Batal',
                              type: AppButtonType.secondary,
                              onPressed: widget.controller.isLoading
                                  ? null
                                  : () {
                                      _closeByAction();
                                      widget.onSecondaryPressed?.call();
                                    },
                            ),
                          ),
                          AppSpacing.w12,
                        ],

                        if (widget.usePrimaryButton)
                          Expanded(
                            child: AppButton(
                              label: widget.primaryText ?? 'OK',
                              radius: AppSizes.s12,
                              paddingVertical: AppSizes.s12,
                              isLoading: widget.controller.isLoading,
                              onPressed: widget.controller.isLoading
                                  ? null
                                  : () {
                                      widget.onPrimaryPressed?.call();
                                    },
                            ),
                          ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
