package com.its.op.entity.its.oper;

import com.its.op.dto.its.oper.TbWwwMemberDto;
import com.its.utils.AES256Util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.binary.Base64;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

/**
 * 웹 사용자 정보 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("웹 사용자 정보")
@Entity
@Table(name = "TB_WWW_MEMBER")
public class TbWwwMember implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("이메일주소")  // N VARCHAR2(50)
    @Id
    @Column(name = "EMAIL", nullable = false, length = 50)
    private String email;

    @ApiModelProperty("이름")  // Y VARCHAR2(15)
    @Column(name = "NAME", length = 15)
    private String name;

    @ApiModelProperty("비밀번호")  // Y VARCHAR2(100)
    @Column(name = "PWD", length = 100)
    private String pwd;

    @ApiModelProperty("연락처")  // Y VARCHAR2(30)
    @Column(name = "CONTACT_NUM", length = 30)
    private String contactNum;

    @ApiModelProperty("권한등급")  // Y VARCHAR2(3)
    @Column(name = "USER_AUTH", length = 3)
    private String userAuth;

    @ApiModelProperty("비밀번호힌트질문('HNT')")  // Y VARCHAR2(7)
    @Column(name = "HINT_QUES", length = 7)
    private String hintQues;

    @ApiModelProperty("비밀번호힌트질문답")  // Y VARCHAR2(100)
    @Column(name = "HINT_ANS", length = 100)
    private String hintAns;

    @ApiModelProperty("로그인 실패 횟수")  // Y NUMBER(2)
    @Column(name = "LOGIN_FAIL_COUNT", columnDefinition = "NUMBER", length = 2)
    private Integer loginFailCount;

    @ApiModelProperty("계정잠금여부(Y:계정잠김)")  // Y CHAR(1)
    @Column(name = "IS_ACCOUNT_LOCK", columnDefinition = "CHAR", length = 1)
    private String isAccountLock;

    @ApiModelProperty("관리자계정접속가능아이피(*:모두가능)")  // Y VARCHAR2(200)
    @Column(name = "IP_ADDRESS", length = 200)
    private String ipAddress;

    @ApiModelProperty("등록시각")  // Y VARCHAR2(14)
    @Column(name = "REG_DT", length = 14)
    private String regDt;

    @ApiModelProperty("삭제여부(Y:삭제됨)")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    public String getPassword() {
        return this.pwd;
    }

    public void setPassword(String password) {
        if (!this.pwd.equals(password)) {
            this.pwd = encodePassword(password);
        }
    }
    public String encodePassword(String password) {
        String encodePassword = "";
        try {
            AES256Util aes = new AES256Util(AES256Util.DEFAULT_KEY);
            byte[] data = aes.aesEncode(this.pwd);
            //encodePassword = new String(data);
            encodePassword = new String(Base64.encodeBase64(data));
        } catch (UnsupportedEncodingException e) {
            encodePassword = this.pwd;
        } catch (Exception e) {
            encodePassword = this.pwd;
        }
        return encodePassword;
    }

    public void setAccountLockFree() {
        this.loginFailCount = 0;
        this.isAccountLock = "N";
    }

    public TbWwwMemberDto toDto() {
        return TbWwwMemberDto.builder()
                .email(this.email)
                .name(this.name)
                .pwd(this.pwd)
                .contactNum(this.contactNum)
                .userAuth(this.userAuth)
                .hintQues(this.hintQues)
                .hintAns(this.hintAns)
                .loginFailCount(this.loginFailCount)
                .isAccountLock(this.isAccountLock)
                .ipAddress(this.ipAddress)
                .regDt(this.regDt)
                .delYn(this.delYn)
                .build();
    }

    public TbWwwMember(String email) {
        this.email = email;
    }

    public void updateInfo(TbWwwMemberDto.TbWwwMemberUpdReq req) {
        this.name = req.getName();
        this.pwd = req.getPwd();
        setPassword(req.getPwd());
        this.contactNum = req.getContactNum();
        this.userAuth = req.getUserAuth();
        this.hintQues = req.getHintQues();
        this.hintAns = req.getHintAns();
        this.loginFailCount = req.getLoginFailCount();
        this.isAccountLock = req.getIsAccountLock();
        this.ipAddress = req.getIpAddress();
        this.regDt = req.getRegDt();
        this.delYn = req.getDelYn();
    }

}
