package org.holer.server.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.holer.server.constant.HolerType;
import org.holer.server.util.ServerMgr;
import org.holer.server.util.ServerUtil;


@Entity
@Table(name = "holer_port")
public class HolerPort implements Serializable {
    @Id
    @GeneratedValue
    private Long portId;
    private String accessKey;
    private Integer portNum;
    private String server;
    private String domain;
    private HolerType type;
    @Transient
    private String inetAddr;
    @Transient
    private Long validDay;
    private Date expireAt;
    private Date createAt;
    private Boolean enabled;

    public void setPortId(Long portId) {
        this.portId = portId;
    }

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

    public void setPortNum(Integer portNum) {
        this.portNum = portNum;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public void setType(HolerType type) {
        this.type = type;
    }

    public void setInetAddr(String inetAddr) {
        this.inetAddr = inetAddr;
    }

    public void setValidDay(Long validDay) {
        this.validDay = validDay;
    }

    public void setExpireAt(Date expireAt) {
        this.expireAt = expireAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

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

    public Long getPortId() {
        return this.portId;
    }


    public String getAccessKey() {
        return this.accessKey;
    }


    public Integer getPortNum() {
        return this.portNum;
    }


    public String getServer() {
        return this.server;
    }


    public String getDomain() {
        return this.domain;
    }


    public HolerType getType() {
        return this.type;
    }


    public Boolean getEnabled() {
        return this.enabled;
    }


    public HolerPort() {
    }


    public HolerPort(String key, Integer portNum) {
        this.accessKey = key;
        this.portNum = portNum;
    }


    public Long getValidDay() {
        if (null != this.validDay) {
            return this.validDay;
        }

        if (null == this.expireAt) {
            return null;
        }

        long time = this.expireAt.getTime() - System.currentTimeMillis();
        if (time < 0L) {

            time = 0L;
            return time;
        }
        return ServerUtil.divide(time, 86400000L);
    }


    public Long getExpireAt() {
        if (null == this.expireAt) {
            return 0L;
        }
        return this.expireAt.getTime();
    }


    public Long getCreateAt() {
        if (null == this.createAt) {
            return 0L;
        }
        return this.createAt.getTime();
    }


    public String getInetAddr() {
        return ServerMgr.getDomainName() + ":" + this.portNum;
    }
}
