package com.red.circle.other.app.dto.cmd.room;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.inner.enums.live.RoomUserRolesEnum;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 成员角色变更.
 *
 * @author pengliang on 2022-03-22
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class RoomMemberIdentityCmd extends AppExtCommand {

  /**
   * 房间id.
   *
   * @eo.required
   */
  @NotNull(message = "roomId required.")
  private Long roomId;

  /**
   * 角色.
   *
   * @eo.required
   */
  @NotNull(message = "userType required.")
  private RoomUserRolesEnum userType;

  /**
   * 变更用户.
   *
   * @eo.required
   */
  @NotNull(message = "changeUserId required.")
  private Long changeUserId;

  /**
   * 操作人.
   *
   * @eo.required
   */
  @NotNull(message = "operatorUserId required.")
  private Long operatorUserId;

  /**
   * 操作原因.
   *
   * @eo.required
   */
  @NotNull(message = "reason required.")
  private RoomChangeRolesReason reason;

  public RoomUserRolesEnum getRoles() {
    return userType;
  }

  public boolean isOperationSelf() {
    return Objects.equals(operatorUserId, changeUserId);
  }

  public boolean allowOperationSelf() {
    return reason.isOneselfOperation();
  }

  public boolean requestUserExists() {
    return Objects.equals(requiredReqUserId(), operatorUserId) ||
        Objects.equals(requiredReqUserId(), changeUserId);
  }

  public boolean checkHomeowner() {
    return Objects.equals(RoomUserRolesEnum.HOMEOWNER, userType);
  }

  public boolean checkTourist() {
    return Objects.equals(RoomUserRolesEnum.TOURIST, userType);
  }

  public boolean checkAdmin() {
    return Objects.equals(RoomUserRolesEnum.ADMIN, userType);
  }

  public String lockKey() {
    return getRoomId() + "_" + getChangeUserId();
  }

  public enum RoomChangeRolesReason {

    /**
     * 操作自己-加入.
     */
    ONESELF_JOIN,

    /**
     * 操作自己-退出.
     */
    ONESELF_EXIT,

    /**
     * 操作他人: 管理员/房主直接变更已存在成员列表用户、用户发出申请、管理员和房主向用户发出申请.
     */
    OTHER_CHANGE;


    public boolean isOneselfOperation() {
      return Objects.equals(this, ONESELF_JOIN) || Objects.equals(this,
          ONESELF_EXIT);
    }

  }


}
