import 'dart:async';

import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/talks.dart';
import 'package:electronic_assistant/data/repositories/account_repository.dart';
import 'package:electronic_assistant/data/repositories/talk_repository.dart';
import 'package:get/get.dart';
import '../../../data/bean/agenda_list_all_bean.dart';
import '../controller.dart';

class SummaryController extends BaseController {
  final TalkController talkController = Get.find();

  final Rxn<TalkBean> summaryBean = Rxn();

  StreamSubscription? _talkStatusListener;

  RxList<AgendaListAllBean> get agendaAllList => talkController.agendaAllList;

  @override
  void onReady() {
    super.onReady();
    _talkStatusListener = talkController.talkBean.value?.status.listen((bean) {
      _dealTalkUpdate();
    });
    _dealTalkUpdate();
  }

  void _dealTalkUpdate() {
    refreshSummaryData();
  }

  void refreshSummaryData() {
    String? id = talkController.talkBean.value?.id;
    if (id == null) {
      return;
    }
    talkRepository.talkInfo(id).then((data) {
      summaryBean.value = data.talkInfo;
    });
    talkController.refreshAgendaAllData();
  }

  @override
  void onClose() {
    super.onClose();
    _talkStatusListener?.cancel();
  }
}
