import 'package:app/global/theme/theme.dart';
import 'package:app/providers/user_store.dart';
import 'package:app/utils/time.dart';
import 'package:app/widgets/base/dialog/config_dialog.dart';
import 'package:app/widgets/room/board/board_manager.dart';
import 'package:app/widgets/room/other_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';

import '../../../../widgets/room/core/count_down_vm.dart';
import '../viewmodel/tch_room_vm.dart';
import '../viewmodel/type.dart';

class TopBar extends StatelessWidget implements PreferredSizeWidget {
  const TopBar({super.key});

  @override
  Widget build(BuildContext context) {
    final userStore = context.read<UserStore>();
    final vm = context.watch<TchRoomVM>();

    return AppBar(
      backgroundColor: Color(0xff373c3e),
      foregroundColor: Colors.white,
      title: Column(
        spacing: 5,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(vm.roomInfo.roomName, style: TextStyle(color: Colors.white, fontSize: 18)),
          Row(
            spacing: 15,
            children: [
              Consumer<CountDownVM>(
                builder: (context, countVM, __) {
                  return _infoItem(
                    context,
                    title: "剩余 ${formatSeconds(countVM.endCountDown)}",
                    icon: RemixIcons.time_line,
                  );
                },
              ),
              _infoItem(
                context,
                title: "${vm.students.length} 名学生",
                icon: RemixIcons.group_line,
              ),
            ],
          ),
        ],
      ),
      actions: [
        ActionButton(
          icon: RemixIcons.video_on_ai_line,
          text: "关闭全部",
          onTap: () {
            _closeAll(context, StudentAction.camera);
          },
        ),
        ActionButton(
          icon: RemixIcons.volume_up_line,
          text: "全部静音",
          onTap: () {
            _closeAll(context, StudentAction.speaker);
          },
        ),
        Visibility(
          visible: vm.roomInfo.roomStatus == 1,
          child: ActionButton(
            text: "打开白板",
            color: Theme.of(context).primaryColor,
            icon: RemixIcons.artboard_line,
            onTap: () {
              final boardManager = BoardManager();
              boardManager.showBoardDialog(
                context,
                uid: userStore.userInfo!.name,
                roomId: vm.roomInfo.id,
                isTeacher: true,
              );
            },
          ),
        ),
        Consumer<TchRoomVM>(
          builder: (context, vm, _) {
            if (vm.roomInfo.roomStatus != 1) {
              return SizedBox();
            }
            return ActionButton(
              icon: RemixIcons.close_line,
              color: context.danger,
              text: "结束自习室",
              onTap: () {
                showDialog(
                  context: context,
                  builder: (_) {
                    return ConfigDialog(
                      content: '是否结束自习室？结束后无法在进入',
                      onCancel: () {
                        context.pop();
                      },
                      onConfirm: () {
                        context.pop();
                        vm.endRoom();
                        EasyLoading.showToast("会议室已结束");
                      },
                    );
                  },
                );
              },
            );
          },
        ),
      ],
    );
  }

  Widget _infoItem(
    BuildContext context, {
    required String title,
    required IconData icon,
  }) {
    return Row(
      children: [
        Icon(icon, color: Colors.white54, size: 14),
        SizedBox(width: 4),
        Text(title, style: TextStyle(fontSize: 12, color: Colors.white54)),
      ],
    );
  }

  void _closeAll(BuildContext context, StudentAction action) {
    final vm = context.read<TchRoomVM>();
    String content = (action == StudentAction.camera) ? '是否关闭所有学生的摄像头？' : '是否关闭所有学生的扬声器？';

    showDialog(
      context: context,
      builder: (_) {
        return ConfigDialog(
          content: content,
          onCancel: () => context.pop(),
          onConfirm: () {
            context.pop();
            vm.closeAllStudentAction(action);
            EasyLoading.showToast("操作已完成");
          },
        );
      },
    );
  }

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