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/ui_kit/components/sc_debounce_widget.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:yumi/ui_kit/components/dialog/dialog_base.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/tools/sc_pick_utils.dart';
import 'package:yumi/services/room/rc_room_manager.dart';
import 'package:yumi/services/audio/rtm_manager.dart';
import '../../../shared/tools/sc_lk_dialog_util.dart';
import '../../../shared/data_sources/models/enum/sc_room_info_event_type.dart';
import '../../../shared/business_logic/usecases/sc_accurate_length_limiting_textInput_formatter.dart';
import '../../../ui_kit/widgets/room/switch_model/room_mic_switch_page.dart';
import '../block/blocked_list_page.dart';
import '../voice_room_route.dart';

///编辑房间信息
class RoomEditPage extends StatefulWidget {
  String needRestCurrentRoomInfo = "false";

  RoomEditPage({super.key, this.needRestCurrentRoomInfo = "false"});

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

class _RoomEditPageState extends State<RoomEditPage> {
  RtcProvider? rtcProvider;
  bool isEdit = false;
  final TextEditingController _roomNameController = TextEditingController();
  final TextEditingController _roomAnnouncementController =
      TextEditingController();
  final int _roomNameMaxLength = 24;
  int _roomNameCurrentLength = 0;
  String roomCover = "";
  final int _roomAnnouncementMaxLength = 100;
  int _roomAnnouncementCurrentLength = 0;

