import 'package:app/utils/time.dart';
import 'package:app/widgets/base/button/index.dart';
import 'package:app/widgets/room/core/count_down_vm.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import '../video/teacher_video.dart';
import '../viewmodel/stu_room_vm.dart';

class StatusView extends StatelessWidget {
  const StatusView({super.key});

  @override
  Widget build(BuildContext context) {
    final vm = context.watch<StuRoomVM>();
    final teacherInfo = vm.teacherInfo;

    ///没开始
    if (vm.roomInfo.roomStatus == 0) {
      return Consumer<CountDownVM>(
        builder: (_, countVM, __) {
          if (countVM.canEnterRoom) {
            return _empty("等待老师进入自习室");
          } else {
            countVM.startStartCountdown();
            return Align(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text(
                    "未到开播时间",
                    style: TextStyle(color: Colors.white),
                  ),
                  Container(
                    margin: const EdgeInsets.symmetric(vertical: 10),
                    child: Text(
                      formatSeconds(countVM.startCountDown),
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 26,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ],
              ),
            );
          }
        },
      );
    }

    ///结束
    if (vm.roomInfo.roomStatus == 2) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        spacing: 5,
        children: [
          _empty("自习室已结束"),
          SizedBox(
            width: 120,
            child: Button(
              text: "返回首页",
              onPressed: () {
                context.pop();
              },
            ),
          ),
        ],
      );
    }

    ///开始
    if (vm.roomInfo.roomStatus == 1 && vm.engine != null) {
      if (teacherInfo == null) {
        return _empty("老师不在自习室内");
      }
      if (teacherInfo.online == 0) {
        return _empty("老师暂时离开，请耐心等待");
      }
      return TeacherVideo();
    }

    return _empty("加载中");
  }

  Widget _empty(String title) {
    return SafeArea(
      child: Align(
        child: Text(
          title,
          style: TextStyle(color: Colors.white),
        ),
      ),
    );
  }
}
