import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/repositories/agenda_repository.dart';
import 'package:electronic_assistant/data/repositories/talk_repository.dart';
import 'package:electronic_assistant/utils/error_handler.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../data/api/request/agenda_request.dart';
import '../../data/bean/agenda.dart';
import 'detail/view.dart';

class AgendaController extends BaseController {
  String get filterTxt => '展示近两周待办';

  final agendaDetailPopupTag = 'agendaDetailPopupTag';
  final todoIsExpanded = true.obs;
  final doneIsExpanded = true.obs;
  final refreshController = RefreshController(initialRefresh: false);

  final RxList<Agenda> agendaTodoList = <Agenda>[].obs;
  final RxList<Agenda> agendaDoneList = <Agenda>[].obs;

  final doneSize = 0.obs;
  final int limit = 10;

  @override
  void onReady() {
    refreshTodoTaskList();
    agendaDonePage(0, limit, isClearAll: true);
  }

  void onLoadMoreDoneData() {
    agendaDonePage(agendaDoneList.length, limit);
  }

  agendaDonePage(int offset, int limit, {bool? isClearAll}) {
    agendaRepository
        .agendaPage(offset, limit, completeStatus: TaskStatus.DONE)
        .then((response) {
      doneSize.value = response.count;
      if (isClearAll == true) {
        agendaDoneList.clear();
      }
      if (response.list != null) {
        agendaDoneList.addAll(response.list!);
      }
      if (agendaDoneList.length >= response.count) {
        debugPrint("refreshAgendaPage-没有更多数据了");
        refreshController.loadNoData();
      } else {
        refreshController.loadComplete();
      }
      refreshController.refreshCompleted();
    }).catchError((error) {
      refreshController.loadFailed();
      refreshController.refreshFailed();
      ErrorHandler.toastError(error);
    });
  }

  refreshTodoTaskList() {
    agendaRepository
        .agendaPage(0, 999, completeStatus: TaskStatus.TODO)
        .then((response) {
      agendaTodoList.clear();
      if (response.list != null) {
        agendaTodoList.addAll(response.list!);
      }
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void onClickTodoGroup() {
    todoIsExpanded.value = !todoIsExpanded.value;
  }

  void onClickDoneGroup() {
    doneIsExpanded.value = !doneIsExpanded.value;
  }

  void agendaComplete(Agenda item, bool isComplete) {
    agendaRepository.agendaFinish(item.id, isComplete).then((data) {
      agendaRepository.requestHomeAgendaData();
      _refreshData();
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void _refreshData() {
    refreshTodoTaskList();
    int size;
    if (agendaDoneList.isEmpty) {
      size = limit;
    } else {
      size = agendaDoneList.length;
    }
    agendaDonePage(0, size, isClearAll: true);
  }

  void onAgendaCancel(Agenda item) {
    agendaRepository.agendaTodo(item.id, false).then((value) {
      agendaRepository.requestHomeAgendaData();
      _refreshData();
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void onAgendaUpdate(Agenda item) {
    AgendaDetailPage.start(item);
  }
}
