package org.holer.server.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.channel.Channel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.validator.constraints.Length;


@Entity
@Table(name = "holer_client")
public class HolerClient implements Serializable {
    private static final long serialVersionUID = -6902829762010304678L;
    @Id
    @GeneratedValue
    private Long clientId;
    @NotBlank(message = "不允许为空")
    @Email(message = "邮箱格式错误")
    @Length(min = 6, max = 64, message = "长度必须在6-64之间")
    private String name;
    private String accessKey;
    private Boolean enabled;
    private Integer status;
    @Transient
    @JsonIgnore
    private Date onlineAt;
    @Transient
    @JsonIgnore
    private Channel channel;

    @Transient
    private List<HolerPort> ports = new ArrayList<>();


    public HolerClient() {
    }
    public HolerClient(String key) {
        this.accessKey = key;
    }

    public Long getClientId() {
        return clientId;
    }

    public void setClientId(Long clientId) {
        this.clientId = clientId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public Boolean getEnabled() {
        return enabled;
    }

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

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getOnlineAt() {
        return onlineAt;
    }

    public void setOnlineAt(Date onlineAt) {
        this.onlineAt = onlineAt;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public List<HolerPort> getPorts() {
        return ports;
    }

    public void setPorts(List<HolerPort> ports) {
        this.ports = ports;
    }

    public List<Integer> publicPorts() {
        List<Integer> ports = new ArrayList<Integer>();
        if (CollectionUtils.isEmpty(this.ports)) {
            return ports;
        }
        for (HolerPort port : this.ports) {
            ports.add(port.getPortNum());
        }
        return ports;
    }
}
