import 'dart:ui' as ui;
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/app_localizations.dart';
import 'package:yumi/ui_kit/widgets/room/set_room_role_page.dart';
import 'package:provider/provider.dart';

import 'package:yumi/ui_kit/components/dialog/dialog_base.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/shared/tools/sc_lk_dialog_util.dart';
import 'package:yumi/shared/business_logic/models/res/room_user_card_res.dart';
import 'package:yumi/services/audio/rtc_manager.dart';

import '../../../shared/data_sources/models/enum/sc_room_roles_type.dart';

class RoomUserCardSetting extends StatefulWidget {
  String? roomId;
  RoomUserCardRes? userCardInfo;

  RoomUserCardSetting({super.key, this.roomId, this.userCardInfo});

  @override
  _RoomUserCardSettingState createState() => _RoomUserCardSettingState();
}

class _RoomUserCardSettingState extends State<RoomUserCardSetting> {
  bool isOnMai = false;
  bool micMute = false;

  @override
  void initState() {
    super.initState();
    isOnMai =
        Provider.of<RtcProvider>(
          context,
          listen: false,
        ).userOnMaiInIndex(widget.userCardInfo?.userProfile?.id ?? "") !=
        -1;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> list = [];
    list.add(SizedBox(height: 10.w));
    if (isOnMai) {
      if (Provider.of<RtcProvider>(context, listen: false).isFz()) {
        ///操作的人是房主
        list.add(
          _item(SCAppLocalizations.of(context)!.removeTheMic, null, () {
            Navigator.of(context).pop();
            Provider.of<RtcProvider>(
              context,
              listen: false,
            ).killXiaMai(widget.userCardInfo?.userProfile?.id ?? "");
          }),
        );
      } else {
        if (widget.userCardInfo?.roomRole != SCRoomRolesType.ADMIN.name &&
            widget.userCardInfo?.roomRole != SCRoomRolesType.HOMEOWNER.name) {
          list.add(
            _item(SCAppLocalizations.of(context)!.removeTheMic, null, () {
              Navigator.of(context).pop();
              Provider.of<RtcProvider>(
                context,
                listen: false,
              ).killXiaMai(widget.userCardInfo?.userProfile?.id ?? "");
            }),
          );
        }
      }
    }
    if (Provider.of<RtcProvider>(context, listen: false).isFz()) {
      list.add(
        _item(SCAppLocalizations.of(context)!.setUpAnIdentity, null, () {
          Navigator.of(context).pop();
          showCenterDialog(context, SetRoomRolePage(widget.userCardInfo));
        }),
      );
    }
    if (widget.userCardInfo?.roomRole != SCRoomRolesType.ADMIN.name &&
        widget.userCardInfo?.roomRole != SCRoomRolesType.HOMEOWNER.name) {
      list.add(
        _item(SCAppLocalizations.of(context)!.kickedOutOfRoom, null, () {
          Navigator.of(context).pop();
          SmartDialog.show(
            tag: "showKickedOutOfRoomDialog",
            maskColor: Colors.black26,
            alignment: Alignment.center,
            animationType: SmartAnimationType.fade,
            clickMaskDismiss: true,
            builder: (_) {
              return KickedOutOfRoomDialog(
                roomId: widget.roomId ?? "",
                userId: widget.userCardInfo?.userProfile?.id ?? "",
              );
            },
          );
        }),
      );
    }
    list.add(
      _item(SCAppLocalizations.of(context)!.cancel, null, () {
        Navigator.of(context).pop();
      }),
    );
    list.add(SizedBox(height: 10.w));
    return SafeArea(
      child: ClipRRect(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(12.0),
          topRight: Radius.circular(12.0),
        ),
        child: BackdropFilter(
          filter: ui.ImageFilter.blur(sigmaX: 15, sigmaY: 15),
          child: Container(
            width: ScreenUtil().screenWidth,
            color: Color(0xff09372E).withOpacity(0.5),
            child: Column(children: list),
          ),
        ),
      ),
    );
  }

  Widget _item(String msg, String? icon, Function onClick) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        onClick();
      },
      child: Column(
        children: <Widget>[
          Container(
            decoration:
            msg == SCAppLocalizations.of(context)!.cancel
                ? BoxDecoration(
              color: Color(0xff18F2B1).withOpacity(0.1),
              borderRadius: BorderRadius.all(Radius.circular(32.w)),
            )
                : null,
            width: ScreenUtil().screenWidth * 0.7,
            padding: EdgeInsets.symmetric(vertical: 10.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                icon != null
                    ? Image.asset(icon, height: 18.w, width: 18.w)
                    : Container(),
                SizedBox(width: 5.w),
                Container(
                  alignment: Alignment.center,
                  child: Text(
                    msg,
                    style: TextStyle(
                      fontSize: sp(16),
                      color:Colors.white,
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
