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/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:provider/provider.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 RoomCharmPage extends StatefulWidget {
  @override
  _RoomCharmPageState createState() => _RoomCharmPageState();
}

class _RoomCharmPageState extends State<RoomCharmPage> {
  JoinRoomRes? room;

  @override
  void initState() {
    super.initState();
    room = Provider.of<RtcProvider>(context!, listen: false).currenRoom;
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: ClipRRect(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(12.w),
          topRight: Radius.circular(12.w),
        ),
        child: BackdropFilter(
          filter: ui.ImageFilter.blur(sigmaX: 15, sigmaY: 15),
          child: Container(
            height: 260.w,
            decoration: BoxDecoration(
              color: Colors.black54,
            ),
            child: Column(
              children: [
                SizedBox(height: 6.w),
                Row(
                  children: [
                    SizedBox(width: 27.w),
                    Spacer(),
                    text(
                      SCAppLocalizations.of(context)!.charm,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      textColor: Colors.white,
                    ),
                    Spacer(),
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      child: Container(
                        color: Colors.transparent,
                        padding: EdgeInsets.all(3.w),
                        child: Icon(
                          Icons.close,
                          size: 22.w,
                          color: Colors.white,
                        ),
                      ),
                      onTap: () {
                        SmartDialog.dismiss(tag: "showRoomCharm");
                      },
                    ),
                    SizedBox(width: 5.w),
                  ],
                ),
                SizedBox(height: 10.w),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(width: 15.w),
                    text(
                      SCAppLocalizations.of(context)!.gameRules,
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w600,
                      textColor: Colors.white,
                    ),
                  ],
                ),
                SizedBox(height: 10.w),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 15.w),
                  alignment: AlignmentDirectional.centerStart,
                  padding: EdgeInsets.symmetric(
                    horizontal: 10.w,
                    vertical: 10.w,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.white10,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: text(
                    SCAppLocalizations.of(context)!.charmGameRulesTips,
                    textColor: Colors.white54,
                    maxLines: 5,
                    fontWeight: FontWeight.w600,
                    fontSize: 14.sp,
                  ),
                ),
                SizedBox(height: 20.w),
                GestureDetector(
                  child: Container(
                    alignment: AlignmentDirectional.center,
                    width: 130.w,
                    height: 45.w,
                    decoration: BoxDecoration(
                      color:
                          (room?.roomProfile?.roomSetting?.showHeartbeat ??
                                  false)
                              ? Colors.white38
                              : SocialChatTheme.primaryLight,
                      borderRadius: BorderRadius.circular(30.w),
                    ),
                    child: text(
                      (room?.roomProfile?.roomSetting?.showHeartbeat ?? false)
                          ? SCAppLocalizations.of(context)!.stop
                          : SCAppLocalizations.of(context)!.start,
                      fontWeight: FontWeight.bold,
                      fontSize: 18.sp,
                    ),
                  ),
                  onTap: () {
                    SCChatRoomRepository()
                        .updateRoomSetting(
                          Provider.of<RtcProvider>(
                                context,
                                listen: false,
                              ).currenRoom?.roomProfile?.roomProfile?.id ??
                              "",
                          Provider.of<RtcProvider>(context, listen: false)
                                  .currenRoom
                                  ?.roomProfile
                                  ?.roomProfile
                                  ?.roomAccount ??
                              "",
                          context,
                          showHeartbeat:
                              !((room
                                      ?.roomProfile
                                      ?.roomSetting
                                      ?.showHeartbeat) ??
                                  false),
                        )
                        .whenComplete(() {
                          Provider.of<RtcProvider>(
                            context,
                            listen: false,
                          ).loadRoomInfo(
                            Provider.of<RtcProvider>(
                                  context,
                                  listen: false,
                                ).currenRoom?.roomProfile?.roomProfile?.id ??
                                "",
                          );
                          SmartDialog.dismiss(tag: "showRoomCharm");
                        });
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
