import 'package:flutter/cupertino.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_room_repository_imp.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
import 'package:yumi/shared/business_logic/models/req/sc_user_profile_cmd.dart';
import 'package:yumi/shared/business_logic/models/res/country_res.dart';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
import 'package:yumi/shared/business_logic/models/res/room_user_card_res.dart';
import 'package:yumi/shared/business_logic/models/res/sc_user_identity_res.dart';

class SocialChatUserProfileManager extends ChangeNotifier {
  SCUserProfileCmd? editUser;

  void updateUserAvatar(String avatar) {
    editUser ??= SCUserProfileCmd();
    editUser!.setUserAvatar = avatar;
    notifyListeners();
  }

  void updateUserSex(num sex) {
    editUser ??= SCUserProfileCmd();
    editUser!.setUserSex = sex;
  }

  void updateUserNickname(String nickname) {
    editUser ??= SCUserProfileCmd();
    editUser!.setUserNickname = nickname;
  }

  void updateBornYear(num bornYear) {
    editUser ??= SCUserProfileCmd();
    editUser!.setBornYear = bornYear;
  }

  void updateBornMonth(num bornMonth) {
    editUser ??= SCUserProfileCmd();
    editUser!.setBornMonth = bornMonth;
  }

  void updateBornDay(num bornDay) {
    editUser ??= SCUserProfileCmd();
    editUser!.setBornDay = bornDay;
  }

  void updateAge(num age) {
    editUser ??= SCUserProfileCmd();
    editUser!.setAge = age;
  }

  void setCountry(Country country) {
    editUser ??= SCUserProfileCmd();
    editUser!.setCountryCode = country.alphaTwo!;
    editUser!.setCountryId = country.id!;
    editUser!.setCountryName = country.countryName!;
  }

  void resetEditUserData() {
    editUser = null;
    notifyListeners();
  }

  void syncCurrentUserProfile(SocialChatUserProfile? profile) {
    if (profile == null) {
      return;
    }
    userProfile = profile;
    final currentUser = AccountStorage().getCurrentUser();
    if (currentUser != null) {
      currentUser.setUserProfile(profile);
      AccountStorage().setCurrentUser(currentUser);
    }
    notifyListeners();
  }

  Future fetchUserProfileData({
    bool loadGuardCount = true,
    bool refreshFamilyData = false,
  }) async {
    var us = AccountStorage().getCurrentUser();
    String userId = us?.userProfile?.id ?? "";
    if (userId.isEmpty) {
      return;
    }
    var userInfo = await SCAccountRepository().loadUserInfo(userId);
    syncCurrentUserProfile(userInfo);
  }

  SCUserIdentityRes? userIdentity;

  void getUserIdentity() async {
    userIdentity = await SCAccountRepository().userIdentity();
    notifyListeners();
  }

  ///用户信息详情个人页
  SocialChatUserProfile? userProfile;

  void getUserInfoById(String userId) async {
    userProfile = null;
    userProfile = await SCAccountRepository().loadUserInfo(userId);

    notifyListeners();
  }

  ///房间资料卡
  RoomUserCardRes? userCardInfo;

  // List<UserCountGuardRes>? userCountGuardResList;

  void roomUserCard(String roomId, String userId) async {
    userCardInfo = null;
    userCardInfo = await SCChatRoomRepository().roomUserCard(roomId, userId);
    notifyListeners();
  }

  void followUser(String userId) async {
    var result = await SCAccountRepository().followUser(userId);
    if (result) {
      if (userCardInfo != null) {
        userCardInfo = userCardInfo?.copyWith(follow: !userCardInfo!.follow!);
        notifyListeners();
      }
    }
  }

  double myBalance = 0.0;

  void balance() async {
    myBalance = await SCAccountRepository().balance();
    notifyListeners();
  }

  updateBalance(double m) {
    myBalance = m;
    notifyListeners();
  }

  void inviteHostOpt(BuildContext ct, String id, String status) async {
    try {
      await SCAccountRepository().inviteHost(id, status);
      getUserIdentity();
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }

  void inviteAgentOpt(BuildContext ct, String id, String status) async {
    try {
      await SCAccountRepository().inviteAgent(id, status);
      getUserIdentity();
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }

  void inviteBDOpt(BuildContext ct, String id, String status) async {
    try {
      await SCAccountRepository().inviteBD(id, status);
      getUserIdentity();
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }

  void inviteBDLeader(BuildContext ct, String id, String status) async {
    try {
      await SCAccountRepository().inviteBDLeader(id, status);
      getUserIdentity();
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }

  void inviteRechargeAgent(BuildContext ct, String id, String status) async {
    try {
      await SCAccountRepository().inviteRechargeAgent(id, status);
      getUserIdentity();
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }

  void cpRlationshipProcessApply(
    BuildContext ct,
    String id,
    bool status,
  ) async {
    try {
      await SCAccountRepository().cpRelationshipProcessApply(id, status);
      fetchUserProfileData(loadGuardCount: false);
      SCTts.show(SCAppLocalizations.of(ct)!.operationSuccessful);
    } catch (e) {}
  }
}
