import 'dart:math';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
import 'package:sport/services/api/inject_api.dart';

class SocialDetailModel extends ViewStateRefreshListModel with InjectApi {
  int _type = 0;
  String forumId;
  String _kw;

  SocialDetailModel(this.forumId, this._type);

  void swtichTab(int type) async {
    this._type = type;
    initData();
  }

  void setKeyword(String kw) async {
    this._type = 100;
    this._kw = kw;
    initData();
  }

  @override
  Future<List> loadData({int pageNum = 1}) async {
    List results = [];
    switch (_type) {
      case 100:
        results = handlePage(await api.getPostList(kw: _kw, page: max(1, pageNum)));
        break;
      case 4:
        SharedPreferences prefs = await SharedPreferences.getInstance();
        results = handlePage(await api.getPostListByUser(prefs.getInt("id").toString(), forumId: forumId, page: max(1, pageNum)));
        break;
      case 3:
        results = handlePage(await api.getPostList(forumId: forumId, isGood: 1, page: max(1, pageNum)));
        break;
      case 2:
        results = handlePage(await api.getPostList(forumId: forumId, page: max(1, pageNum)));
        break;
      case 1:
        SharedPreferences prefs = await SharedPreferences.getInstance();
        results = handlePage(await api.getPostListByFollow(prefs.getInt("id").toString(), forumId, page: max(1, pageNum)),);
        break;
      default:
        results = handlePage(await api.getPostListByHot(forumId, page: max(1, pageNum)));
        break;
    }
    return results;
  }
}
