import 'package:app/request/api/room_api.dart';
import 'package:app/request/dto/room/room_list_item_dto.dart';
import 'package:app/utils/time.dart';
import 'package:flutter/material.dart';

class HomeViewModel extends ChangeNotifier {
  RoomListItemDto ? roomInfo;
  bool loading = true;

  HomeViewModel() {
    loadData();
  }

  //加载数据
  Future<void> loadData() async {
    final list = await getRoomListApi();
    loading = false;

    if (list.isNotEmpty) {
      roomInfo = list.first;
    }

    notifyListeners();
  }

  ///计算会议时间
  int get roomMinutes {
    if (roomInfo == null) return 0;
    final start = parseTime(roomInfo!.startTime);
    final end = parseTime(roomInfo!.endTime);

    return end.difference(start).inMinutes;
  }

  ///能否进入房间
  bool get canEnterRoom {
    final info = roomInfo;
    if (info == null) return false;

    final now = DateTime.now();

    //开始时间
    final startTime = parseTime(info.startTime);

    // 当前时间距离开始时间是否超过 5 分钟
    if (now.isBefore(startTime) && startTime.difference(now).inMinutes > 5) {
      return false;
    }

    return true;
  }
}
