import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app/constants/sc_room_msg_type.dart';
import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_room_repository_imp.dart';
import 'package:yumi/shared/business_logic/models/res/room_user_card_res.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:yumi/services/audio/rtm_manager.dart';

import '../../../shared/data_sources/models/enum/sc_room_roles_type.dart';
import '../../components/sc_tts.dart';
import '../../components/text/sc_text.dart';

class SetRoomRolePage extends StatefulWidget {
  RoomUserCardRes? userCardInfo;

  SetRoomRolePage(this.userCardInfo);

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

class _SetRoomRolePageState extends State<SetRoomRolePage> {
  String roomId = "";
  String roomAccount = "";
  String optTips = "";
  RtmProvider? rtmProvider;
  @override
  void initState() {
    super.initState();
    roomId =
        Provider.of<RtcProvider>(
          context,
          listen: false,
        ).currenRoom?.roomProfile?.roomProfile?.id ??
        "";
    rtmProvider = Provider.of<RtmProvider>(
      context,
      listen: false,
    );
    roomAccount =
        Provider.of<RtcProvider>(
          context,
          listen: false,
        ).currenRoom?.roomProfile?.roomProfile?.roomAccount ??
        "";
  }

  @override
  Widget build(BuildContext context) {
    optTips = SCAppLocalizations.of(context)!.operationSuccessful;
    return ClipRect(
      child: BackdropFilter(
        filter: ui.ImageFilter.blur(sigmaX: 15, sigmaY: 15),
        child: Container(
          width: ScreenUtil().screenWidth * 0.75,
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          decoration: BoxDecoration(
            color:  Color(0xff09372E).withOpacity(0.5),
            borderRadius: BorderRadius.all(Radius.circular(12.w)),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(height: 18.w),
              Row(
                children: [
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        if(widget.userCardInfo?.roomRole==SCRoomRolesType.ADMIN.name){
                          SCTts.show(SCAppLocalizations.of(context)!.alreadyAnAdministrator);
                          return;
                        }
                        Navigator.of(context).pop();
                        SCChatRoomRepository()
                            .changeRoomRole(
                              roomId,
                              SCRoomRolesType.ADMIN.name,
                              widget.userCardInfo?.userProfile?.id ?? "",
                              AccountStorage().getCurrentUser()?.userProfile?.id ??
                                  "",
                              "OTHER_CHANGE",
                            )
                            .whenComplete(() {
                              SCTts.show(optTips);
                              Msg msg = Msg(
                                groupId: roomAccount,
                                msg: 'ADMIN',
                                toUser: widget.userCardInfo?.userProfile,
                                type: SCRoomMsgType.roomRoleChange,
                              );
                              rtmProvider?.dispatchMessage(msg, addLocal: true);
                            });
                      },
                      child: Column(
                        children: [
                          Image.asset(
                            "sc_images/room/sc_icon_room_gly.png",
                            width: 30.w,
                            height: 30.w,
                          ),
                          SizedBox(height: 10.w),
                          text(
                            SCAppLocalizations.of(context)!.admin,
                            textColor: Color(0xffB1B1B1),
                            fontSize: 13.sp,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      child: Column(
                        children: [
                          Image.asset(
                            "sc_images/room/sc_icon_room_hy.png",
                            width: 30.w,
                            height: 30.w,
                          ),
                          SizedBox(height: 10.w),
                          text(
                            SCAppLocalizations.of(context)!.member,
                            textColor: Color(0xffB1B1B1),
                            fontSize: 13.sp,
                          ),
                        ],
                      ),
                      onTap: () {
                        if(widget.userCardInfo?.roomRole==SCRoomRolesType.MEMBER.name){
                          SCTts.show(SCAppLocalizations.of(context)!.alreadyAnMember);
                          return;
                        }
                        Navigator.of(context).pop();
                        SCChatRoomRepository()
                            .changeRoomRole(
                              roomId,
                              SCRoomRolesType.MEMBER.name,
                              widget.userCardInfo?.userProfile?.id ?? "",
                              AccountStorage().getCurrentUser()?.userProfile?.id ??
                                  "",
                              "OTHER_CHANGE",
                            )
                            .whenComplete(() {
                              SCTts.show(optTips);
                              Msg msg = Msg(
                                groupId: roomAccount,
                                msg: 'MEMBER',
                                toUser: widget.userCardInfo?.userProfile,
                                type: SCRoomMsgType.roomRoleChange,
                              );
                              rtmProvider?.dispatchMessage(msg, addLocal: true);
                            });
                      },
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      child: Column(
                        children: [
                          Image.asset(
                            "sc_images/room/sc_icon_room_guest.png",
                            width: 30.w,
                            height: 30.w,
                          ),
                          SizedBox(height: 10.w),
                          text(
                            SCAppLocalizations.of(context)!.guest,
                            textColor: Color(0xffB1B1B1),
                            fontSize: 13.sp,
                          ),
                        ],
                      ),
                      onTap: () {
                        if(widget.userCardInfo?.roomRole==SCRoomRolesType.TOURIST.name){
                          SCTts.show(SCAppLocalizations.of(context)!.alreadyAnTourist);
                          return;
                        }
                        Navigator.of(context).pop();
                        SCChatRoomRepository()
                            .changeRoomRole(
                              roomId,
                              SCRoomRolesType.TOURIST.name,
                              widget.userCardInfo?.userProfile?.id ?? "",
                              AccountStorage().getCurrentUser()?.userProfile?.id ??
                                  "",
                              "OTHER_CHANGE",
                            )
                            .whenComplete(() {
                              SCTts.show(optTips);
                              Msg msg = Msg(
                                groupId: roomAccount,
                                msg: 'TOURIST',
                                toUser: widget.userCardInfo?.userProfile,
                                type: SCRoomMsgType.roomRoleChange,
                              );
                              rtmProvider?.dispatchMessage(msg, addLocal: true);
                            });
                      },
                    ),
                  ),
                ],
              ),
              SizedBox(height: 20.w),
            ],
          ),
        ),
      ),
    );
  }
}
