import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/action_sheet_data.dart';
import 'package:didvan/models/view/alert_data.dart';
import 'package:didvan/views/widgets/didvan/button.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:rive/rive.dart';

class ActionSheetUtils {
  static late BuildContext context;

  static Future<void> showLogoLoadingIndicator() async {
    await showDialog(
      context: context,
      builder: (context) => Padding(
        padding: EdgeInsets.symmetric(
          horizontal: MediaQuery.of(context).size.width / 3,
        ),
        child: RiveAnimation.asset(Assets.logoLoadingAnimation),
      ),
    );
  }

  static Future<void> showAlert(AlertData alertData) async {
    bool isInit = true;
    Color backgroundColor;
    Color foregroundColor;
    switch (alertData.aLertType) {
      case ALertType.info:
        backgroundColor = Theme.of(context).colorScheme.focused;
        foregroundColor = Theme.of(context).colorScheme.focusedBorder;
        break;
      case ALertType.success:
        backgroundColor = Theme.of(context).colorScheme.successBack;
        foregroundColor = Theme.of(context).colorScheme.success;
        break;
      case ALertType.error:
        backgroundColor = Theme.of(context).colorScheme.errorBack;
        foregroundColor = Theme.of(context).colorScheme.error;
        break;
    }
    BotToast.showNotification(
      backgroundColor: backgroundColor,
      title: (cancelFunc) => StatefulBuilder(
        builder: (context, setState) {
          if (isInit) {
            Future.delayed(Duration.zero, () {
              setState(() {});
              isInit = false;
            });
          }
          return Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              DidvanText(alertData.message, color: foregroundColor),
              AnimatedContainer(
                duration: const Duration(seconds: 2),
                width: isInit ? MediaQuery.of(context).size.width - 32 : 0,
                height: 2,
                color: foregroundColor,
              ),
            ],
          );
        },
      ),
    );
  }

  static Future<void> showBottomSheet({required ActionSheetData data}) async {
    await showModalBottomSheet(
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          borderRadius: const BorderRadius.vertical(
            top: Radius.circular(10),
          ),
        ),
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Center(
                child: Container(
                  height: 3,
                  width: 50,
                  color: Theme.of(context).colorScheme.hint,
                ),
              ),
              const SizedBox(height: 8),
              Row(
                children: [
                  if (data.titleIcon != null)
                    Icon(
                      data.titleIcon,
                      color: data.titleColor ??
                          Theme.of(context).colorScheme.title,
                    ),
                  if (data.titleIcon != null) const SizedBox(width: 8),
                  DidvanText(
                    data.title,
                    style: Theme.of(context).textTheme.subtitle1,
                    color:
                        data.titleColor ?? Theme.of(context).colorScheme.title,
                  )
                ],
              ),
              const SizedBox(height: 28),
              data.content,
              const SizedBox(height: 28),
              if (!data.withoutButtonMode)
                Row(
                  children: [
                    if (data.hasDismissButton)
                      Expanded(
                        child: DidvanButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                            data.onDismissed?.call();
                          },
                          title: data.dismissTitle ?? 'بازگشت',
                          style: ButtonStyleMode.secondary,
                        ),
                      ),
                    if (data.hasDismissButton) const SizedBox(width: 20),
                    if (data.hasConfirmButton)
                      Expanded(
                        flex: data.smallDismissButton ? 2 : 1,
                        child: DidvanButton(
                          style: ButtonStyleMode.primary,
                          onPressed: () {
                            Navigator.of(context).pop();
                            data.onConfirmed?.call();
                          },
                          title: data.confrimTitle ?? 'تایید',
                        ),
                      ),
                  ],
                ),
            ],
          ),
        ),
      ),
    );
  }

  static Future<void> openDialog({required ActionSheetData data}) async {
    await showDialog(
      context: context,
      builder: (context) => Dialog(
        backgroundColor: Theme.of(context).colorScheme.surface,
        shape: const RoundedRectangleBorder(
          borderRadius: DesignConfig.mediumBorderRadius,
        ),
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Icon(
                      data.titleIcon,
                      size: 20,
                      color: data.titleColor,
                    ),
                  ),
                  const SizedBox(
                    width: 8,
                  ),
                  Expanded(
                    child: DidvanText(
                      data.title,
                      style: Theme.of(context).textTheme.headline3,
                      color: data.titleColor,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 12,
              ),
              data.content,
              const SizedBox(
                height: 12,
              ),
              Row(
                children: [
                  if (data.hasDismissButton)
                    Expanded(
                      child: DidvanButton(
                        onPressed: data.onDismissed ?? () => pop(),
                        title: data.dismissTitle ?? 'بازگشت',
                        style: ButtonStyleMode.flat,
                      ),
                    ),
                  if (data.hasDismissButton)
                    const SizedBox(
                      width: 20,
                    ),
                  Expanded(
                    child: DidvanButton(
                      onPressed: () {
                        pop();
                        data.onConfirmed?.call();
                      },
                      title: data.confrimTitle ?? 'تایید',
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  static void pop() {
    DesignConfig.updateSystemUiOverlayStyle();
    Navigator.of(context).pop();
  }
}
