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_localizations.dart';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart';
import 'package:yumi/ui_kit/widgets/room/room_user_info_card.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/constants/sc_room_msg_type.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/shared/tools/sc_lk_dialog_util.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/business_logic/models/res/mic_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';

class EmptyMaiSelect extends StatelessWidget {
  final num index;
  final SocialChatUserProfile? clickUser;

  const EmptyMaiSelect({Key? key, required this.index, this.clickUser})
    : super(key: key);

  @override
  Widget build(BuildContext context) {
    RtcProvider provider = Provider.of<RtcProvider>(context, listen: false);
    List<Widget> list = [];
    list.add(SizedBox(height: 10.w));
    if (index == -1) {
      if (provider.isFz() || provider.isGL()) {
        if (clickUser?.id !=
            AccountStorage().getCurrentUser()?.userProfile?.id) {
          list.add(
            _item(
              SCAppLocalizations.of(context)!.inviteToTheMicrophone,
              "sc_images/room/sc_icon_mic_go_up.png",
              context,
              () {
                Navigator.of(context).pop();
                Provider.of<RtmProvider>(
                  context,
                  listen: false,
                ).dispatchMessage(
                  Msg(
                    groupId:
                        provider
                            .currenRoom
                            ?.roomProfile
                            ?.roomProfile
                            ?.roomAccount ??
                        "",
                    msg:
                        AccountStorage()
                            .getCurrentUser()
                            ?.userProfile
                            ?.userNickname,
                    role: AccountStorage().getCurrentUser()?.userProfile?.id,
                    type: SCRoomMsgType.bsm,
                    toUser: clickUser,
                  ),
                  addLocal: false,
                );
              },
            ),
          );
        }
      }
    } else {
      MicRes? userWheat = provider.roomWheatMap[index];
      if (userWheat?.user?.id ==
          AccountStorage().getCurrentUser()?.userProfile?.id) {
        if (provider.isFz()) {
          ///点击的是自己
          if (userWheat?.micMute ?? false) {
            list.add(
              _item(
                SCAppLocalizations.of(context)!.openTheMic,
                "sc_images/room/sc_icon_mic_open.png",
                context,
                () {
                  Navigator.of(context).pop();
                  provider.jieJinMai(index);
                },
              ),
            );
          } else {
            list.add(
              _item(
                SCAppLocalizations.of(context)!.muteTheMic,
                "sc_images/room/sc_icon_mic_mute.png",
                context,
                () {
                  Navigator.of(context).pop();
                  provider.jinMai(index);
                },
              ),
            );
          }
        }

        list.add(
          _item(
            SCAppLocalizations.of(context)!.leavelTheMic,
            "sc_images/room/sc_icon_mic_leavel.png",
            context,
            () {
              Navigator.of(context).pop();
              provider.xiaMai(index);
            },
          ),
        );
      } else {
        if (provider.isFz() || provider.isGL()) {
          ///操作的人是房主,并且座位没人
          if (clickUser == null) {
            if (!(userWheat?.micLock ?? false)) {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.takeTheMic,
                  "sc_images/room/sc_icon_mic_go_up.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.shangMai(index);
                  },
                ),
              );
            }
            if ((userWheat?.micLock ?? false)) {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.unlockTheMic,
                  "sc_images/room/sc_icon_mic_unlock.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.jieFeng(index);
                  },
                ),
              );
            } else {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.lockTheMic,
                  "sc_images/room/sc_icon_mic_lock.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.fengMai(index);
                  },
                ),
              );
            }
          }

          if (clickUser?.roles == SCRoomRolesType.HOMEOWNER.name ||
              clickUser?.roles == SCRoomRolesType.ADMIN.name) {
            if (provider.isFz()) {
              if (userWheat?.micMute ?? false) {
                list.add(
                  _item(
                    SCAppLocalizations.of(context)!.openTheMic,
                    "sc_images/room/sc_icon_mic_open.png",
                    context,
                    () {
                      Navigator.of(context).pop();
                      provider.jieJinMai(index);
                    },
                  ),
                );
              } else {
                list.add(
                  _item(
                    SCAppLocalizations.of(context)!.muteTheMic,
                    "sc_images/room/sc_icon_mic_mute.png",
                    context,
                    () {
                      Navigator.of(context).pop();
                      provider.jinMai(index);
                    },
                  ),
                );
              }
            }
          } else {
            if (userWheat?.micMute ?? false) {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.openTheMic,
                  "sc_images/room/sc_icon_mic_open.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.jieJinMai(index);
                  },
                ),
              );
            } else {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.muteTheMic,
                  "sc_images/room/sc_icon_mic_mute.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.jinMai(index);
                  },
                ),
              );
            }
          }
        } else {
          if (userWheat?.user == null) {
            if (!(userWheat?.micLock ?? false)) {
              list.add(
                _item(
                  SCAppLocalizations.of(context)!.takeTheMic,
                  "sc_images/room/sc_icon_mic_go_up.png",
                  context,
                  () {
                    Navigator.of(context).pop();
                    provider.shangMai(index);
                  },
                ),
              );
            } else {
              // list.add(
              //   _item('Leavel the mic ', "sc_images/room/sc_icon_mic_leavel.png", () {
              //     Navigator.of(context).pop();
              //     provider.xiaMai(index);
              //   }),
              // );
            }
          }
        }
      }
    }
    if (clickUser != null) {
      list.add(
        _item(
          SCAppLocalizations.of(context)!.openUserProfleCard,
          "sc_images/room/sc_icon_open_card.png",
          context,
          () {
            Navigator.of(context).pop();
            showBottomInCenterDialog(
              context!,
              RoomUserInfoCard(userId: clickUser?.id),
            );
          },
        ),
      );
    }
    list.add(
      _item(SCAppLocalizations.of(context)!.cancel, null, context, () {
        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,
    BuildContext context,
    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: [
                Container(
                  alignment: Alignment.center,
                  child: Text(
                    msg,
                    style: TextStyle(
                      fontSize: sp(16),
                      color: Colors.white,
                      fontWeight: FontWeight.w400,
                      decoration: TextDecoration.none,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
