import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:yumi/shared/tools/sc_room_utils.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:yumi/ui_kit/widgets/sc_lk_tap_widget.dart';
import 'package:yumi/ui_kit/components/dialog/dialog_base.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/app/constants/sc_screen.dart';

void showEnterRoomConfirm(
  String roomId,
  BuildContext context, {
  bool needOpenRedenvelope = false,
  bool fromFloting = false,
  String redPackId = "",
}) {
  if (roomId.isNotEmpty) {
    SmartDialog.dismiss(tag: "showConfirmDialog");
    SmartDialog.show(
      tag: "showConfirmDialog",
      alignment: Alignment.center,
      animationType: SmartAnimationType.fade,
      builder: (_) {
        return MsgDialog(
          title:
              fromFloting
                  ? SCAppLocalizations.of(context)!.enterThisVoiceChatRoom
                  : SCAppLocalizations.of(context)!.tips,
          msg:
              fromFloting
                  ? SCAppLocalizations.of(
                    context,
                  )!.swipeLeftOnTheFloatingScreenAreaToQuicklyCloseIt
                  : SCAppLocalizations.of(context)!.enterRoomConfirmTips,
          btnText: SCAppLocalizations.of(context)!.confirm,
          onEnsure: () {
            Provider.of<RealTimeCommunicationManager>(context, listen: false).joinVoiceRoomSession(
              context,
              roomId,
              needOpenRedenvelope: needOpenRedenvelope,
              redPackId: redPackId,
            );
          },
        );
      },
    );
  }
}

///中间对话框 半透明
Future showCenterDialog(
  BuildContext context,
  Widget child, {
  Color? barrierColor,
}) {
  return showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    transitionDuration: Duration(milliseconds: 350),
    barrierColor: barrierColor ?? Colors.black.withOpacity(0.2),
    pageBuilder: (
      BuildContext context,
      Animation animation,
      Animation secondaryAnimation,
    ) {
      return Center(child: child);
    },
  );
}

///中间对话框 半透明
showCenterAnimationDialog(BuildContext context, Widget child) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    transitionDuration: Duration(milliseconds: 800),
    barrierColor: Colors.black.withOpacity(0.2),
    pageBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
    ) {
      print(
        'showCenterAnimationDialog:${animation.value},${secondaryAnimation.value}',
      );
      CurvedAnimation curvedAnimation = CurvedAnimation(
        curve: Curves.bounceOut,
        reverseCurve: Curves.easeInExpo,
        parent: animation,
      );
      return Center(
        child: ScaleTransition(
          scale: Tween(begin: 0.0, end: 1.0).animate(curvedAnimation),
          child: SlideTransition(
            position: Tween(
              begin: Offset(0.0, 0.0),
              end: Offset(0.0, 3.0),
            ).animate(secondaryAnimation),
            child: FadeTransition(
              opacity: Tween(begin: 0.0, end: 1.0).animate(curvedAnimation),
              child: child,
            ),
          ),
        ),
      );
    },
  );
}

///中间对话框 半透明(重下面弹出)
showBottomInCenterDialog(BuildContext context, Widget child) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    transitionDuration: Duration(milliseconds: 350),
    barrierColor: Colors.black.withOpacity(0.6),
    pageBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation secondaryAnimation,
    ) {
      return SlideTransition(
        position: Tween(
          begin: Offset(0.0, 2.0),
          end: Offset(0.0, 0.0),
        ).chain(CurveTween(curve: Curves.ease)).animate(animation),
        child: Center(child: child),
      );
    },
  );
}

///中间对话框 半透明 点外部不消失
showCenterDialog2(BuildContext context, Widget child, {Color? mask}) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: false,
    transitionDuration: Duration(milliseconds: 350),
    barrierColor: mask ?? Colors.black.withOpacity(0.6),
    pageBuilder: (
      BuildContext context,
      Animation animation,
      Animation secondaryAnimation,
    ) {
      return Center(child: child);
    },
  );
}

///底部对话框 透明
showBottomDialog(
  BuildContext context,
  Widget child, {
  Color? barrierColor,
  Duration? transitionDuration,
}) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    barrierColor: barrierColor ?? Colors.transparent,
    transitionDuration: transitionDuration ?? Duration(milliseconds: 350),
    pageBuilder: (
      BuildContext context,
      Animation animation,
      Animation secondaryAnimation,
    ) {
      return Column(
        children: <Widget>[Expanded(child: SizedBox(width: width(1))), child],
      );
    },
  );
}

///底部对话框 透明(从下面弹出)
showBottomInBottomDialog(
  BuildContext context,
  Widget child, {
  Color? barrierColor,
  bool barrierDismissible = true,
}) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: barrierDismissible,
    transitionDuration: Duration(milliseconds: 325),
    barrierColor: barrierColor ?? Colors.black.withOpacity(0.3),
    pageBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation secondaryAnimation,
    ) {
      return Column(
        children: <Widget>[
          Expanded(child: SizedBox(width: width(1))),
          SlideTransition(
            position: Tween(
              begin: Offset(0.0, 2.0),
              end: Offset(0.0, 0.0),
            ).chain(CurveTween(curve: Curves.ease)).animate(animation),
            child: child,
          ),
        ],
      );
    },
  );
}

///底部对话框 半透明
showBottomDialog2(BuildContext context, Widget child) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    transitionDuration: Duration(milliseconds: 350),
    barrierColor: Colors.black.withOpacity(0.2),
    pageBuilder: (
      BuildContext context,
      Animation animation,
      Animation secondaryAnimation,
    ) {
      return Column(
        children: <Widget>[Expanded(child: SizedBox(width: width(1))), child],
      );
    },
  );
}

