package org.iri_research.renkan.forms;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.iri_research.renkan.Constants;
import org.iri_research.renkan.models.User;
import org.iri_research.renkan.repositories.IRenkanRepository;
import org.iri_research.renkan.repositories.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;

public class UserForm extends RenkanForm<String, User> {

    private String avatar;

    private Date credentialExpirationDate;
    private Date expirationDate;

    private String email;

    private boolean enabled;
    private boolean locked;
    private String password;
    private String passwordConfirm;
    private List<String> userAuthorities;

    private UsersRepository usersRepository;
    
    
    private PasswordEncoder passwordEncoder;
    
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
    
    public UserForm() {
        super();
    }
    
    public UserForm(User model) {
        super(model);
        if (model != null) {
            this.avatar = model.getAvatar();
            this.credentialExpirationDate = model.getCredentialExpirationDate();
            this.email = model.getEmail();
            this.expirationDate = model.getExpirationDate();
            this.enabled = model.isEnabled();
            this.locked = model.isLocked();
            this.userAuthorities = model.getUserAuthorities()!=null?new ArrayList<String>(model.getUserAuthorities()):new ArrayList<String>();
        }
    }

    public String getAvatar() {
        return avatar;
    }

    public Date getCredentialExpirationDate() {
        return credentialExpirationDate;
    }

    public String getEmail() {
        return email;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    @Override
    protected User getModelInstance() {
        return new User();
    }

    public String getPassword() {
        return password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    @Override
    protected IRenkanRepository<User, String> getRepository() {
        return this.usersRepository;
    }

    public UsersRepository getUsersRepository() {
        return usersRepository;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isLocked() {
        return locked;
    }

    @Override
    protected void saveToModel() {
        if (this.getId() == null || this.getId().length() == 0) {
            this.model.setId(Constants.UUID_GENERATOR.generate().toString());
        }
        this.model.setAvatar(this.avatar);
        this.model.setEmail(this.email);
        this.model.setCredentialExpirationDate(this.credentialExpirationDate);
        this.model.setExpirationDate(this.expirationDate);
        this.model.setEnabled(this.enabled);
        this.model.setLocked(this.locked);
        this.model.setUserAuthorities((this.userAuthorities!=null && !this.userAuthorities.isEmpty())?new ArrayList<String>(this.userAuthorities):null);
        if(this.password != null && this.password.length() > 0) {
            this.model.setPassword(this.passwordEncoder.encode(this.password));
        }

    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setCredentialExpirationDate(Date credentialExpirationDate) {
        this.credentialExpirationDate = credentialExpirationDate;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    @Autowired
    public void setUsersRepository(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

    public List<String> getUserAuthorities() {
        return userAuthorities;
    }

    public void setUserAuthorities(List<String> userAuthorities) {
        this.userAuthorities = userAuthorities;
    }

}
