package com.ibeetl.admin.core.rbac;

import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreRole;
import com.ibeetl.admin.core.entity.CoreUser;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.collections4.CollectionUtils;

public class UserLoginInfo {

  // 用户所在机构
  List<CoreOrg> orgs;

  // 用户信息
  CoreUser user;

  // 用户默认登录机构
  CoreOrg currentOrg;

  List<CoreRole> roles;

  public List<CoreOrg> getOrgs() {

    return orgs;
  }

  public void setOrgs(List<CoreOrg> orgs) {

    this.orgs = orgs;
  }

  public CoreUser getUser() {

    return user;
  }

  public void setUser(CoreUser user) {

    this.user = user;
  }

  public CoreOrg getCurrentOrg() {

    if (Objects.isNull(this.currentOrg)) {
      Collection<CoreOrg> orgs = CollectionUtils.emptyIfNull(this.orgs);
      this.currentOrg = orgs.stream().findFirst().orElse(null);
      Long orgId = Optional.ofNullable(this.user).map(CoreUser::getOrgId).orElse(null);
      for (CoreOrg org : orgs) {
        if (Objects.equals(org.getId(), orgId)) {
          this.currentOrg = org;
          break;
        }
      }
    }
    return this.currentOrg;
  }

  public void setCurrentOrg(CoreOrg currentOrg) {

    this.currentOrg = currentOrg;
  }

  public List<CoreRole> getRoles() {

    return roles;
  }

  public void setRoles(List<CoreRole> roles) {

    this.roles = roles;
  }

}
