import 'package:chat/models/upload_model.dart';
import 'package:chat/providers/user_provider.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/utils/ui_tools.dart';
import 'package:get/get.dart';

class UserController extends GetxController {
  static UserController get to => Get.find<UserController>();

  Future<bool> updateNickname(String nickname) async {
    var result = await UserProvider.updateNickname(nickname);
    if (result) {
      AuthService.to.updateUserInfo('nickname', nickname);
      UiTools.toast('昵称修改成功');
    }

    try {
      TimFriendService.to.setSelfInfo(nickname: nickname);
    } catch (e) {
      UiTools.toast(e.toString());
    }

    return result;
  }

  Future<void> uploadAvatar(String filePath) async {
    UploadModel? result = await UserProvider.uploadAvatar(filePath);
    if (result != null) {
      AuthService.to.updateUserInfo('avatar', result.url);
      UiTools.toast('头像修改成功');

      try {
        TimFriendService.to.setSelfInfo(avatar: result.url);
      } catch (e) {
        UiTools.toast(e.toString());
      }
    }
  }
}
