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/components/text/sc_text.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:provider/provider.dart';

import 'package:yumi/ui_kit/components/dialog/dialog_base.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_room_repository_imp.dart';
import 'package:yumi/shared/business_logic/models/res/join_room_res.dart';
import 'package:yumi/services/audio/rtc_manager.dart';

class RoomSeatNumberPage extends StatefulWidget {
  @override
  _RoomSeatNumberPageState createState() => _RoomSeatNumberPageState();
}

class _RoomSeatNumberPageState extends State<RoomSeatNumberPage> {
  num mikeSize = 10;

  @override
  void initState() {
    super.initState();
    mikeSize =
        Provider.of<RtcProvider>(
          context,
          listen: false,
        ).currenRoom?.roomProfile?.roomSetting?.mikeSize ??
        10;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 8.w, vertical: 5.w),
      child: Column(
        children: [
          SizedBox(height: 5.w),
          Row(
            spacing: 8.w,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.w),
                    image: DecorationImage(
                      image: AssetImage("sc_images/room/sc_icon_seat_num_bg.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                  child: Stack(
                    alignment: AlignmentDirectional.center,
                    children: [
                      GestureDetector(
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 12.w),
                          decoration: BoxDecoration(
                            color: Colors.white10,
                            borderRadius: BorderRadius.circular(12.w),
                            border: Border.all(
                              color:
                                  mikeSize == 5
                                      ? SocialChatTheme.primaryLight
                                      : Colors.transparent,
                              width: 2.w,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Column(
                                spacing: 5.w,
                                children: [
                                  SizedBox(height: 5.w),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                    color: Colors.transparent,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                    color: Colors.transparent,
                                  ),
                                  text(
                                    textColor: Colors.white,
                                    SCAppLocalizations.of(
                                      context,
                                    )!.classicMic("5"),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 12.sp,
                                  ),
                                  SizedBox(height: 5.w),
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          mikeSize = 5;
                          setState(() {});
                        },
                      ),
                    ],
                  ),
                ),
              ),

              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.w),
                    image: DecorationImage(
                      image: AssetImage("sc_images/room/sc_icon_seat_num_bg.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                  child: Stack(
                    alignment: AlignmentDirectional.center,
                    children: [
                      GestureDetector(
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 12.w),
                          decoration: BoxDecoration(
                            color: Colors.white10,
                            borderRadius: BorderRadius.circular(12.w),
                            border: Border.all(
                              color:
                                  mikeSize == 10
                                      ? SocialChatTheme.primaryLight
                                      : Colors.transparent,
                              width: 2.w,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Column(
                                spacing: 5.w,
                                children: [
                                  SizedBox(height: 5.w),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                    color: Colors.transparent,
                                  ),
                                  text(
                                    textColor: Colors.white,
                                    SCAppLocalizations.of(
                                      context,
                                    )!.classicMic("10"),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 12.sp,
                                  ),
                                  SizedBox(height: 5.w),
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          mikeSize = 10;
                          setState(() {});
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 15.w),
          Row(
            spacing: 8.w,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.w),
                    image: DecorationImage(
                      image: AssetImage("sc_images/room/sc_icon_seat_num_bg.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                  child: Stack(
                    alignment: AlignmentDirectional.center,
                    children: [
                      GestureDetector(
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 12.w),
                          decoration: BoxDecoration(
                            color: Colors.white10,
                            borderRadius: BorderRadius.circular(12.w),
                            border: Border.all(
                              color:
                                  mikeSize == 15
                                      ? SocialChatTheme.primaryLight
                                      : Colors.transparent,
                              width: 2.w,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Column(
                                spacing: 5.w,
                                children: [
                                  SizedBox(height: 5.w),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                    color: Colors.transparent,
                                  ),
                                  text(
                                    textColor: Colors.white,
                                    SCAppLocalizations.of(
                                      context,
                                    )!.classicMic("15"),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 12.sp,
                                  ),
                                  SizedBox(height: 5.w),
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          mikeSize = 15;
                          setState(() {});
                        },
                      ),
                    ],
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.w),
                    image: DecorationImage(
                      image: AssetImage("sc_images/room/sc_icon_seat_num_bg.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                  child: Stack(
                    alignment: AlignmentDirectional.center,
                    children: [
                      GestureDetector(
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 12.w),
                          decoration: BoxDecoration(
                            color: Colors.white10,
                            borderRadius: BorderRadius.circular(12.w),
                            border: Border.all(
                              color:
                                  mikeSize == 20
                                      ? SocialChatTheme.primaryLight
                                      : Colors.transparent,
                              width: 2.w,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Column(
                                spacing: 5.w,
                                children: [
                                  SizedBox(height: 5.w),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  Image.asset(
                                    "sc_images/room/sc_icon_room_mic_model_5.png",
                                    height: 25.w,
                                  ),
                                  text(
                                    textColor: Colors.white,
                                    SCAppLocalizations.of(
                                      context,
                                    )!.classicMic("20"),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 12.sp,
                                  ),
                                  SizedBox(height: 5.w),
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          mikeSize = 20;
                          setState(() {});
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          Spacer(),
          GestureDetector(
            child: Container(
              alignment: AlignmentDirectional.center,
              margin: EdgeInsets.only(bottom: 20.w),
              height: 45.w,
              width: 180.w,
              decoration: BoxDecoration(
              color: SocialChatTheme.primaryLight,
                borderRadius: BorderRadius.circular(25.w),
              ),
              child: text(
                SCAppLocalizations.of(context)!.save,
                textColor: Colors.white,
                fontSize: 16.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
            onTap: () {
              SmartDialog.show(
                tag: "showConfirmDialog",
                alignment: Alignment.center,
                debounce: true,
                animationType: SmartAnimationType.fade,
                builder: (_) {
                  return MsgDialog(
                    title: SCAppLocalizations.of(context)!.tips,
                    msg:
                        SCAppLocalizations.of(context)!.confirmSwitchMicModelTips,
                    btnText: SCAppLocalizations.of(context)!.confirm,
                    onEnsure: () {
                      SCLoadingManager.show();
                      JoinRoomRes? room =
                          Provider.of<RtcProvider>(
                            context,
                            listen: false,
                          ).currenRoom;
                      if (room != null) {
                        SCChatRoomRepository()
                            .updateRoomSetting(
                              room.roomProfile?.roomProfile?.id ?? "",
                              room.roomProfile?.roomProfile?.roomAccount ?? "",
                              context,
                              mikeSize: mikeSize,
                            )
                            .then((value) {
                              var roomSetting = room.roomProfile?.roomSetting;
                              Provider.of<RtcProvider>(
                                context,
                                listen: false,
                              ).updateRoomSetting(
                                roomSetting!.copyWith(mikeSize: mikeSize),
                              );
                              SCTts.show(
                                SCAppLocalizations.of(
                                  context,
                                )!.operationSuccessful,
                              );
                              SCLoadingManager.hide();
                              SmartDialog.dismiss(tag: "showRoomMicSwitch");
                            })
                            .catchError((e) {
                              SCLoadingManager.hide();
                            });
                      }
                    },
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }
}
