import 'package:chat/models/group_info_server_model.dart';
import 'package:chat/proto/all.pbserver.dart';
import 'package:lpinyin/lpinyin.dart';

class GroupMemberModel {
  int memberId;
  int identity;
  String refName;
  String nickName;
  String avtar;
  int inGroup;
  String nameTag;

  GroupMemberModel(
      {this.memberId,
      this.identity,
      this.refName,
      this.nickName,
      this.inGroup,
      this.nameTag,
      this.avtar});

  GroupMemberModel.fromPb(GroupMember member) {
    memberId = member.info.id;
    identity = member.identity.value;
    refName = member.name;
    nickName = member.info.niceName;
    avtar = member.info.headUrl;
    inGroup = member.effectiveUser ? 1 : 0;
  }

  GroupMemberModel.fromServerMember(GroupServerMembers member) {
    memberId = member.info.id;
    identity = member.identity;
    refName = member.name;
    nickName = member.info.niceName;
    avtar = member.info.headUrl;
    inGroup = member.effectiveUser ? 1 : 0;
  }

  GroupMemberModel.fromBaseInfo(BaseUserInfo baseInfo) {
    memberId = baseInfo.id;
    identity = 0;
    inGroup = 1;
    refName = baseInfo.niceName;
    nickName = baseInfo.niceName;
    avtar = baseInfo.headUrl;
  }

  GroupMemberModel.fromJson(Map<String, dynamic> json) {
    memberId = json['memberId'];
    identity = json['identity'] ?? 0;
    refName = json['refName'];
    nickName = json['nickName'];
    avtar = json['avtar'];
    inGroup = json['inGroup'] ?? 1;
  }

  getNameTag() {
    var origin = refName;
    if (origin == null || origin.length == 0) {
      origin = nickName;
    }
    String pinyin = PinyinHelper.getPinyinE(origin);

    String tag = '#';
    if (pinyin == null || pinyin.length == 0) {
      tag = '#';
    } else {
      tag = pinyin.substring(0, 1).toUpperCase();
    }

    if (tag == 'Ẩ') {
      tag = 'A';
    } else if (tag == 'Đ') {
      tag = 'D';
    }

    this.nameTag = tag;
  }
}
