package com.icomsys.main_vm.db.jpa.entity.system;

import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.system.vo.SystemBotUserUpdateReq;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Table(name = "TB_BOT_USER")
@Entity
@Getter
@Setter
@NoArgsConstructor
@DynamicUpdate
public class TbBotUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_SEQ", nullable = false)
    private Long userSeq;
    @Column(name = "CUST_CODE", nullable = false, length = 20)
    private String custCode;
    @Column(name = "LAST_USE_SERVICE_GROUP", nullable = false, length = 20)
    private String lastUseServiceGroup;
    @Column(name = "ROLE_VAL", nullable = false, length = 100)
    private String roleVal;
    @Column(name = "USER_ID", nullable = false, length = 20)
    private String userId;
    @Column(name = "PASSWORD", nullable = false, length = 200)
    private String password;
    @Column(name = "USER_NAME", nullable = false, length = 20)
    private String userName;
    @Column(name = "E_MAIL", nullable = true, length = 50)
    private String eMail;
    @Column(name = "MOBILE_NO", nullable = true, length = 15)
    private String mobileNo;
    @Column(name = "LOGIN_CHECK", nullable = false, length = 10)
    private int loginCheck;
    @Column(name = "USE_YN", nullable = false, length = 1)
    private String useYn;
    @Column(name = "REGIST_ID", nullable = false, length = 20)
    private String registId;
    @Column(name = "REGIST_DATE", nullable = false)
    private LocalDateTime registDate;
    @Column(name = "UPDATE_ID", nullable = true, length = 20)
    private String updateId;
    @Column(name = "UPDATE_DATE", nullable = true)
    private LocalDateTime updateDate;


    @Builder
    public TbBotUser(Long userSeq, String custCode, String lastUseServiceGroup, String roleVal, String userId, String password, String userName, String eMail, String mobileNo, int loginCheck, String useYn, String registId, LocalDateTime registDate, String updateId, LocalDateTime updateDate) {
        this.userSeq = userSeq;
        this.custCode = custCode;
        this.lastUseServiceGroup = lastUseServiceGroup;
        this.roleVal = roleVal;
        this.userId = userId;
        this.password = password;
        this.userName = userName;
        this.eMail = eMail;
        this.mobileNo = mobileNo;
        this.loginCheck = loginCheck;
        this.useYn = useYn;
        this.registId = registId;
        this.registDate = registDate;
        this.updateId = updateId;
        this.updateDate = updateDate;
    }


    public LoginVO toLoginVO() {
        LoginVO re = new LoginVO();
        re.setUserId(this.userId);
        re.setUserName(this.userName);
        re.setPassword(this.password);
        re.seteMail(this.eMail);
        re.setSiteCode(this.custCode);
        re.setSystemUserId(this.userSeq.toString());
        re.setAuthCode(this.roleVal);
        re.setLoginCheck(this.loginCheck);
        re.setUpdate_date(this.updateDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        return re;
    }

    public UserVo toUserVO() {
        UserVo uv = new UserVo();
        uv.setUserSeq(this.userSeq);
        uv.setCustCode(this.custCode);
        uv.setLastUseServiceGroup(this.lastUseServiceGroup);
        uv.setRoleVal(this.roleVal);
        uv.setUserId(this.userId);
        uv.setUserName(this.userName);
        uv.setEMail(this.eMail);
        uv.setMobileNo(this.mobileNo);
        uv.setLoginCheck(this.loginCheck);
        uv.setUseYn(this.useYn);
        uv.setRegistId(this.registId);
        uv.setRegistDate(this.registDate);
        uv.setUpdateId(this.updateId);
        uv.setUpdateDate(this.updateDate);
        return uv;
    }


    public TbBotUser resetLoginFailCheck() {
        this.loginCheck = 0;
        return this;
    }

    public TbBotUser loginFailCheck() {
        this.loginCheck += 1;
        return this;
    }

    public TbBotUser useYn(String use) {
        this.useYn = use;
        return this;
    }

    public TbBotUser updateLastService(String lastService) {
        this.lastUseServiceGroup = lastService;
        return this;
    }

    public TbBotUser updatePassword(String pass) {
        this.password = pass;
        this.updateDate = LocalDateTime.now();
        return this;
    }



    public TbBotUser toUpdate(SystemBotUserUpdateReq dto, UserVo uv, PasswordEncoder pe) {
        if (this.roleVal != dto.getRoleVal()) {
            this.roleVal = dto.getRoleVal();
        }
        if (dto.getUserId() != null && !dto.getUserId().equals("") && this.userId != dto.getUserId()) {
            this.userId = dto.getUserId();
        }
        if (dto.getUserPass() != null && !dto.getUserPass().equals("")) {
            this.password = pe.encode(dto.getUserPass());
        }
        if (dto.getUserName() != null && !dto.getUserName().equals("") && this.userName != dto.getUserName()) {
            this.userName = dto.getUserName();
        }
        if (dto.getEmail() != null && !dto.getEmail().equals("") && this.eMail != dto.getEmail()) {
            this.eMail = dto.getEmail();
        }
        if (dto.getMobileNo() != null && !dto.getMobileNo().equals("") && this.mobileNo != dto.getMobileNo()) {
            this.mobileNo = dto.getMobileNo();
        }
        if (dto.getUseYn() != null && !dto.getUseYn().equals("") && this.useYn != dto.getUseYn()) {
            this.useYn = dto.getUseYn();
        }
        this.loginCheck = dto.getLoginCheck();
        this.updateId = uv.getUserId();
        this.updateDate = LocalDateTime.now();
        return this;
    }
}