///显示菜单
showMenuPop(BuildContext context, Widget child) {
  showGeneralDialog(
    context: context,
    barrierLabel: '',
    barrierDismissible: true,
    transitionDuration: Duration(milliseconds: 350),
    pageBuilder: (
      BuildContext context,
      Animation animation,
      Animation secondaryAnimation,
    ) {
      return Row(
        children: <Widget>[
          Spacer(),
          Column(
            children: <Widget>[SizedBox(height: height(55)), child, Spacer()],
          ),
          SizedBox(width: width(15)),
        ],
      );
    },
  );
}

class SCForceDialog extends StatelessWidget {
  final String msg;
  final Function onClick;

  const SCForceDialog({Key? key, required this.msg, required this.onClick})
    : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width(269),
      decoration: ShapeDecoration(
        color: Color(0xffffffff),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(width(10))),
        ),
      ),
      child: _bdg(context),
    );
  }

  Widget _bdg(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: width(30),
            vertical: height(15),
          ),
          alignment: Alignment.center,
          child: Text(
            msg,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: sp(16),
              color: Color(0xff333333),
              fontWeight: FontWeight.w500,
              decoration: TextDecoration.none,
            ),
          ),
        ),
        Divider(height: height(1), color: SocialChatTheme.dividerColor),
        Container(
          height: height(46),
          alignment: Alignment.center,
          child: Row(
            children: <Widget>[
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Text(
                    "确定",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: sp(16),
                      color: Color(0xfFFFF4FBB),
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    if (onClick != null) {
                      onClick();
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

///提示对话框
class SCTsDialog extends StatelessWidget {
  final String msg;
  final Function onClick;

  const SCTsDialog({Key? key, required this.msg, required this.onClick})
    : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width(269),
      decoration: ShapeDecoration(
        color: Color(0xffffffff),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(width(10))),
        ),
      ),
      child: _bdg(context),
    );
  }

  Widget _bdg(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: width(30),
            vertical: height(25),
          ),
          alignment: Alignment.center,
          child: Text(
            msg,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: sp(16),
              color: Color(0xff333333),
              fontWeight: FontWeight.w500,
              decoration: TextDecoration.none,
            ),
          ),
        ),
        Divider(height: height(1), color: SocialChatTheme.dividerColor),
        Container(
          height: height(46),
          alignment: Alignment.center,
          child: Row(
            children: <Widget>[
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Text(
                    "取消",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: sp(16),
                      color: Color(0xFFAAAAAA),
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                ),
              ),
              VerticalDivider(width: width(1), color: SocialChatTheme.dividerColor),
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Text(
                    "确定",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: sp(16),
                      color: SocialChatTheme.primaryColor,
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    if (onClick != null) {
                      onClick();
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

///提示对话框
class SCTsDialog2 extends StatelessWidget {
  final String msg;
  final String btmText;
  final Function onClick;

  const SCTsDialog2({
    Key? key,
    required this.msg,
    required this.onClick,
    required this.btmText,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width(269),
      decoration: ShapeDecoration(
        color: Color(0xffffffff),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(width(10))),
        ),
      ),
      child: _bdg(context),
    );
  }

  Widget _bdg(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: width(30),
            vertical: height(15),
          ),
          alignment: Alignment.center,
          child: Text(
            msg,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: sp(16),
              color: Color(0xff333333),
              fontWeight: FontWeight.w500,
              decoration: TextDecoration.none,
            ),
          ),
        ),
        Divider(color: Color(0xffF3F3F3), endIndent: 15.w, indent: 15.w),
        Container(
          height: height(46),
          alignment: Alignment.center,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              SizedBox(width: 15.w),
              Expanded(
                child: SCLkTapWidget(
                  borderRadius: BorderRadius.circular(52),
                  child: Container(
                    width: 100.w,
                    height: 35.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Color(0xffF3F3F3),
                      borderRadius: BorderRadius.circular(52),
                    ),
                    child: Text(
                      "在考虑下",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: sp(16),
                        color: Color(0xFF333333),
                        fontWeight: FontWeight.w400,
                        decoration: TextDecoration.none,
                      ),
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                ),
              ),
              SizedBox(width: 35.w),
              Expanded(
                child: SCLkTapWidget(
                  borderRadius: BorderRadius.circular(52),
                  child: Container(
                    width: 100.w,
                    height: 35.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Color(0xffBA0CF9),
                      borderRadius: BorderRadius.circular(52),
                    ),
                    child: Text(
                      "$btmText",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: sp(16),
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                        decoration: TextDecoration.none,
                      ),
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    if (onClick != null) {
                      onClick();
                    }
                  },
                ),
              ),
              SizedBox(width: 15.w),
            ],
          ),
        ),
      ],
    );
  }
}

// 提示框
class SCPromptDialog extends StatelessWidget {
  final String msg;
  final Function onClick;

  const SCPromptDialog({Key? key, required this.msg, required this.onClick})
    : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width(269),
      decoration: ShapeDecoration(
        color: Color(0xffffffff),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(width(10))),
        ),
      ),
      child: _bdg(context),
    );
  }

  Widget _bdg(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: EdgeInsets.symmetric(
            horizontal: width(30),
            vertical: height(15),
          ),
          alignment: Alignment.center,
          child: Text(
            msg,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: sp(16),
              color: Color(0xff333333),
              fontWeight: FontWeight.w500,
              decoration: TextDecoration.none,
            ),
          ),
        ),
        Divider(height: height(1), color: SocialChatTheme.dividerColor),
        Container(
          height: height(46),
          alignment: Alignment.center,
          child: Row(
            children: <Widget>[
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Text(
                    "我知道了",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: sp(16),
                      color: Color(0xffFFE400),
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    if (onClick != null) {
                      onClick();
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
