import 'package:chat/models/UserInfo.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/conversation_table.dart';
import 'package:chat/utils/sql_util.dart';
import 'package:chat/utils/TokenMgr.dart';

class FriendListMgr {
  // 工厂模式
  factory FriendListMgr() => _getInstance();

  static FriendListMgr get instance => _getInstance();
  static FriendListMgr _instance;

  FriendListMgr._internal();

  static FriendListMgr _getInstance() {
    if (_instance == null) {
      _instance = FriendListMgr._internal();
    }
    return _instance;
  }

  List<FriendModel> friendList = [];

  //获取好友列表
  getFriendList() async {
    if (friendList != null && friendList.length > 0) {
      return friendList;
    }
    friendList = await SqlUtil.friendTableModel.getAllConversation();
    if (friendList.length == 0) {
      //从服务器拉取
      print('从服务器获取好友数据');
      getFriendListFromServer();
    }

    return friendList;
  }

  getFriendListFromServer() {
    HttpUtil().getFriendList((List data) {
      Set oldUserId = new Set();
      for (var friendModel in friendList) {
        oldUserId.add(friendModel.friendId);
      }
      data.forEach((f) {
        var friend = FriendModel.fromServerJson(f);
        if (!oldUserId.contains(friend.friendId)) {
          addFriend(friend);
        }
        oldUserId.remove(friend.friendId);
      });
      for (var friendId in oldUserId) {
        delteFriend(friendId);
      }
    });
  }

  //添加好友
  addFriend(FriendModel friendModel) {
    if (isMyFriend(friendModel.friendId)) {
      return;
    }
    friendList.add(friendModel);
    SqlUtil.friendTableModel.insert(friendModel);
  }

  //删除好友
  delteFriend(int friendId) {
    for (var friendModel in friendList) {
      if (friendModel.friendId == friendId) {
        friendList.remove(friendModel);
        break;
      }
    }
    SqlUtil.friendTableModel.deleteFriend(friendId);
  }

  //删除所有好友
  deleteAllFriend() {
    friendList.clear();
    SqlUtil.friendTableModel.clear();
  }

  //是否是我的朋友
  isMyFriend(int userId) {
    for (var friendModel in friendList) {
      if (friendModel.friendId == userId) {
        return true;
      }
    }
    return false;
  }

  //同步好友信息
  updateFriendInfo(UserInfo info) {
    for (int i = 0; i < friendList.length; i++) {
      if (friendList[i].friendId == info.userId) {
        SqlUtil.friendTableModel.deleteFriend(info.userId);
        friendList[i].name = info.nickName;
        friendList[i].avatar = info.headimgurl;
        friendList[i].nameTag = friendList[i].getNameTag();
        SqlUtil.friendTableModel.insert(friendList[i]);
      }
    }
  }

  updateRefNmae(int userId) {
    for (int i = 0; i < friendList.length; i++) {
      if (friendList[i].friendId == userId) {
        friendList[i].updataRefName();
      }
    }
  }

  //从服务器校验好友
  checkFromServer(String sign) async {
    List<FriendModel> friendList =
        await SqlUtil.friendTableModel.getAllConversation();
    List idList = [];
    for (var model in friendList) {
      idList.add(model.friendId);
    }
    idList.sort();
    if (friendList.length == 0 || TokenMgr().generateMd5(idList.join(',')) != sign) {
      getFriendListFromServer();
    }
  }
}
