package cn.wise.sc.energy.power.plant.business.domain;

import cn.hutool.core.builder.EqualsBuilder;
import cn.hutool.core.builder.HashCodeBuilder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: qh
 * @create: 2020-08-10 22:27
 **/
@Data
@Entity
@Table(name = "userinfo")
@NoArgsConstructor
public class UserInfo extends AbstractEntity<String> implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    private String username;
    private String password;
    private Integer role;
    private String plantid;
    private String phone;
    private String name;
    private String sex;

    @Transient
    private Collection<? extends GrantedAuthority> authorities;
    @Transient
    private boolean enabled;
    @Transient
    private boolean locked;

    public UserInfo(
            String id,
            String username,
            String password, List<String> authorities,
            boolean enabled
    ) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = mapToGrantedAuthorities(authorities);
        this.enabled = enabled;
    }

    public UserInfo(
            String id,
            String username,
            String password, String authoritie,
            boolean enabled
    ) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = mapToGrantedAuthorities(authoritie);
        this.enabled = enabled;
    }

    private List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        return authorities.stream()
                .map(authority -> new SimpleGrantedAuthority(authority))
                .collect(Collectors.toList());
    }

    private List<GrantedAuthority> mapToGrantedAuthorities(String authoritie) {
        return Arrays.asList(new SimpleGrantedAuthority(authoritie));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

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


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

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

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

    @Override
    public int hashCode() {
        // 自定义userInfo hashCode方法，session管理时会用做key
        return new HashCodeBuilder(17, 37).append(getId()).append(getUsername()).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
       // 自定义equals方法
        boolean isEqual = false;
        if (obj != null && UserInfo.class.isAssignableFrom(obj.getClass())) {
            UserInfo userInfo = (UserInfo) obj;
            isEqual = new EqualsBuilder().append(getId(), userInfo.getId())
                    .append(getUsername(), userInfo.getUsername()).append(getUsername(), userInfo.getUsername()).isEquals();
        }
        return isEqual;
    }

}