import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';

enum KwDialogState { neutral, positive, negative, warning, info }

class KwDialog extends StatefulWidget {
  final SvgGenImage icon;
  final KwDialogState state;
  final String title;
  final String message;
  final String? primaryButtonLabel;
  final String? secondaryButtonLabel;
  final void Function(BuildContext dialogContext)? onPrimaryButtonPressed;
  final void Function(BuildContext dialogContext)? onSecondaryButtonPressed;
  final Widget? child;

  const KwDialog({
    super.key,
    required this.icon,
    required this.state,
    required this.title,
    required this.message,
    this.primaryButtonLabel,
    this.secondaryButtonLabel,
    this.onPrimaryButtonPressed,
    this.onSecondaryButtonPressed,
    this.child,
  });

  @override
  State<KwDialog> createState() => _KwDialogState();

  static Future<R?> show<R>({
    required BuildContext context,
    required SvgGenImage icon,
    KwDialogState state = KwDialogState.neutral,
    required String title,
    required String message,
    String? primaryButtonLabel,
    String? secondaryButtonLabel,
    bool barrierDismissible = true,
    void Function(BuildContext dialogContext)? onPrimaryButtonPressed,
    void Function(BuildContext dialogContext)? onSecondaryButtonPressed,
    Widget? child,
  }) async {
    return showDialog<R>(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) => KwDialog(
        icon: icon,
        state: state,
        title: title,
        message: message,
        primaryButtonLabel: primaryButtonLabel,
        secondaryButtonLabel: secondaryButtonLabel,
        onPrimaryButtonPressed: onPrimaryButtonPressed,
        onSecondaryButtonPressed: onSecondaryButtonPressed,
        child: child,
      ),
    );
  }
}

class _KwDialogState extends State<KwDialog> {
  Color get _iconColor {
    switch (widget.state) {
      case KwDialogState.neutral:
        return AppColors.blackBlack;
      case KwDialogState.positive:
        return AppColors.statusSuccess;
      case KwDialogState.negative:
        return AppColors.statusError;
      case KwDialogState.warning:
        return AppColors.statusWarning;
      case KwDialogState.info:
        return AppColors.primaryBlue;
    }
  }

  Color get _iconBgColor {
    switch (widget.state) {
      case KwDialogState.neutral:
        return AppColors.tintGray;
      case KwDialogState.positive:
        return AppColors.tintGreen;
      case KwDialogState.negative:
        return AppColors.tintRed;
      case KwDialogState.warning:
        return AppColors.tintYellow;
      case KwDialogState.info:
        return AppColors.tintBlue;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedSize(
        duration: const Duration(milliseconds: 300),
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          decoration: BoxDecoration(
            color: AppColors.grayWhite,
            borderRadius: BorderRadius.circular(24),
          ),
          child: RawScrollbar(
            thumbVisibility: true,
            thumbColor: AppColors.blackCaptionText,
            radius: const Radius.circular(20),
            crossAxisMargin: 4,
            mainAxisMargin: 24,
            thickness: 6,
            child: SingleChildScrollView(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 56),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  AnimatedContainer(
                    duration: const Duration(milliseconds: 300),
                    height: 64,
                    width: 64,
                    decoration: BoxDecoration(
                      color: _iconBgColor,
                      shape: BoxShape.circle,
                    ),
                    child: Center(
                      child: widget.icon.svg(
                        width: 32,
                        height: 32,
                        colorFilter:
                            ColorFilter.mode(_iconColor, BlendMode.srcIn),
                      ),
                    ),
                  ),
                  const Gap(32),
                  Text(
                    widget.title,
                    style: AppTextStyles.headingH1.copyWith(height: 1),
                    textAlign: TextAlign.center,
                  ),
                  const Gap(8),
                  Text(
                    widget.message,
                    style: AppTextStyles.bodyMediumReg
                        .copyWith(color: AppColors.blackGray),
                    textAlign: TextAlign.center,
                  ),
                  if (widget.child != null) ...[
                    const Gap(8),
                    widget.child!,
                  ],
                  const Gap(24),
                  ..._buttonGroup(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> _buttonGroup() {
    return [
      if (widget.primaryButtonLabel != null)
        KwButton.primary(
          label: widget.primaryButtonLabel ?? '',
          onPressed: () {
            if (widget.onPrimaryButtonPressed != null) {
              widget.onPrimaryButtonPressed?.call(context);
            } else {
              Navigator.of(context).pop();
            }
          },
        ),
      if (widget.primaryButtonLabel != null &&
          widget.secondaryButtonLabel != null)
        const Gap(8),
      if (widget.secondaryButtonLabel != null)
        KwButton.outlinedPrimary(
          label: widget.secondaryButtonLabel ?? '',
          onPressed: () {
            if (widget.onSecondaryButtonPressed != null) {
              widget.onSecondaryButtonPressed?.call(context);
            } else {
              Navigator.of(context).pop();
            }
          },
        ),
    ];
  }
}
