import 'dart:convert';
import 'dart:math';

import 'package:azlistview/azlistview.dart';
import 'package:sport/bean/user_friend.dart';
import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';

class UserFriendModel extends ViewStateRefreshListModel<UserFriend>
    with InjectApi {
  int type;
  String kw;
//  Map map;
  List<SuspensionBean> items = [];
  List<SuspensionBean> originItems = [];

  List originList = [];
  List<String> tags = [];
  UserFriendModel(this.type);

  @override
  Future<List<UserFriend>> loadData({int pageNum}) async {
    List<UserFriend> results = [];
    switch (type) {
      case 2:
        results =
            (await api.userFansList(kw: kw, page: max(1, pageNum))).results.list;
        originList = results;

        break;
      case 1:
        results =
            (await api.userFollowList(kw: kw, page: max(1, pageNum))).results.list;
        originList = results;
        break;
      default:
        ResultsTest<UserFriend> data = (await api.userFriendsTest(
                kw: kw, page: max(1, pageNum), test: "1"))
            .results;
        items = [];
        tags = data.map.keys.toList();
        data.map.forEach((key, value) {
          value.forEach((element) {
            items.add(SuspensionBean(key, UserFriend.fromJson(element)));
          });
        });
//        items.forEach((element) {
//          print(element.toJson());
//        });
        originItems = items;
        SuspensionUtil.setShowSuspensionStatus(items);
        SuspensionUtil.sortListBySuspensionTag(items);
        break;
    }
    print("[RESULTS]${results}------------------------------");
    return results;
  }

  void submitValue(String value) {
    this.kw = value;
    refresh(init: true);
  }
}

class SuspensionBean extends ISuspensionBean {
  final String tag;
  final UserFriend user;

  SuspensionBean(this.tag, this.user);

  @override
  String getSuspensionTag() => tag;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['tag'] = this.tag;
    data['user'] = this.user.toJson();
    return data;
  }
}

class UserFriendListModel extends ISuspensionBean {
  String name;
  String tagIndex;
  String namePinyin;

  UserFriendListModel({
    this.name,
    this.tagIndex,
    this.namePinyin,
  });

  UserFriendListModel.fromJson(Map<String, dynamic> json) : name = json['name'];

  Map<String, dynamic> toJson() => {
        'name': name,
//        'tagIndex': tagIndex,
//        'namePinyin': namePinyin,
//        'isShowSuspension': isShowSuspension
      };

  @override
  String getSuspensionTag() => tagIndex;

  @override
  String toString() => json.encode(this);
}
