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/app/constants/sc_room_msg_type.dart';
import 'package:yumi/shared/tools/sc_room_utils.dart';
import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart';
import 'package:yumi/ui_kit/widgets/room/switch_model/room_mic_switch_page.dart';
import 'package:yumi/main.dart';
import 'package:provider/provider.dart';
import 'package:yumi/shared/tools/sc_pick_utils.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:yumi/services/audio/rtm_manager.dart';
import '../../../app/routes/sc_fluro_navigator.dart';
import '../../../modules/index/main_route.dart';
import '../../../modules/room/voice_room_route.dart';
import '../../../modules/store/store_route.dart';
import '../../components/sc_debounce_widget.dart';
import '../../components/sc_tts.dart';
import '../../components/text/sc_text.dart';

class RoomMenuDialog extends StatefulWidget {
  int roomMenuStime = 0;
  Function(int eTime) callBack;

  RoomMenuDialog(this.roomMenuStime, this.callBack);

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

class _RoomMenuDialogState extends State<RoomMenuDialog> {
  List<RoomMenu> items1 = [];
  List<RoomMenu> items2 = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // items1.clear();
    // items1.add(
    //   RoomMenu(
    //     0,
    //     SCAppLocalizations.of(context)!.music,
    //     "sc_icon_room_music_tag.png",
    //   ),
    // );
    // items1.add(
    //   RoomMenu(
    //     1,
    //     SCAppLocalizations.of(context)!.redEnvelope,
    //     "sc_icon_redpackg_tag.png",
    //   ),
    // );
    // items1.add(
    //   RoomMenu(3, SCAppLocalizations.of(context)!.rps, "sc_icon_rps_tag.png"),
    // );
    // items1.add(
    //   RoomMenu(2, SCAppLocalizations.of(context)!.dice, "sc_icon_dice_tag.png"),
    // );
    // items1.add(
    //   RoomMenu(
    //     4,
    //     SCAppLocalizations.of(context)!.luckNumber,
    //     "sc_icon_luck_number_tag.png",
    //   ),
    // );
    items2.clear();
    if (Provider.of<RtcProvider>(context, listen: false).isFz()) {
      items2.add(
        RoomMenu(
          0,
          SCAppLocalizations.of(context)!.micManagement,
          "sc_icon_menu_mic_model_change.png",
        ),
      );
    }

    items2.add(
      RoomMenu(
        1,
        SCAppLocalizations.of(context)!.clearMessage,
        "sc_icon_room_msg_clear.png",
      ),
    );
    items2.add(
      RoomMenu(
        2,
        SCAppLocalizations.of(context)!.picture,
        "sc_icon_room_msg_pic.png",
      ),
    );
    if (Provider.of<RtcProvider>(context, listen: false).isFz()) {
      items2.add(
        RoomMenu(
          3,
          SCAppLocalizations.of(context)!.settings,
          "sc_icon_room_menu_settins.png",
        ),
      );
    }
    items2.add(
      RoomMenu(
        4,
        SCAppLocalizations.of(context)!.sound2,
        Provider.of<RtcProvider>(context, listen: false).roomIsMute
            ? "sc_icon_mic_mute.png"
            : "sc_icon_mic_open.png",
      ),
    );
    if (!Provider.of<RtcProvider>(context, listen: false).isFz()) {
      items2.add(
        RoomMenu(
          5,
          SCAppLocalizations.of(context)!.report,
          "sc_icon_room_report.png",
        ),
      );
    }
    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,
            height: 200.w,
            color: Color(0xff09372E).withOpacity(0.5),
            child: Column(
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        SizedBox(height: 15.w),
                        GridView.builder(
                          physics: NeverScrollableScrollPhysics(),
                          padding: EdgeInsets.only(bottom: 2.w),
                          shrinkWrap: true,
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 4, // 每行3个项目
                                crossAxisSpacing: 2,
                                mainAxisSpacing: 2,
                                childAspectRatio: 1,
                              ),
                          itemBuilder: (c, i) {
                            return _buildItem2(items2[i], i);
                          },
                          itemCount: items2.length,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildItem2(RoomMenu item, int i) {
    return SCDebounceWidget(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset("sc_images/room/${item.icon}", width: 45.w, height: 45.w),
          SizedBox(height: 10.w),
          text(
            item.title,
            fontSize: 12.sp,
            textColor: Colors.white,
            fontWeight: FontWeight.w600,
            lineHeight: 1.1,
            letterSpacing: 0.1,
            maxLines: 2,
            textAlign: TextAlign.center,
          ),
        ],
      ),
      onTap: () async {
        if (item.id == 0) {
          SmartDialog.dismiss(tag: "showRoomMenuDialog");
          SmartDialog.show(
            tag: "showRoomMicSwitch",
            alignment: Alignment.bottomCenter,
            animationType: SmartAnimationType.fade,
            debounce: true,
            builder: (_) {
              return RoomMicSwitchPage();
            },
          );
        } else if (item.id == 1) {
          Provider.of<RtmProvider>(context, listen: false).clearMessage();
          SmartDialog.dismiss(tag: "showRoomMenuDialog");
        } else if (item.id == 2) {
          if (SCRoomUtils.touristCanMsg(context)) {
            SCPickUtils.pickImage(context, (bool success, String url) {
              if (success) {
                Provider.of<RtmProvider>(
                  context,
                  listen: false,
                ).dispatchMessage(
                  Msg(
                    groupId:
                        Provider.of<RtcProvider>(
                          context,
                          listen: false,
                        ).currenRoom?.roomProfile?.roomProfile?.roomAccount,
                    msg: url,
                    type: SCRoomMsgType.image,
                    role:
                        Provider.of<RtcProvider>(
                          context,
                          listen: false,
                        ).currenRoom?.entrants?.roles ??
                        "",
                    user: AccountStorage().getCurrentUser()?.userProfile,
                  ),
                  addLocal: true,
                );
              }
              SmartDialog.dismiss(tag: "showRoomMenuDialog");
            }, neeCrop: false);
          }
        } else if (item.id == 3) {
          SmartDialog.dismiss(tag: "showRoomMenuDialog");
          SCNavigatorUtils.push(
            navigatorKey.currentState!.context,
            "${VoiceRoomRoute.roomEdit}?need=false",
            replace: false,
          );
        } else if (item.id == 4) {
          SmartDialog.dismiss(tag: "showRoomMenuDialog");
          Provider.of<RtcProvider>(
            context,
            listen: false,
          ).toggleRemoteAudioMuteForAllUsers();
        } else if (item.id == 5) {
          SmartDialog.dismiss(tag: "showRoomMenuDialog");
          SCNavigatorUtils.push(
            navigatorKey.currentState!.context,
            "${SCMainRoute.report}?type=room&tageId=${Provider.of<RtcProvider>(context, listen: false).currenRoom?.roomProfile?.roomProfile?.id}",
            replace: false,
          );
        }
      },
    );
  }
}

class RoomMenu {
  int id = 0;

  String title = "";
  String icon = "";

  RoomMenu(this.id, this.title, this.icon);
}