  @override
  void initState() {
    super.initState();
    rtcProvider = Provider.of<RtcProvider>(context, listen: false);
    _roomNameController.addListener(() {
      setState(() {
        _roomNameCurrentLength = _getActualCharacterCount(
          _roomNameController.text,
        );
      });
    });
    _roomAnnouncementController.addListener(() {
      setState(() {
        _roomAnnouncementCurrentLength = _getActualCharacterCount(
          _roomAnnouncementController.text,
        );
      });
    });
    _roomNameController.text =
        rtcProvider?.currenRoom?.roomProfile?.roomProfile?.roomName ?? "";
    _roomNameCurrentLength = _getActualCharacterCount(_roomNameController.text);
    _roomAnnouncementController.text =
        rtcProvider?.currenRoom?.roomProfile?.roomProfile?.roomDesc ?? "";
    _roomAnnouncementCurrentLength = _getActualCharacterCount(
      _roomAnnouncementController.text,
    );
    roomCover =
        rtcProvider?.currenRoom?.roomProfile?.roomProfile?.roomCover ?? "";
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        if (!isEdit) {
          SCNavigatorUtils.goBack(context);
          return Future.value(false);
        }
        SmartDialog.show(
          tag: "showConfirmDialog",
          alignment: Alignment.center,
          debounce: true,
          animationType: SmartAnimationType.fade,
          builder: (_) {
            return MsgDialog(
              title: SCAppLocalizations.of(context)!.tips,
              msg: SCAppLocalizations.of(context)!.theModificationsMade,
              onEnsure: () {
                SCNavigatorUtils.goBack(context);
              },
            );
          },
        );
        return Future.value(false);
      },
      child: Stack(
        children: [
          Image.asset(
            "sc_images/person/sc_icon_edit_userinfo_bg.png",
            width: ScreenUtil().screenWidth,
            height: ScreenUtil().screenHeight,
            fit: BoxFit.fill,
          ),
          Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.transparent,
            appBar: SocialChatStandardAppBar(
              title: SCAppLocalizations.of(context)!.roomSetting,
              onTag: () {
                if (widget.needRestCurrentRoomInfo == "true") {
                  Provider.of<RtcProvider>(context, listen: false).currenRoom =
                      null;
                }
                Navigator.pop(context);
              },
              actions: [
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 5.w),
                    margin: EdgeInsetsDirectional.only(end: 8.w),
                    child: text(
                      SCAppLocalizations.of(context)!.save,
                      textColor: Colors.white,
                      fontSize: 15.sp,
                    ),
                  ),
                  onTap: () {
                    submit(context);
                  },
                ),
              ],
            ),
            body: SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(height: 35.w),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      SizedBox(width: 20.w,),
                      GestureDetector(
                        child: Stack(
                          alignment: Alignment.bottomRight,
                          children: [
                            netImage(
                              url: roomCover,
                              borderRadius: BorderRadius.all(
                                Radius.circular(12),
                              ),
                              width: 90.w,
                              height: 90.w,
                            ),
                            Icon(Icons.camera_alt, size: 25.w,color: Colors.white,),
                          ],
                        ),
                        onTap: () {
                          SCPickUtils.pickImage(context, (
                            bool success,
                            String url,
                          ) {
                            if (success) {
                              setState(() {
                                roomCover = url;
                              });
                            }
                          });
                        },
                      ),
                    ],
                  ),
                  SizedBox(height: 25.w),
                  Row(
                    children: [
                      SizedBox(width: 25.w),
                      text(
                        SCAppLocalizations.of(context)!.roomName,
                        textColor: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 15.sp,
                      ),
                    ],
                  ),
                  SizedBox(height: 10.w),
                  Row(
                    children: [
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 25.w),
                          padding: EdgeInsets.only(
                            left: width(12),
                            right: width(12),
                          ),
                          alignment: AlignmentDirectional.centerStart,
                          height: 45.w,
                          width: ScreenUtil().screenWidth,
                          decoration: BoxDecoration(
                            border: Border.all(color: Color(0xff5FFFB7), width: 0.5),
                            gradient: LinearGradient(colors: [Color(0xff5FFFB7).withOpacity(0.1),Color(0xff5FFFB7).withOpacity(0.6)]),
                            borderRadius: BorderRadius.all(
                              Radius.circular(height(8)),
                            ),
                          ),
                          child: TextField(
                            controller: _roomNameController,
                            onChanged: (text) {
                              isEdit = true;
                            },
                            inputFormatters: [
                              SCAccurateLengthLimitingTextInputFormatter(
                                _roomNameMaxLength,
                              ),
                            ],
                            decoration: InputDecoration(
                              hintText:
                                  SCAppLocalizations.of(context)!.enterRoomName,
                              hintStyle: TextStyle(
                                color: Colors.white54,
                                fontSize: 14.sp,
                              ),
                              contentPadding: EdgeInsets.only(top: 0.w),
                              counterText: '',
                              isDense: true,
                              filled: false,
                              focusColor: Colors.transparent,
                              hoverColor: Colors.transparent,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              disabledBorder: InputBorder.none,
                              errorBorder: InputBorder.none,
                              focusedErrorBorder: InputBorder.none,
                              // enabledBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // focusedBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // prefixIcon: Padding(padding: EdgeInsets.all(8.w), child: Image.asset("images/login/sc_icon_phone.png",width: 20.w, height: 20.w,fit: BoxFit.fill,),),
                              fillColor: Colors.white54,
                              suffix: Row(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  SizedBox(width: 3.w),
                                  text(
                                    '$_roomNameCurrentLength/$_roomNameMaxLength',
                                    textColor: Colors.white54,
                                  ),
                                ],
                              ),
                            ),
                            style: TextStyle(
                              fontSize: sp(15),
                              color: Colors.white,
                              textBaseline: TextBaseline.alphabetic,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 15.w),
                  Row(
                    children: [
                      SizedBox(width: 25.w),
                      text(
                        SCAppLocalizations.of(context)!.roomAnnouncement,
                        textColor: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 15.sp,
                      ),
                    ],
                  ),
                  SizedBox(height: 10.w),
                  Row(
                    children: [
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 25.w),
                          padding: EdgeInsets.only(
                            top: 5.w,
                            left: width(12),
                            right: width(12),
                          ),
                          alignment: AlignmentDirectional.centerStart,
                          height: 110.w,
                          width: ScreenUtil().screenWidth,
                          decoration: BoxDecoration(
                            border: Border.all(color: Color(0xff5FFFB7), width: 0.5),
                            gradient: LinearGradient(colors: [Color(0xff5FFFB7).withOpacity(0.1),Color(0xff5FFFB7).withOpacity(0.6)]),
                            borderRadius: BorderRadius.all(
                              Radius.circular(height(8)),
                            ),
                          ),
                          child: TextField(
                            controller: _roomAnnouncementController,
                            onChanged: (text) {
                              isEdit = true;
                            },
                            inputFormatters: [
                              SCAccurateLengthLimitingTextInputFormatter(
                                _roomAnnouncementMaxLength,
                              ),
                            ],
                            maxLines: 5,
                            decoration: InputDecoration(
                              hintText: "",
                              hintStyle: TextStyle(
                                color: Colors.white54,
                                fontSize: 14.sp,
                              ),
                              contentPadding: EdgeInsets.only(top: 0.w),
                              counterText: '',
                              isDense: true,
                              filled: false,
                              focusColor: Colors.transparent,
                              hoverColor: Colors.transparent,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              disabledBorder: InputBorder.none,
                              errorBorder: InputBorder.none,
                              focusedErrorBorder: InputBorder.none,
                              // enabledBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // focusedBorder: UnderlineInputBorder(
                              //   borderSide: BorderSide(width: 0.5,color: Colors.white,style: BorderStyle.solid),),
                              // prefixIcon: Padding(padding: EdgeInsets.all(8.w), child: Image.asset("images/login/sc_icon_phone.png",width: 20.w, height: 20.w,fit: BoxFit.fill,),),
                              fillColor: Colors.white54,
                              suffix: Row(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  SizedBox(width: 3.w),
                                  text(
                                    '$_roomAnnouncementCurrentLength/$_roomAnnouncementMaxLength',
                                    textColor: Colors.white54,
                                  ),
                                ],
                              ),
                            ),
                            style: TextStyle(
                              fontSize: sp(15),
                              color: Colors.white,
                              textBaseline: TextBaseline.alphabetic,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  widget.needRestCurrentRoomInfo != "true"
                      ? Column(
                        children: [
                          SizedBox(height: 12.w),
                          SCDebounceWidget(
                            child: Row(
                              children: [
                                SizedBox(width: 25.w),
                                text(
                                  SCAppLocalizations.of(context)!.roomTheme2,
                                  textColor: Colors.white,
                                  fontWeight: FontWeight.w500,
                                  fontSize: 15.sp,
                                ),
                                Spacer(),
                                Icon(
                                  Icons.keyboard_arrow_right,
                                  color: Colors.white,
                                  size: 20.w,
                                ),
                                SizedBox(width: 25.w),
                              ],
                            ),
                            onTap: () {
                              SCNavigatorUtils.push(
                                context,
                                VoiceRoomRoute.roomTheme,
                                replace: false,
                              );
                            },
                          ),
                          SizedBox(height: 12.w),
                          SCDebounceWidget(
                            child: Row(
                              children: [
                                SizedBox(width: 25.w),
                                text(
                                  SCAppLocalizations.of(context)!.micManagement,
                                  textColor: Colors.white,
                                  fontWeight: FontWeight.w500,
                                  fontSize: 15.sp,
                                ),
                                Spacer(),
                                Icon(
                                  Icons.keyboard_arrow_right,
                                  color: Colors.white,
                                  size: 20.w,
                                ),
                                SizedBox(width: 25.w),
                              ],
                            ),
                            onTap: () {
                              SmartDialog.show(
                                tag: "showRoomMicSwitch",
                                alignment: Alignment.bottomCenter,
                                animationType: SmartAnimationType.fade,
                                debounce: true,
                                builder: (_) {
                                  return RoomMicSwitchPage();
                                },
                              );
                            },
                          ),
                          SizedBox(height: 12.w),
                          SCDebounceWidget(
                            child: Row(
                              children: [
                                SizedBox(width: 25.w),
                                text(
                                  SCAppLocalizations.of(context)!.blockedList2,
                                  textColor: Colors.white,
                                  fontWeight: FontWeight.w500,
                                  fontSize: 15.sp,
                                ),
                                Spacer(),
                                Icon(
                                  Icons.keyboard_arrow_right,
                                  color: Colors.white,
                                  size: 20.w,
                                ),
                                SizedBox(width: 25.w),
                              ],
                            ),
                            onTap: () {
                              showBottomInBottomDialog(
                                context,
                                BlockedListPage(
                                  roomId:
                                      Provider.of<RtcProvider>(
                                            context!,
                                            listen: false,
                                          )
                                          .currenRoom
                                          ?.roomProfile
                                          ?.roomProfile
                                          ?.id ??
                                      "",
                                ),
                              );
                            },
                          ),
                          SizedBox(height: 15.w),
                        ],
                      )
                      : Container(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  int _getActualCharacterCount(String text) {
    return text.characters.length;
  }

  void submit(BuildContext context) async {
    if (_roomNameController.text.trim().isEmpty) {
      SCTts.show("Room name not empty!");
      return;
    }
    if (_roomAnnouncementController.text.trim().isEmpty) {
      SCTts.show("Room announcement not empty!");
      return;
    }
    SCLoadingManager.show(context: context);
    var roomInfo = await SCAccountRepository().editRoomInfo(
      rtcProvider?.currenRoom?.roomProfile?.roomProfile?.id ?? "",
      roomCover,
      _roomNameController.text,
      _roomAnnouncementController.text,
      SCRoomInfoEventType.AVAILABLE.name,
    );
    Provider.of<SocialChatRoomManager>(
      context,
      listen: false,
    ).updateMyRoomInfo(roomInfo);
    if (widget.needRestCurrentRoomInfo == "true") {
      ///需要创建群组
      var c = await Provider.of<RtmProvider>(
        context,
        listen: false,
      ).createRoomGroup(
        rtcProvider?.currenRoom?.roomProfile?.roomProfile?.roomAccount ?? "",
        rtcProvider?.currenRoom?.roomProfile?.roomProfile?.roomName ?? "",
      );
      if (c.code == 0) {
        SCLoadingManager.hide();
        SCNavigatorUtils.goBack(context);
        Provider.of<RtcProvider>(
          context,
          listen: false,
        ).joinVoiceRoomSession(context, roomInfo.id ?? "", clearRoomData: true);
      } else {
        SCLoadingManager.hide();
        SCTts.show("${c.code}");
      }
    } else {
      Provider.of<RtcProvider>(
        context,
        listen: false,
      ).loadRoomInfo(roomInfo.id ?? "");
      SCLoadingManager.hide();
      SCNavigatorUtils.goBack(context);
    }
  }

}
