import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/repositories/agenda_repository.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../data/api/response/example_info_response.dart';
import '../../../data/bean/talks.dart';
import '../../../data/consts/event_report_id.dart';
import '../../../data/repositories/account_repository.dart';
import '../../../data/repositories/config_repository.dart';
import '../../../data/repositories/talk_repository.dart';
import '../../../dialog/rename_dialog.dart';
import '../../../dialog/talk_delete_dialog.dart';
import '../../../handler/event_handler.dart';
import '../../../popup/talk_popup.dart';
import '../../../resource/string.gen.dart';
import '../../../utils/error_handler.dart';
import '../../../utils/event_bus.dart';
import '../../../utils/toast_util.dart';
import '../../main/controller.dart';
import '../../talk/view.dart';

class HomeTalkController extends BaseController {
  RxList<TalkBean> get talkList => talkRepository.talkList;

  final refreshController = RefreshController(initialRefresh: false);

  @override
  void onReady() {
    super.onReady();
    eventBus.on(EventUserLogin, (arg) {
      requestHomeTalkData();
    });
    eventBus.on(EventUserLogout, (arg) {
      agendaRepository.setAgendaList([]);
      requestHomeTalkData();
    });
    requestHomeTalkData();
    EventHandler.report(EventId.event_100100);
  }

  @override
  void onClose() {
    eventBus.off(EventUserLogin);
    eventBus.off(EventUserLogout);
  }

  requestHomeTalkData() async {
    try {
      if (accountRepository.isLogin.value) {
        await talkRepository.refreshHomeTalkData();
      } else {
        ExampleInfoResponse exampleInfo =
            await configRepository.getExampleInfo();
        talkRepository.setTalkList(
            exampleInfo.talkInfoList != null ? exampleInfo.talkInfoList! : []);
      }
    } catch (e) {
      ErrorHandler.toastError(e);
    } finally {
      refreshController.refreshCompleted();
    }
  }

  onRefresh() {
    requestHomeTalkData();
  }

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

  void onTalkItemClick(TalkBean item) {
    if (item.isExample == true) {
      EventHandler.report(EventId.event_100001);
    }
    TalkPage.start(item);
  }

  void onLongPressTalkItem(TalkBean item, LongPressStartDetails details) {
    if (!accountRepository.isLogin.value) {
      return;
    }
    showTalkPopup(details.globalPosition, Alignment.bottomRight, onRename: () {
      showRenameTalkDialog(item);
    }, onDelete: () {
      showDeleteTalkDialog(item);
    });
  }

  void showRenameTalkDialog(TalkBean item) {
    reNameDialog(StringName.talkRenameTitle.tr, item.title.value,
        hintTxt: StringName.talkRenameTitleHint.tr,
        maxLength: 15, returnBuilder: (newName) {
      requestName(newName, item);
    });
  }

  void showDeleteTalkDialog(TalkBean item) {
    talkDeleteDialog(item.id, item.title.value, returnBuilder: () {
      requestDelete(item);
    });
  }

  void requestName(String? newName, TalkBean bean) {
    talkRepository.talkRename(bean.id, newName).then((data) {
      bean.title.value = newName;
      ToastUtil.showToast(StringName.talkRenameSuccess.tr);
    }).catchError((error) {
      ErrorHandler.toastError(error, message: StringName.talkRenameFail.tr);
    });
  }

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