package com.its.op.security;

import com.its.op.dto.its.oper.TbUserInfrDto;
import com.its.op.entity.its.oper.TbUserInfr;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 사용자정보 Entity Class
 */
@Slf4j
@Data
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@EqualsAndHashCode(of = "userId")
public class UserInfrVo implements UserDetails {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("사용자ID")  // N VARCHAR(20)
    private String userId;
    @ApiModelProperty("암호")  // Y VARCHAR(64)
    private String pwd;
    @ApiModelProperty("성명")  // Y VARCHAR(30)
    private String name;
    @ApiModelProperty("회사")  // Y VARCHAR(50)
    private String comp;
    @ApiModelProperty("전화번호")  // Y VARCHAR(128)
    private String tel;
    @ApiModelProperty("주소")  // Y VARCHAR(200)
    private String addr;
    @ApiModelProperty("이동전화")  // Y VARCHAR(128)
    private String mobile;
    @ApiModelProperty("이메일")  // Y VARCHAR(128)
    private String email;
    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    private String delYn;

    @ApiModelProperty("등록일자")  // Y VARCHAR(14)
    private String rgstymd;
    @ApiModelProperty("수정일자")  // Y VARCHAR(14)
    private String crctymd;

    @ApiModelProperty("힌트질문")  // Y VARCHAR(7)
    private String hintQues;
    @ApiModelProperty("힌트질문답")  // Y VARCHAR(100)
    private String hintAns;

    @ApiModelProperty("그룹ID")  // Y VARCHAR(30)
    private String gropId;
    @ApiModelProperty("운영시스템ID")  // Y VARCHAR(7)
    private String operSystId;

    public TbUserInfrDto toDto() {
        return TbUserInfrDto.builder()
                .userId(this.userId)
                .pwd(this.pwd)
                .name(this.name)
                .comp(this.comp)
                .tel(this.tel)
                .mobile(this.mobile)
                .email(this.email)
                .delYn(this.delYn)
                .hintQues(this.hintQues)
                .hintAns(this.hintAns)
                .gropId(this.gropId)
                .operSystId(this.operSystId)
                .build();
    }
    public TbUserInfr toEntity() {
        return TbUserInfr.builder()
                .userId(this.userId)
                .pwd(this.pwd)
                .name(this.name)
                .comp(this.comp)
                .tel(this.tel)
                .mobile(this.mobile)
                .email(this.email)
                .delYn(this.delYn)
                .hintQues(this.hintQues)
                .hintAns(this.hintAns)
                .gropId(this.gropId)
                .operSystId(this.operSystId)
                .build();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
        if (StringUtils.equalsIgnoreCase("GROPMANAGER", this.gropId)) {
            roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        else if (StringUtils.equalsIgnoreCase("GROPSYSOP", this.gropId)) {
            roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        else {
            roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        //log.error("getAuthorities: {}", roles.toString());
        return roles;
    }

    @Override
    public String getPassword() {
        //log.error("getPassword: {}", this.pwd);
        WebPasswordEncoder be = new WebPasswordEncoder();
        String pwd = be.encode(this.pwd);
        //log.error("getPassword: {}, encoded.", this.pwd);
        return pwd;
    }

    @Override
    public String getUsername() {
//        log.error("{}", Thread.currentThread().getStackTrace()[0].toString());
//        log.error("{}", Thread.currentThread().getStackTrace()[1].toString());
//        log.error("{}", Thread.currentThread().getStackTrace()[2].toString());
//        log.error("{}", Thread.currentThread().getStackTrace()[3].toString());
        //log.error("getUsername: {}, {}.{}", this.userId, Thread.currentThread().getStackTrace()[2].getClassName(), Thread.currentThread().getStackTrace()[2].getMethodName());
        return this.userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        //log.error("isAccountNonExpired");
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        //log.error("isAccountNonLocked");
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        //log.error("isCredentialsNonExpired");
        return true;
    }

    @Override
    public boolean isEnabled() {
        //log.error("isEnabled: {}", StringUtils.equalsIgnoreCase("N", this.delYn));
        return StringUtils.equalsIgnoreCase("N", this.delYn);
    }

}
