import 'package:app/pages/student/room/viewmodel/stu_room_vm.dart';
import 'package:app/providers/user_store.dart';
import 'package:app/utils/time.dart';
import 'package:app/widgets/base/button/index.dart';
import 'package:app/widgets/base/dialog/config_dialog.dart';
import 'package:app/widgets/room/board/board_manager.dart';
import 'package:app/widgets/room/core/count_down_vm.dart';
import 'package:app/widgets/room/other_widget.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';

class TopBar extends StatelessWidget implements PreferredSizeWidget {
  final bool showOther;
  final void Function()? onOther;

  const TopBar({
    super.key,
    this.showOther = false,
    this.onOther,
  });

  @override
  Widget build(BuildContext context) {
    final vm = context.watch<StuRoomVM>();
    final userStore = context.read<UserStore>();
    return AppBar(
      foregroundColor: Colors.white,
      titleTextStyle: const TextStyle(color: Colors.white, fontSize: 18),
      backgroundColor: const Color(0xff232426),
      centerTitle: true,
      leadingWidth: 100,
      leading: Container(
        padding: EdgeInsets.only(left: 10),
        alignment: Alignment.centerLeft,
        child: GestureDetector(
          onTap: () {
            showDialog(
              context: context,
              builder: (_) {
                return ConfigDialog(
                  content: "请确认是否退出自习室",
                  onCancel: () {
                    context.pop();
                  },
                  onConfirm: () {
                    context.pop();
                    context.pop();
                  },
                );
              },
            );
          },
          child: Text(
            "退出自习室",
            style: TextStyle(color: Colors.red),
          ),
        ),
      ),
      title: Consumer<CountDownVM>(
        builder: (context, vm, _) {
          return Column(
            children: [
              Text(vm.roomInfo!.roomName),
              Text(
                formatSeconds(vm.studyTime),
                style: const TextStyle(
                  fontSize: 12,
                  color: Colors.white24,
                ),
              ),
            ],
          );
        },
      ),
      actions: [
        ActionButton(
          icon: showOther ? RemixIcons.team_fill : RemixIcons.team_line,
          text: showOther ? "隐藏学生" : '显示学生',
          onTap: onOther,
        ),
        Visibility(
          visible: vm.roomInfo.roomStatus == 1,
          child: ActionButton(
            color: Theme.of(context).primaryColor,
            icon: RemixIcons.artboard_line,
            text: "进入白板",
            onTap: () {
              final boardManager = BoardManager();
              final vm = context.read<StuRoomVM>();
              boardManager.showBoardDialog(
                context,
                uid: userStore.userInfo!.name,
                roomId: vm.roomInfo.id,
                isTeacher: true,
              );
            },
          ),
        ),
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
