import 'package:get/get.dart';
import 'package:news_getx/data/model/news.dart';
import 'package:news_getx/data/repository/news_repository.dart';
import 'package:news_getx/modules/category/category_state.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CategoryController extends GetxController {
  NewsRepository newsRepository;

  CategoryController({required this.newsRepository});

  /// UI 组件
  final RefreshController refreshController = RefreshController(
    initialRefresh: true,
  );

  /// 响应式成员变量
  final state = CategoryState();

  /// 成员变量
  String categoryCode = '';
  int curPage = 1;
  int pageSize = 20;
  int total = 20;

  /// 事件
  void onRefresh() {
    fetchNewsList(isRefresh: true).then((_) {
      refreshController.refreshCompleted(resetFooterState: true);
    }).catchError((_) {
      refreshController.refreshFailed();
    });
  }

  void onLoading() {
    if (state.newsList.length < total) {
      fetchNewsList().then((_) {
        refreshController.loadComplete();
      }).catchError((_) {
        refreshController.loadFailed();
      });
    } else {
      refreshController.loadNoData();
    }
  }

  /// 方法

  // 拉取数据
  Future<void> fetchNewsList({bool isRefresh = false}) async {
    var result = await newsRepository.newsPageList(
      params: NewsPageListRequest(
        categoryCode: categoryCode,
        pageNum: curPage + 1,
        pageSize: pageSize,
      ),
    );

    if (isRefresh) {
      curPage = 1;
      total = result.counts!;
      state.newsList.clear();
    } else {
      curPage++;
    }

    state.newsList.addAll(result.items!);
  }

  ///dispose 释放内存
  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
  }
}
