import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/talks.dart';
import 'package:electronic_assistant/data/repositories/agenda_repository.dart';
import 'package:electronic_assistant/data/repositories/talk_repository.dart';
import 'package:electronic_assistant/module/home/view.dart';
import 'package:electronic_assistant/module/main/controller.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/event_bus.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:electronic_assistant/widget/pull_to_refresh.dart';
import 'package:get/get.dart';
import '../../data/bean/agenda.dart';
import '../../data/repositories/account_repository.dart';
import '../../data/repositories/home_repository.dart';
import '../../utils/animated_list_controller.dart';
import '../../utils/error_handler.dart';

class HomePageController extends BaseController {
  get isLogin => accountRepository.isLogin.value;

  get loginTxt => accountRepository.isLogin.value
      ? accountRepository.getUserSubName(accountRepository.phone)
      : StringName.homeGoLogin.tr;

  final taskList = AnimatedListController<TalkBean>();
  final agendaList = AnimatedListController<Agenda>();

  final refreshController = PullToRefreshController();

  @override
  void onReady() {
    super.onReady();
    refreshController.requestRefresh();

    eventBus.on(EventUserLogin, (arg) {
      requestHomeData();
    });

    eventBus.on(EventUserLogout, (arg) {
      requestHomeData();
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    eventBus.off(EventUserLogin);
    eventBus.off(EventUserLogout);
  }

  void requestHomeData() {
    homeRepository.homeInfo().then((data) {
      taskList.clearAll();
      taskList.addAll(data.talks);
      agendaList.clearAll();
      agendaList.addAll(data.agendas,
          duration: const Duration(milliseconds: 500));
    }).whenComplete(() {
      refreshController.refreshCompleted();
    }).catchError((e) {
      // accountRepository.logout();
    });
  }

  void requestName(String? newName, TalkBean bean) {
    talkRepository.talkRename(bean.id, newName).then((data) {
      bean.title = newName;
      int index = taskList.indexOf(bean);
      if (index != -1) {
        taskList.update(index, bean);
        ToastUtil.showToast(StringName.talkRenameSuccess.tr);
      }
    }).catchError((error) {
      ErrorHandler.toastError(error, message: StringName.talkRenameFail.tr);
    });
  }

  void goTalkRecordPage() {
    Get.find<MainController>().updateIndexByPageName(StringName.mainTabFile);
  }

  void showLoginDrawer() {
    Get.find<MainController>().openDrawer();
  }

  void requestDelete(TalkBean item) {
    talkRepository.talkDelete(item.id).then((data) {
      requestHomeData();
    }).catchError((error) {
      ErrorHandler.toastError(error, message: StringName.talkDeleteFail.tr);
    });
  }

  void agendaComplete(Agenda item) {
    agendaRepository.agendaFinish(item.id, true).then((data) {
      item.isDone = true;
      agendaList.removeItem(
          item,
          (context, animation, item) =>
              buildRemoveTodoItem(context, animation, item));
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }
}
