package vut.msa.pojos;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import vut.msa.enums.BackupSchedule;
import vut.msa.interfaces.SelectPojo;
import vut.msa.utilities.TimeInterval;

public class Client implements java.io.Serializable, SelectPojo {

    private boolean active;
    private Short id;
    private Integer version;
    private String clientId;
    private String clientName;
    private String password;
    private String type;
    private Date lastUpdate;
    private BackupDisk backupDisk;
    private Set<Backup> backups = new
            HashSet<Backup>(0);

    private ClientInformation information;

    public Client() {
        this.clientId = "";
        this.clientName = "";
        this.password = "";
        this.lastUpdate = new Date();
        this.version = 1;
        this.active = true;
        this.backupDisk = new BackupDisk();
    }

    //PROPERTY: backups
    public Set<Backup> getBackups() { return backups; }
    public void setBackups(Set<Backup> backups) { this.backups = backups; }

    //PROPERTY: id 
    public Short getId() { return id; }
    public void setId(Short id) { this.id = id; }

    //PROPERTY: information
    public ClientInformation getInformation() { return information; }
    public void setInformation(ClientInformation information) { this.information = information; }

    //PROPERTY: clientId
    public String getClientId() { return clientId; }
    public void setClientId(String clientId) { this.clientId = clientId; }

    //PROPERTY: clientName
    public String getClientName() { return clientName; }
    public void setClientName(String clientName) { this.clientName = clientName; }

    //PROPERTY: lastUpdate
    public Date getLastUpdate() { return lastUpdate; }
    public void setLastUpdate(Date lastUpdate) { this.lastUpdate = lastUpdate; }

    //PROPERTY: password
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    //PROPERTY: version
    public Integer getVersion() { return version; }
    public void setVersion(Integer version) { this.version = version; }

    //PROPERTY: type
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public void setType(BackupSchedule type) { this.type = type.getName(); }

    //PROPERTY: backupDisk
    public BackupDisk getBackupDisk() { return backupDisk; }
    public void setBackupDisk(BackupDisk backupDisk) { this.backupDisk = backupDisk; }

    /**
     * Metoda vrati pocet zaloh daneho klienta.
     *
     * @return pocet zaloh na disku daneho klienta
     */
    public int getBackupsCount() {
        if (backups != null) {
            return backups.size();
        }

        return 0;
    }

    public boolean isActive() {

        this.active = false;

        if(this.lastUpdate != null) {
            Date today = new Date();
            long diff = today.getTime() - lastUpdate.getTime();

            if(diff < TimeInterval.DAY)
                this.active = true;
        }

        return this.active;
    }

    public String getName() { return clientName; }

}
