package org.iri_research.renkan.forms;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.security.crypto.password.PasswordEncoder;

public class UserForm extends RenkanForm<String, User> {

    private String avatar;

    @DateTimeFormat(iso = ISO.DATE)
    private LocalDate credentialsExpirationDate;
    @DateTimeFormat(iso = ISO.DATE)
    private LocalDate expirationDate;

    private String email;

    private boolean enabled;
    private boolean locked;
    private String password;
    private String passwordConfirm;
    private Set<String> groups;
    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.credentialsExpirationDate = model.getCredentialsExpirationDate();
            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>();
            this.setGroups(new HashSet<>(model.getGroups()));
        }
    }

    public String getAvatar() {
        return avatar;
    }

    public LocalDate getCredentialsExpirationDate() {
        return credentialsExpirationDate;
    }

    public String getEmail() {
        return email;
    }

    public LocalDate 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.setCredentialsExpirationDate(this.credentialsExpirationDate);
        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.getGroups() != null) {
            this.usersRepository.setGroupsList(this.model, this.getGroups());
        }
        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 setCredentialsExpirationDate(LocalDate credentialsExpirationDate) {
        this.credentialsExpirationDate = credentialsExpirationDate;
    }

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

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

    public void setExpirationDate(LocalDate 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;
    }

    public Set<String> getGroups() {
        return groups;
    }

    public void setGroups(Set<String> groups) {
        this.groups = groups;
    }

}
