import 'package:app/pages/student/room/widgets/status_view.dart';
import 'package:app/providers/user_store.dart';
import 'package:app/request/dto/room/room_list_item_dto.dart';
import 'package:app/widgets/base/transition/slide_hide.dart';
import 'package:app/widgets/room/core/count_down_vm.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'controls/bottom_bar.dart';
import 'controls/top_bar.dart';
import 'video/student_video_list.dart';
import 'viewmodel/stu_room_vm.dart';

class SRoomPage extends StatefulWidget {
  final RoomListItemDto roomInfo;

  const SRoomPage({super.key, required this.roomInfo});

  @override
  State<SRoomPage> createState() => _SRoomPageState();
}

class _SRoomPageState extends State<SRoomPage> {
  /// 显示控制栏
  bool _controlsVisible = true;

  ///显示其他学生画面
  bool _showOtherStudent = true;

  ///切换显示控制栏
  void _toggleOverlay() {
    setState(() {
      _controlsVisible = !_controlsVisible;
    });
  }

  @override
  Widget build(BuildContext context) {
    UserStore userStore = context.read<UserStore>();
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<StuRoomVM>(
          create: (_) => StuRoomVM(
            info: widget.roomInfo,
            uid: userStore.userInfo!.id,
          ),
        ),
        ChangeNotifierProxyProvider<StuRoomVM, CountDownVM>(
          create: (_) => CountDownVM(),
          update: (_, stuVM, countDownVM) {
            countDownVM!.bind(stuVM.roomInfo);
            return countDownVM;
          },
        ),
      ],
      child: Scaffold(
        body: Stack(
          children: [
            GestureDetector(
              onTap: _toggleOverlay,
              child: Container(color: Color(0xff2c3032)),
            ),
            StatusView(),
            //其他学生
            Positioned(
              right: 0,
              top: 0,
              bottom: 0,
              child: IgnorePointer(
                child: Visibility(
                  visible: _showOtherStudent,
                  child: StudentVideoList(),
                ),
              ),
            ),

            ///控制栏
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: SlideHide(
                direction: SlideDirection.up,
                hide: !_controlsVisible,
                child: TopBar(
                  showOther: _showOtherStudent,
                  onOther: () {
                    setState(() {
                      _showOtherStudent = !_showOtherStudent;
                    });
                  },
                ),
              ),
            ),
            Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              child: SlideHide(
                direction: SlideDirection.down,
                hide: !_controlsVisible,
                child: BottomBar(
                  onTap: _toggleOverlay,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
