import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/api/request/talk_request.dart';
import 'package:electronic_assistant/data/bean/agenda.dart';
import 'package:electronic_assistant/data/consts/error_code.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';

import '../../utils/http_handler.dart';
import '../api/atmob_api.dart';
import '../api/request/agenda_request.dart';
import '../api/request/agenda_status_request.dart';
import '../api/request/agenda_todo_request.dart';
import '../api/response/agenda_list_all_response.dart';
import '../api/response/agenda_list_mine_response.dart';
import '../api/response/agenda_response.dart';
import 'account_repository.dart';

class AgendaRepository {
  AgendaRepository._();

  final _agendaList = RxList<Agenda>();

  RxList<Agenda> get agendaList => _agendaList;

  clearAgendaList() {
    _agendaList.clear();
  }

  void setAgendaList(List<Agenda> list) {
    _agendaList.assignAll(list);
  }

  removeItem(Agenda item) {
    _agendaList.removeWhere((element) => element.id == item.id);
  }

  Future<AgendaResponse> requestHomeAgendaData(
      {int pageSize = 999, TaskStatus completeStatus = TaskStatus.TODO}) {
    return requestAgendaPagePaginate(1, pageSize,
        completeStatus: completeStatus);
  }

  Future<AgendaResponse> requestAgendaPagePaginate(int page, int pageSize,
      {TaskStatus? completeStatus}) {
    return agendaPage(page, pageSize, completeStatus: completeStatus)
        .then((response) {
      if (page == 1) {
        _agendaList.clear();
      }
      if (response.list != null) {
        _agendaList.addAll(response.list!);
      }
      return response;
    });
  }

  Future<AgendaResponse> agendaPage(int page, int pageSize,
      {int? startTime, int? endTime, TaskStatus? completeStatus}) {
    return atmobApi
        .agendaPage(AgendaRequest(page, pageSize,
            startTime: startTime,
            endTime: endTime,
            completeStatus: completeStatus?.value))
        .then(HttpHandler.handle(true));
  }

  Future<void> agendaFinish(String? id, bool complete) async {
    if (!accountRepository.isLogin.value) {
      throw ServerErrorException(
          ErrorCode.errorCodeNoLogin, StringName.errorCodeNoLogin.tr);
    }
    return atmobApi
        .agendaFinish(AgendaStatusRequest(id, complete))
        .then(HttpHandler.handle(true));
  }

  Future<AgendaListAllResponse> agendaListAll(String talkId) {
    return atmobApi
        .agendaListAll(
            TalkRequest(talkId, isExample: !accountRepository.isLogin.value))
        .then(HttpHandler.handle(true));
  }

  Future<AgendaListMineResponse> agendaListMine(String talkId) {
    return atmobApi
        .agendaListMine(
            TalkRequest(talkId, isExample: !accountRepository.isLogin.value))
        .then(HttpHandler.handle(true));
  }

  Future<void> agendaTodo(String id, bool isTodo) async {
    if (!accountRepository.isLogin.value) {
      throw ServerErrorException(
          ErrorCode.errorCodeNoLogin, StringName.errorCodeNoLogin.tr);
    }
    return atmobApi
        .agendaTodo(AgendaTodoRequest(id, isTodo))
        .then(HttpHandler.handle(true))
        .then((data) {
      requestHomeAgendaData();
    });
  }
}

final agendaRepository = AgendaRepository._();
