import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';

import 'location_info.dart';

part 'user_info.g.dart';

@JsonSerializable()
class UserInfo {
  @JsonKey(name: 'friendId')
  final String id;

  @JsonKey(name: 'phone')
  final String phoneNumber;

  @JsonKey(name: 'name')
  final String? name;

  @JsonKey(name: 'remark')
  String? remark;

  @JsonKey(name: 'timestamp')
  final int? timestamp;

  @JsonKey(name: 'blockedHim')
  bool? blockedHim;

  @JsonKey(name: 'blockedMe')
  final bool? blockedMe;

  @JsonKey(name: 'location', ignore: true)
  final Rxn<LocationInfo> lastLocation = Rxn<LocationInfo>();

  @JsonKey(name: 'virtual')
  final bool? virtual;

  @JsonKey(name: 'avatar')
  String? avatar;

  final bool? isMine;

  late bool? isShowYesterday;

  UserInfo({
    required this.id,
    required this.phoneNumber,
    this.name,
    this.remark,
    this.timestamp,
    this.blockedHim,
    this.blockedMe,
    this.virtual,
    this.isMine,
    this.avatar,
    this.isShowYesterday
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    final userinfo = _$UserInfoFromJson(json);

    LocationInfo? locationInfo = json['location'] == null
        ? null
        : LocationInfo.fromJson(json['location'] as Map<String, dynamic>);
    userinfo.lastLocation.value = locationInfo;
    return userinfo;
  }

  String getUserNickName() {
    return (remark?.isNotEmpty == true ? remark : phoneNumber) ?? '';
  }

  void updateLocation(LocationInfo location) {
    lastLocation.value = location;
  }
}
