package com.red.circle.other.inner.model.cmd.user.account;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.other.inner.enums.user.AuthTypeEnum;
import com.red.circle.other.inner.model.cmd.user.UserProfileCmd;
import com.red.circle.tool.core.text.StringUtils;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 创建账号.
 *
 * @author pengliang on 2023/5/22
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CreateAccountCmd extends AppExtCommand {

  /**
   * 邀请人.
   */
  private String invitePeople;

  /**
   * 邀请活动邀请码.
   */
  private String invitationCode;

  /**
   * 注册类型:MOBILE、FACEBOOK、GOOGLE、APPLE、Snapchat.
   */
  private String type = AuthTypeEnum.GOOGLE.name();

  /**
   * 开放id.
   */
  private String openId;

  /**
   * 基本信息.
   */
  @NotNull(message = "profile required.")
  private UserProfileCmd profile;

  /**
   * 手机号码注册.
   */
  private MobileAuthCmd mobile;

  /**
   * 用户邮箱.
   */
  private String email;

  /**
   * 居住地址.
   */
  private String residentialAddress;

  public Integer profileUserSex() {
    return profile.getUserSex();
  }

  public String profileNickname() {
    return profile.getUserNickname();
  }

  public String profileAvatar() {
    return profile.getUserAvatar();
  }

  public String getType() {
    return StringUtils.isBlank(type) ? AuthTypeEnum.GOOGLE.name() : type;
  }

  public void setType(String type) {
    this.type = StringUtils.isBlank(type) ? AuthTypeEnum.GOOGLE.name() : type;
  }

  public boolean checkTypeMobile() {
    return Objects.equals(getType(), AuthTypeEnum.MOBILE.name());
  }

  public boolean checkChina() {
    if (Objects.equals(invitePeople, "CY1101")) {
      return false;
    }

    if (StringUtils.equalsIgnoreCase(profile.getCountryCode(), "CN")
        || StringUtils.equalsIgnoreCase(profile.getCountryCode(), "CHN")) {
      return true;
    }

    return Objects.nonNull(getMobile())
        && Objects.equals(getMobile().getPhonePrefix(), 86);
  }

  public void changeOpenId() {
    if (!this.checkTypeMobile()) {
      ResponseAssert.isTrue(ResponseErrorCode.REQUEST_PARAMETER_ERROR,
          StringUtils.isNotBlank(this.openId)
              && !Objects.equals(this.openId, "null"));
    }
    setOpenId(this.openId.concat("_").concat(requireReqSysOrigin()));
  }

  public String registerOpenId() {
    return checkTypeMobile() ? this.mobile.getFullPhoneNumber() : this.openId;
  }

}
