import 'dart:math';

import 'package:sport/bean/comment.dart';
import 'package:sport/bean/post.dart';
import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
import 'package:sport/services/api/inject_api.dart';

class PostDetailModel extends ViewStateRefreshListModel with InjectApi {
  final Post _post;

  String _sortBy = "created_at";

  PostDetailModel(this._post);

  @override
  Future<List> loadData({int pageNum = 1}) async {
    if (pageNum == 1) {
      return handlePage(await api.getPostComments(_post.id, page: max(1, pageNum), sortBy: _sortBy));
    } else {
      if (list.isEmpty) {
        return handlePage(await api.getPostComments(_post.id, page: max(1, pageNum), sortBy: _sortBy));
      } else {
        Comment comment = list.last;
        return (await api.getPostCommentsFromId(comment.id, sortBy: _sortBy)).results;
      }
    }
  }

  sortBy(String name) {
    if (name == "点赞最多") {
      _sortBy = "like_count";
    } else if (name == "最新评论") {
      _sortBy = "created_at";
    }
    initData();
  }
}
