import 'dart:async';

import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/agenda.dart';
import 'package:electronic_assistant/data/consts/event_report_id.dart';
import 'package:electronic_assistant/data/repositories/agenda_repository.dart';
import 'package:electronic_assistant/module/chat/view.dart';
import 'package:electronic_assistant/utils/error_handler.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../data/bean/agenda_list_all_bean.dart';
import '../../../data/bean/talks.dart';
import '../../../utils/event_bus.dart';
import '../controller.dart';

class TodoController extends BaseController {
  static String refreshTalkMineTask = "refreshTalkMineTask";
  final String? talkId;

  StreamSubscription? _talkBeanListener;

  final agendaMineList = <Agenda>[].obs;

  final mineAgendaIsExpanded = true.obs;

  TodoController(this.talkId);

  get _talkController => Get.find<TalkController>(tag: talkId);

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

  Rxn<TalkBean> get talkBean => _talkController.talkBean;

  bool get isEditModel => _talkController.isEditModelRx.value;

  TalkController get talkController => _talkController;

  @override
  void onReady() {
    super.onReady();
    _talkBeanListener = _talkController.talkBean.listen((bean) {
      _dealTalkUpdate(bean);
    });
    _dealTalkUpdate(_talkController.talkBean.value);
    _eventRefresh();
  }

  void _eventRefresh() {
    eventBus.on(refreshTalkMineTask, (data) {
      requestMineTodoData();
    });
  }

  void _dealTalkUpdate(TalkBean? bean) {
    if (bean?.status.value == TalkStatus.analysisSuccess) {
      _talkController.refreshAgendaAllData();
      requestMineTodoData();
      eventReport(EventId.event_101200);
    } else if (bean?.status.value == TalkStatus.analysisFail) {
      eventReport(EventId.event_101203);
    }
  }

  void eventReport(String eventId, {Map<String, dynamic>? params}) {
    _talkController.eventReport(eventId, params: params);
  }

  void requestMineTodoData() {
    String? id = _talkController.talkBean.value?.id;
    if (id == null) {
      return;
    }
    agendaRepository.agendaListMine(id).then((data) {
      agendaMineList.value = data.list ?? [];
    });
  }

  void agendaTodo(Agenda? agenda) {
    if (agenda == null) {
      return;
    }
    if (!agenda.todo.value) {
      eventReport(EventId.event_101201);
    }
    bool isSetTodo = !agenda.todo.value;
    agendaRepository.agendaTodo(agenda.id, isSetTodo).then((value) {
      agenda.todo.value = isSetTodo;
      requestMineTodoData();
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void onClickThinking(Agenda agenda) async {
    if (!await _talkController.checkLogin()) {
      return;
    }
    TalkBean? bean = talkBean.value;
    eventReport(EventId.event_101202);
    ChatFromType fromType = talkBean.value!.isExample == true
        ? ChatFromType.fromTalkExample
        : ChatFromType.fromAnalysisBtn;
    if (bean != null) {
      ChatPage.startByTalk(fromType, bean, agenda: agenda);
    } else {
      ChatPage.start(fromType);
    }
  }

  void showAddAgendaDialog(BuildContext context) {
    _talkController.showSingleAddAgendaDialog(context);
  }

  void removeTalkAgenda(List<Agenda>? list, Agenda agenda) {
    _talkController.removeTalkAgenda(list, agenda);
  }

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