import 'package:app/utils/time.dart';
import 'package:app/widgets/base/dialog/config_dialog.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 '../../../../widgets/room/core/count_down_vm.dart';
import 'content_view.dart';
import '../viewmodel/tch_room_vm.dart';

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

  @override
  State<StatusView> createState() => _StatusViewState();
}

class _StatusViewState extends State<StatusView> {
  @override
  void initState() {
    super.initState();
    final countVM = context.read<CountDownVM>();
    countVM.removeListener(_onCountDownEnd);
    countVM.addListener(_onCountDownEnd);
  }

  ///开播中返回拦截弹窗
  void _interceptPop() {
    showDialog(
      context: context,
      builder: (context) {
        return ConfigDialog(
          content: "是否退出自习室",
          onCancel: () {
            context.pop();
          },
          onConfirm: () {
            context.pop();
            context.pop();
          },
        );
      },
    );
  }

  ///监听会议室倒计时结束的时候
  void _onCountDownEnd() {
    final countVM = context.read<CountDownVM>();
    if (countVM.endCountDown == 0) {
      EasyLoading.showToast("自习室已到结束时间，请记得关闭会议室");
      countVM.removeListener(_onCountDownEnd);
    }
  }

  @override
  Widget build(BuildContext context) {
    final tchVM = context.watch<TchRoomVM>();
    var roomStatus = tchVM.roomInfo.roomStatus;

    /// 1. 未加载
    if (roomStatus == -1) {
      return const Align(
        child: Text("加载中", style: TextStyle(color: Colors.white)),
      );
    }

    /// 2. 未开始的房间
    if (roomStatus == 0) {
      return Consumer<CountDownVM>(
        builder: (_, countVM, __) {
          if (countVM.canEnterRoom) {
            tchVM.toggleRoom(isOpen: true);
            return SizedBox();
          } 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,
                      ),
                    ),
                  ),
                ],
              ),
            );
          }
        },
      );
    }

    /// 3. 已开播
    if (roomStatus == 1) {
      return PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, _) {
          if (!didPop) {
            _interceptPop();
          }
        },
        child: const ContentView(),
      );
    }

    return const SizedBox();
  }
}
