import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.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/services/audio/rtc_manager.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/tools/sc_lk_dialog_util.dart';
import 'package:yumi/modules/room/manager/room_member_page.dart';
import 'package:yumi/modules/room/voice_room_route.dart';
import '../../../shared/data_sources/models/enum/sc_room_roles_type.dart';
import '../../../shared/data_sources/sources/local/user_manager.dart';
import '../../../shared/data_sources/sources/repositories/sc_room_repository_imp.dart';
import '../../../ui_kit/components/sc_compontent.dart';
import '../../../ui_kit/components/sc_debounce_widget.dart';
import '../../../ui_kit/components/sc_tts.dart';
import '../../../ui_kit/components/dialog/dialog_base.dart';
import '../../../ui_kit/components/text/sc_text.dart';
import '../../../main.dart';
import '../../index/main_route.dart';

///房间详情
class RoomDetailPage extends StatefulWidget {
  ///是否是房主
  final bool isHomeowner;

  const RoomDetailPage(this.isHomeowner, {super.key});

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

class _RoomDetailPageState extends State<RoomDetailPage> {
  @override
  void initState() {
    super.initState();
    Provider.of<RtcProvider>(context, listen: false).loadRoomInfo(
      Provider.of<RtcProvider>(
            context,
            listen: false,
          ).currenRoom?.roomProfile?.roomProfile?.id ??
          "",
    );
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Consumer<RtcProvider>(
        builder: (context, ref, child) {
          return 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(
                height: widget.isHomeowner ? 300.w : 400.w,
                decoration: BoxDecoration(
                  color: Color(0xff09372E).withOpacity(0.5),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(12.w),
                    topRight: Radius.circular(12.w),
                  ),
                ),
                child: Column(
                  children: [
                    SizedBox(height: 15.w),
                    text(
                      widget.isHomeowner
                          ? SCAppLocalizations.of(context)!.roomSetting
                          : SCAppLocalizations.of(context)!.roomDetails,
                      fontSize: 14.sp,
                      textColor: Colors.white,
                      fontWeight: FontWeight.w600,
                    ),
                    Expanded(
                      child: SingleChildScrollView(
                        child: Column(
                          spacing: 10.w,
                          children: [
                            SizedBox(height: 15.w),
                            GestureDetector(
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Color(0xffF2F2F2),
                                  borderRadius: BorderRadius.circular(10.w),
                                ),
                                margin: EdgeInsets.symmetric(horizontal: 15.w),
                                padding: EdgeInsets.all(15.w),
                                child: Row(
                                  children: [
                                    netImage(
                                      url: resolveRoomCoverUrl(
                                        ref
                                            .currenRoom
                                            ?.roomProfile
                                            ?.roomProfile
                                            ?.id,
                                        ref
                                            .currenRoom
                                            ?.roomProfile
                                            ?.roomProfile
                                            ?.roomCover,
                                      ),
                                      defaultImg: kRoomCoverDefaultImg,
                                      width: 48.w,
                                      height: 48.w,
                                      borderRadius: BorderRadius.all(
                                        Radius.circular(8.w),
                                      ),
                                    ),
                                    SizedBox(width: 8.w),
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Container(
                                              constraints: BoxConstraints(
                                                maxWidth:
                                                    ScreenUtil().screenWidth *
                                                    0.6,
                                              ),
                                              child: text(
                                                textColor: Colors.black,
                                                ref
                                                        .currenRoom
                                                        ?.roomProfile
                                                        ?.roomProfile
                                                        ?.roomName ??
                                                    "",
                                                fontSize: 14.sp,
                                              ),
                                            ),
                                            SizedBox(width: 3.w),
                                            widget.isHomeowner
                                                ? Image.asset(
                                                  "sc_images/room/sc_icon_room_edit.png",
                                                  width: 13.w,
                                                  height: 13.w,
                                                )
                                                : Container(),
                                          ],
                                        ),
                                        SizedBox(height: 5.w),
                                        Row(
                                          children: [
                                            text(
                                              "ID:${ref.currenRoom?.roomProfile?.userProfile?.getID()}",
                                              fontSize: 12.sp,
                                              textColor: Colors.black,
                                            ),
                                            SizedBox(width: 3.w),
                                            GestureDetector(
                                              child: Container(
                                                padding: EdgeInsets.all(3.w),
                                                child: Image.asset(
                                                  "sc_images/room/sc_icon_user_card_copy_id.png",
                                                  width: 11.w,
                                                  height: 11.w,
                                                  color: Colors.black,
                                                ),
                                              ),
                                              onTap: () {
                                                Clipboard.setData(
                                                  ClipboardData(
                                                    text:
                                                        ref
                                                            .currenRoom
                                                            ?.roomProfile
                                                            ?.roomProfile
                                                            ?.roomAccount ??
                                                        "",
                                                  ),
                                                );
                                                SCTts.show(
                                                  SCAppLocalizations.of(
                                                    context,
                                                  )!.copiedToClipboard,
                                                );
                                              },
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                              onTap: () {
                                if (!widget.isHomeowner) {
                                  return;
                                }
                                SCNavigatorUtils.push(
                                  context,
                                  "${VoiceRoomRoute.roomEdit}?need=false",
                                  replace: false,
                                );
                              },
                            ),
                            if (!widget.isHomeowner)
                              SCDebounceWidget(
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Color(0xffF2F2F2),
                                    borderRadius: BorderRadius.circular(10.w),
                                  ),
                                  margin: EdgeInsets.symmetric(
                                    horizontal: 15.w,
                                  ),
                                  padding: EdgeInsets.all(15.w),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      text(
                                        "${SCAppLocalizations.of(context)!.roomOwner}:",
                                        fontSize: 15.sp,
                                        textColor: Colors.black,
                                      ),
                                      SizedBox(height: 3.w),
                                      Row(
                                        children: [
                                          netImage(
                                            url:
                                                ref
                                                    .currenRoom
                                                    ?.roomProfile
                                                    ?.userProfile
                                                    ?.userAvatar ??
                                                "",
                                            shape: BoxShape.circle,
                                            width: 40.w,
                                            height: 40.w,
                                          ),
                                          SizedBox(width: 10.w),
                                          Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              socialchatNickNameText(
                                                maxWidth: 135.w,
                                                textColor: Colors.black,
                                                ref
                                                        .currenRoom
                                                        ?.roomProfile
                                                        ?.userProfile
                                                        ?.userNickname ??
                                                    "",
                                                fontSize: 14.sp,
                                                fontWeight: FontWeight.w600,
                                                type:
                                                    ref
                                                        .currenRoom
                                                        ?.roomProfile
                                                        ?.userProfile
                                                        ?.getVIP()
                                                        ?.name ??
                                                    "",
                                                needScroll:
                                                    (ref
                                                            .currenRoom
                                                            ?.roomProfile
                                                            ?.userProfile
                                                            ?.userNickname
                                                            ?.characters
                                                            .length ??
                                                        0) >
                                                    13,
                                              ),
                                              Row(
                                                children: [
                                                  text(
                                                    "ID:${ref.currenRoom?.roomProfile?.userProfile?.account}",
                                                    textColor: Colors.black,
                                                    fontSize: 14.sp,
                                                  ),
                                                  SizedBox(width: 3.w),
                                                  GestureDetector(
                                                    child: Container(
                                                      padding: EdgeInsets.all(
                                                        3.w,
                                                      ),
                                                      child: Image.asset(
                                                        "sc_images/room/sc_icon_user_card_copy_id.png",
                                                        width: 11.w,
                                                        height: 11.w,
                                                        color: Colors.black,
                                                      ),
                                                    ),
                                                    onTap: () {
                                                      Clipboard.setData(
                                                        ClipboardData(
                                                          text:
                                                              ref
                                                                  .currenRoom
                                                                  ?.roomProfile
                                                                  ?.userProfile
                                                                  ?.account ??
                                                              "",
                                                        ),
                                                      );
                                                      SCTts.show(
                                                        SCAppLocalizations.of(
                                                          context,
                                                        )!.copiedToClipboard,
                                                      );
                                                    },
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: () {
                                  SCNavigatorUtils.push(
                                    context,
                                    replace: false,
                                    "${SCMainRoute.person}?isMe=${AccountStorage().getCurrentUser()?.userProfile?.id == ref.currenRoom?.roomProfile?.userProfile?.id}&tageId=${ref.currenRoom?.roomProfile?.userProfile?.id}",
                                  );
                                },
                              ),
                            SCDebounceWidget(
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Color(0xffF2F2F2),
                                  borderRadius: BorderRadius.circular(10.w),
                                ),
                                margin: EdgeInsets.symmetric(horizontal: 15.w),
                                padding: EdgeInsets.all(15.w),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [
                                        text(
                                          textColor: Colors.black,
                                          fontSize: 14.sp,
                                          SCAppLocalizations.of(
                                            context,
                                          )!.roomAnnouncement,
                                        ),
                                        Spacer(),
                                        widget.isHomeowner
                                            ? Icon(
                                              Icons.keyboard_arrow_right_sharp,
                                              color: Colors.black,
                                              size: 15.w,
                                            )
                                            : Container(),
                                      ],
                                    ),
                                    Row(
                                      children: [
                                        Consumer<RtcProvider>(
                                          builder: (context, ref, child) {
                                            return Expanded(
                                              child: text(
                                                ref
                                                        .currenRoom
                                                        ?.roomProfile
                                                        ?.roomProfile
                                                        ?.roomDesc ??
                                                    "",
                                                fontSize: 13.sp,
                                                maxLines: 2,
                                                textColor: Color(0xffB1B1B1),
                                              ),
                                            );
                                          },
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                              onTap: () {
                                if (!widget.isHomeowner) {
                                  return;
                                }
                                SCNavigatorUtils.push(
                                  context,
                                  "${VoiceRoomRoute.roomEdit}?need=false",
                                  replace: false,
                                );
                              },
                            ),
                            widget.isHomeowner
                                ? Row(
                                  children: [
                                    SizedBox(width: 15.w),
                                    Expanded(
                                      child: SCDebounceWidget(
                                        child: Container(
                                          height: 45.w,
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(
                                              10.w,
                                            ),
                                            color: SocialChatTheme.primaryLight,
                                          ),
                                          child: Row(
                                            children: [
                                              SizedBox(width: 20.w),
                                              text(
                                                SCAppLocalizations.of(
                                                  context,
                                                )!.roomMember,
                                                textColor: Colors.white,
                                                fontSize: 14.sp,
                                              ),
                                              Spacer(),
                                              Icon(
                                                Icons
                                                    .keyboard_arrow_right_sharp,
                                                color: Colors.white,
                                                size: 20.w,
                                              ),
                                              SizedBox(width: 15.w),
                                            ],
                                          ),
                                        ),
                                        onTap: () {
                                          Navigator.of(context).pop();
                                          showBottomInBottomDialog(
                                            context,
                                            RoomMemberPage(
                                              roomId:
                                                  Provider.of<RtcProvider>(
                                                        context!,
                                                        listen: false,
                                                      )
                                                      .currenRoom
                                                      ?.roomProfile
                                                      ?.roomProfile
                                                      ?.id ??
                                                  "",
                                              isHomeowner: widget.isHomeowner,
                                            ),
                                          );
                                        },
                                      ),
                                    ),
                                    SizedBox(width: 10.w),
                                    Expanded(
                                      child: SCDebounceWidget(
                                        child: Container(
                                          height: 45.w,
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(
                                              10.w,
                                            ),
                                            color: SocialChatTheme.primaryLight,
                                          ),
                                          child: Row(
                                            children: [
                                              SizedBox(width: 20.w),
                                              text(
                                                SCAppLocalizations.of(
                                                  context,
                                                )!.roomEdit,
                                                textColor: Colors.white,
                                                fontSize: 14.sp,
                                              ),
                                              Spacer(),
                                              Icon(
                                                Icons
                                                    .keyboard_arrow_right_sharp,
                                                color: Colors.white,
                                                size: 20.w,
                                              ),
                                              SizedBox(width: 15.w),
                                            ],
                                          ),
                                        ),
                                        onTap: () {
                                          SCNavigatorUtils.push(
                                            context,
                                            "${VoiceRoomRoute.roomEdit}?need=false",
                                            replace: false,
                                          );
                                        },
                                      ),
                                    ),
                                    SizedBox(width: 15.w),
                                  ],
                                )
                                : Container(),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              spacing: 15.w,
                              children: [
                                ref.isTourists()
                                    ? SCDebounceWidget(
                                      child: Container(
                                        width: 160.w,
                                        decoration: BoxDecoration(
                                          color: SocialChatTheme.primaryLight,
                                          borderRadius: BorderRadius.circular(
                                            35.w,
                                          ),
                                        ),
                                        height: 42.w,
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Image.asset(
                                              "sc_images/room/sc_icon_join_room_member.png",
                                              height: 20.w,
                                              width: 20.w,
                                            ),
                                            SizedBox(width: 8.w),
                                            text(
                                              ref.isTourists()
                                                  ? SCAppLocalizations.of(
                                                    context,
                                                  )!.join
                                                  : SCAppLocalizations.of(
                                                    context,
                                                  )!.giveUpIdentity,
                                              textColor: Colors.white,
                                              fontSize: 15.sp,
                                              fontWeight: FontWeight.bold,
                                            ),
                                            SizedBox(width: 3.w),
                                            text(
                                              "(${ref.currenRoom?.roomProfile?.roomSetting?.joinGolds ?? 0})",
                                              textColor: Colors.white,
                                              fontSize: 15.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:
                                                  ref.isTourists()
                                                      ? SCAppLocalizations.of(
                                                        context,
                                                      )!.joinMemberTips2
                                                      : SCAppLocalizations.of(
                                                        context,
                                                      )!.leaveRoomIdentityTips,
                                              btnText:
                                                  SCAppLocalizations.of(
                                                    context,
                                                  )!.confirm,
                                              onEnsure: () {
                                                SCChatRoomRepository()
                                                    .changeRoomRole(
                                                      ref
                                                              .currenRoom
                                                              ?.roomProfile
                                                              ?.roomProfile
                                                              ?.id ??
                                                          "",
                                                      ref.isTourists()
                                                          ? SCRoomRolesType
                                                              .ADMIN
                                                              .name
                                                          : SCRoomRolesType
                                                              .TOURIST
                                                              .name,
                                                      UserManager()
                                                              .getCurrentUser()
                                                              ?.userProfile
                                                              ?.id ??
                                                          "",
                                                      UserManager()
                                                              .getCurrentUser()
                                                              ?.userProfile
                                                              ?.id ??
                                                          "",
                                                      "ONESELF_JOIN",
                                                    )
                                                    .then((reslt) {
                                                      ref.currenRoom = ref
                                                          .currenRoom
                                                          ?.copyWith(
                                                            entrants: ref
                                                                .currenRoom
                                                                ?.entrants
                                                                ?.copyWith(
                                                                  roles:
                                                                      SCRoomRolesType
                                                                          .MEMBER
                                                                          .name,
                                                                ),
                                                          );
                                                      setState(() {});
                                                      SCTts.show(
                                                        SCAppLocalizations.of(
                                                          navigatorKey
                                                              .currentState!
                                                              .context,
                                                        )!.operationSuccessful,
                                                      );
                                                    });
                                              },
                                            );
                                          },
                                        );
                                      },
                                    )
                                    : SizedBox.shrink(),
                                ref
                                            .currenRoom
                                            ?.roomProfile
                                            ?.roomProfile
                                            ?.userId ==
                                        UserManager()
                                            .getCurrentUser()
                                            ?.userProfile
                                            ?.id
                                    ? SizedBox.shrink()
                                    : Selector<RtcProvider, bool>(
                                      selector:
                                          (c, p) =>
                                              p.isFollowRoomRes?.followRoom ??
                                              false,
                                      shouldRebuild:
                                          (prev, next) => prev != next,
                                      builder: (_, follow, __) {
                                        return SCDebounceWidget(
                                          child: Container(
                                            width: 160.w,
                                            decoration: BoxDecoration(
                                              color:
                                                  SocialChatTheme.primaryLight,
                                              borderRadius:
                                                  BorderRadius.circular(35.w),
                                            ),
                                            height: 42.w,
                                            child: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment.center,
                                              children: [
                                                Row(
                                                  children: [
                                                    Image.asset(
                                                      !follow
                                                          ? "sc_images/room/sc_icon_follow_room_un.png"
                                                          : "sc_images/room/sc_icon_follow_room_en.png",
                                                      width: 20.w,
                                                      height: 20.w,
                                                    ),
                                                    SizedBox(width: 8.w),
                                                    text(
                                                      !follow
                                                          ? SCAppLocalizations.of(
                                                            context,
                                                          )!.follow
                                                          : SCAppLocalizations.of(
                                                            context,
                                                          )!.following,
                                                      textColor: Colors.white,
                                                      fontSize: 15.sp,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ],
                                                ),
                                              ],
                                            ),
                                          ),
                                          onTap: () {
                                            if (follow) {
                                              SmartDialog.show(
                                                tag: "unFollowDialog",
                                                alignment: Alignment.center,
                                                animationType:
                                                    SmartAnimationType.fade,
                                                builder: (_) {
                                                  return Container(
                                                    width:
                                                        ScreenUtil()
                                                            .screenWidth *
                                                        0.75,
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                          horizontal: 10.w,
                                                        ),
                                                    decoration: BoxDecoration(
                                                      color: Colors.white,
                                                      borderRadius:
                                                          BorderRadius.all(
                                                            Radius.circular(
                                                              12.w,
                                                            ),
                                                          ),
                                                    ),
                                                    child: Column(
                                                      mainAxisSize:
                                                          MainAxisSize.min,
                                                      crossAxisAlignment:
                                                          CrossAxisAlignment
                                                              .center,
                                                      children: [
                                                        SizedBox(height: 15.w),
                                                        text(
                                                          SCAppLocalizations.of(
                                                            context,
                                                          )!.unFollow,
                                                          fontSize: 16.sp,
                                                          textColor:
                                                              Colors.black,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                        SizedBox(height: 15.w),
                                                        text(
                                                          SCAppLocalizations.of(
                                                            context,
                                                          )!.sureUnfollowThisRoom,
                                                          fontWeight:
                                                              FontWeight.w600,
                                                          textColor:
                                                              Colors.grey,
                                                          fontSize: 14.sp,
                                                        ),
                                                        SizedBox(height: 15.w),
                                                        Row(
                                                          mainAxisSize:
                                                              MainAxisSize.min,
                                                          crossAxisAlignment:
                                                              CrossAxisAlignment
                                                                  .center,
                                                          children: [
                                                            GestureDetector(
                                                              behavior:
                                                                  HitTestBehavior
                                                                      .opaque,
                                                              onTap: () {
                                                                SmartDialog.dismiss(
                                                                  tag:
                                                                      "unFollowDialog",
                                                                );
                                                              },
                                                              child: Container(
                                                                alignment:
                                                                    AlignmentDirectional
                                                                        .center,
                                                                decoration: BoxDecoration(
                                                                  borderRadius:
                                                                      BorderRadius.circular(
                                                                        35.w,
                                                                      ),
                                                                  color: Color(
                                                                    0xffF2F2F2,
                                                                  ),
                                                                ),
                                                                height: 35.w,
                                                                width: 95.w,
                                                                child: text(
                                                                  SCAppLocalizations.of(
                                                                    context,
                                                                  )!.cancel,
                                                                  textColor:
                                                                      Colors
                                                                          .grey,
                                                                  fontSize:
                                                                      15.sp,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w600,
                                                                ),
                                                              ),
                                                            ),
                                                            SizedBox(
                                                              width: 18.w,
                                                            ),
                                                            GestureDetector(
                                                              onTap: () {
                                                                ref.followCurrentVoiceRoom();
                                                              },
                                                              behavior:
                                                                  HitTestBehavior
                                                                      .opaque,
                                                              child: Container(
                                                                alignment:
                                                                    AlignmentDirectional
                                                                        .center,
                                                                decoration: BoxDecoration(
                                                                  color:
                                                                      SocialChatTheme
                                                                          .primaryLight,
                                                                  borderRadius:
                                                                      BorderRadius.circular(
                                                                        35.w,
                                                                      ),
                                                                ),
                                                                height: 35.w,
                                                                width: 95.w,
                                                                child: text(
                                                                  SCAppLocalizations.of(
                                                                    context,
                                                                  )!.unFollow,
                                                                  textColor:
                                                                      Colors
                                                                          .white,
                                                                  fontSize:
                                                                      15.sp,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w600,
                                                                ),
                                                              ),
                                                            ),
                                                          ],
                                                        ),
                                                        SizedBox(height: 25.w),
                                                      ],
                                                    ),
                                                  );
                                                },
                                              );
                                            } else {
                                              ref.followCurrentVoiceRoom();
                                            }
                                          },
                                        );
                                      },
                                    ),
                              ],
                            ),
                            SizedBox(height: 10.w),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
