import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_debounce_widget.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/business_logic/models/res/room_res.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/routes/sc_routes.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/shared/business_logic/models/res/sc_user_identity_res.dart';
import 'package:yumi/services/gift/gift_animation_manager.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:yumi/services/auth/user_profile_manager.dart';
import 'package:yumi/modules/index/main_route.dart';

import '../../components/sc_float_ichart.dart';

class ExitMinRoomPage extends StatefulWidget {
  bool isFz;
  String roomId;

  ExitMinRoomPage(this.isFz, this.roomId);

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

class _ExitMinRoomPageState extends State<ExitMinRoomPage> {
  SCUserIdentityRes? userIdentity;
  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    SCAccountRepository()
        .userIdentity(
          userId:
              Provider.of<RtcProvider>(
                context,
                listen: false,
              ).currenRoom?.roomProfile?.roomProfile?.userId,
        )
        .then((v) {
          userIdentity = v;
          isLoading = false;
          setState(() {});
        })
        .catchError((e) {
          isLoading = false;
          setState(() {});
        });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        SCDebounceWidget(
          child: Container(color: Colors.transparent),
          onTap: () {
            Navigator.of(context).pop();
          },
        ),
        Column(
          spacing: 55.w,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SCDebounceWidget(
              child: Column(
                children: [
                  Image.asset(
                    "sc_images/room/sc_icon_min_room.png",
                    width: 48.w,
                    height: 48.w,
                  ),
                  SizedBox(height: 8.w),
                  text(
                    SCAppLocalizations.of(context)!.mInimize,
                    fontSize: 15.sp,
                    textColor: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                ],
              ),
              onTap: () {
                SCFloatIchart().show();
                SCNavigatorUtils.popUntil(
                  context,
                  ModalRoute.withName(SCRoutes.home),
                );
                Provider.of<GiftAnimationManager>(
                  context,
                  listen: false,
                ).cleanupAnimationResources();
              },
            ),
            SCDebounceWidget(
              child: Column(
                children: [
                  Image.asset(
                    "sc_images/room/sc_icon_exit_room.png",
                    width: 48.w,
                    height: 48.w,
                  ),
                  SizedBox(height: 8.w),
                  text(
                    SCAppLocalizations.of(context)!.exit,
                    fontSize: 15.sp,
                    textColor: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                ],
              ),
              onTap: () {
                SCNavigatorUtils.goBack(context);
                Provider.of<RtcProvider>(
                  context,
                  listen: false,
                ).exitCurrentVoiceRoomSession(false);
              },
            ),
          ],
        ),
      ],
    );
  }
}
