package com.red.circle.other.app.command.user;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.web.i18n.I18nProperties;
import com.red.circle.other.app.dto.cmd.user.user.LanguageUploadCmd;
import com.red.circle.other.infra.database.cache.key.user.UserHashKey;
import com.red.circle.other.infra.database.cache.service.user.UserCacheService;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.service.user.user.ExpandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 上传语言.
 *
 * @author pengliang on 2022/5/27
 */
@Component
@RequiredArgsConstructor
public class UploadLanguageCmdExe {

  private final ExpandService expandService;
  private final I18nProperties i18nProperties;
  private final UserCacheService userCacheService;
  private final RoomProfileManagerService roomProfileManagerService;

  public void execute(LanguageUploadCmd cmd) {
    if (i18nProperties.containsAllowLocale(cmd.getLang())) {
      userCacheService.remove(cmd.requiredReqUserId(), UserHashKey.LANGUAGE);
      expandService.updateLastActiveTimeAndLange(cmd.requiredReqUserId(), cmd.getLang());
      roomProfileManagerService.updateUserLangCode(cmd.requiredReqUserId(), cmd.getLang());
      return;
    }
    ResponseAssert.failure(CommonErrorCode.TYPE_IS_NOT_IN_SCOPE);
  }

}
