package com.dbnt.sims.userInfo.model;

import com.dbnt.sims.authMgt.model.AccessConfig;
import com.dbnt.sims.authMgt.model.ApprovalConfig;
import com.dbnt.sims.config.BaseModel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@Entity
@NoArgsConstructor
@DynamicInsert
@DynamicUpdate
@Table(name = "user_info")
public class UserInfo extends BaseModel implements UserDetails{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "user_seq")
  private Integer userSeq;
  @Column(name = "dic_code")
  private String dicCode;
  @Column(name = "user_id")
  private String userId;
  @Column(name = "passwd")
  private String password;
  @Column(name = "user_nm")
  private String userNm;
  @Column(name = "birth_date")
  private LocalDate birthDate;
  @Column(name = "sex")
  private String sex;
  @Column(name = "email")
  private String email;
  @Column(name = "phone_no")
  private String phoneNo;
  @Column(name = "area_cd")
  private String areaCd;
  @Column(name = "login_chk")
  private String loginChk;
  @Column(name = "fail_cnt")
  private Integer failCnt;
  @Column(name = "user_role")
  private String userRole;
  @Column(name = "user_status")
  private String userStatus;
  @Column(name = "og_cd")
  private String ogCd;
  @Column(name = "ofc_cd")
  private String ofcCd;
  @Column(name = "title_cd")
  private String titleCd;
  @Column(name = "wrt_dt")
  private LocalDateTime wrtDt;
  @Column(name = "group_cd")
  private String groupCd;
  @Column(name = "series_cd")
  private String seriesCd;
  @Column(name = "ofc_head_yn")
  private String ofcHeadYn;
  @Column(name = "hiring_cd")
  private String hiringCd;
  @Column(name = "employ_cd")
  private String employCd;
  @Column(name = "outturn_cd")
  private String outturnCd;
  @Column(name = "work_cd")
  private String workCd;
  @Column(name = "job_in_cd")
  private String jobInCd;
  @Column(name = "language_cd")
  private String languageCd;
  @Column(name = "police_in_date")
  private LocalDate policeInDate;
  @Column(name = "organ_in_date")
  private LocalDate organInDate;
  @Column(name = "ofc_in_date")
  private LocalDate ofcInDate;
  @Column(name = "title_in_date")
  private LocalDate titleInDate;

  @Transient
  private String modifyPassword;
  @Transient
  private String positionName;
  @Transient
  private String departmentName;

  @Transient
  private List<AccessConfig> accessConfigList;
  @Transient
  private List<ApprovalConfig> approvalConfigList;

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    Set<GrantedAuthority> roles = new HashSet<>();
    for (String role : userRole.split(",")) {
        roles.add(new SimpleGrantedAuthority(role));
    }
    return roles;
  }

  @Override
  public String getUsername() {
      return userId;
  }

  @Override
  public boolean isAccountNonExpired() {
      return true;
  }

  @Override
  public boolean isAccountNonLocked() {
      return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
      return true;
  }

  @Override
  public boolean isEnabled() {
      return userStatus.equals("USC003");
  }

  @Override
  public String toString() {
    return "UserInfo [userSeq=" + userSeq + ", userId=" + userId + ", password=" + password + ", userNm=" + userNm
        + ", ogCd=" + ogCd + ", ofcCd=" + ofcCd + ", userRole=" + userRole + ", userStatus=" + userStatus
        + ", wrtDt=" + wrtDt + ", modifyPassword=" + modifyPassword + ", positionName=" + positionName
        + ", departmentName=" + departmentName + ", accessConfigList=" + accessConfigList + ", approvalConfigList="
        + approvalConfigList + "]";
  }
}
