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/request/agenda_update_bean.dart';
import '../api/request/agenda_update_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._();

  static const homeMaxShowCount = 15;

  final _agendaList = RxList<Agenda>();

  RxList<Agenda> get agendaList => _agendaList;

  clearAgendaList() {
    _agendaList.clear();
  }

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

  Future<AgendaResponse> requestHomeAgendaData() {
    return requestAgendaPagePaginate(0, homeMaxShowCount,
        completeStatus: TaskStatus.TODO, isClearAll: true);
  }

  Future<AgendaResponse> requestAgendaPagePaginate(int offset, int limit,
      {TaskStatus? completeStatus, bool? isClearAll}) {
    return agendaPage(offset, limit, completeStatus: completeStatus)
        .then((response) {
      if (isClearAll == true) {
        _agendaList.clear();
      }
      if (response.list != null) {
        _agendaList.addAll(response.list!);
      }
      return response;
    });
  }

  Future<AgendaResponse> agendaPage(int offset, int limit,
      {int? startTime, int? endTime, TaskStatus? completeStatus}) {
    return atmobApi
        .agendaPage(AgendaRequest(offset, limit,
            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> agendaUpdate(String talkId, List<AgendaUpdateBean> list) {
    return atmobApi
        .agendaUpdate(AgendaUpdateRequest(talkId, list))
        .then(HttpHandler.handle(true))
        .then((data) {
      requestHomeAgendaData();
      return data;
    });
  }

  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._();
