import 'dart:async';

import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/repositories/talk_repository.dart';
import 'package:get/get.dart';

import '../../../data/bean/talk_original.dart';
import '../../../data/bean/talks.dart';
import '../controller.dart';

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

  final originalList = <TalkOriginal>[].obs;

  StreamSubscription? _talkBeanListener;

  @override
  void onReady() {
    super.onReady();
    _talkBeanListener = talkController.talkBean.listen((bean) {
      if (bean?.status.value == TalkStatus.analysisSuccess) {
        requestOriginal();
      }
    });
    requestOriginal();
  }

  void requestOriginal() {
    if (originalList.isNotEmpty) {
      return;
    }
    talkRepository.talkOriginal(talkController.talkBean.value?.id).then((value) {
      originalList.value = value;
    });
  }

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