package com.yihu.hos.arbiter.models;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

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

/**
 * @created Airhead 2016/7/27.
 */
@Document
public class BrokerServer {
    @Id
    private String id;
    private String tenant;
    private String hostName;
    private String hostAddress;
    private int port;
    private boolean enable;
    private boolean registered;
    @Indexed(name = "updateTime_1", expireAfterSeconds = 30)
    private Date updateTime;
    private ArrayList<ServiceFlow> serviceFlows;

    public boolean isFlowOn(String routeCode) {
        if (serviceFlows == null) {
            return false;
        }

        for (ServiceFlow flow : serviceFlows) {
            if (flow.getRouteCode().equals(routeCode)) {
                return true;
            }
        }

        return false;
    }

    public String getTenant() {
        return tenant;
    }

    public void setTenant(String tenant) {
        this.tenant = tenant;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getHostAddress() {
        return hostAddress;
    }

    public void setHostAddress(String hostAddress) {
        this.hostAddress = hostAddress;
    }

    public String getURL() {
        return "http://" + hostAddress + ":" + port;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public ArrayList<ServiceFlow> getServiceFlows() {
        return serviceFlows;
    }

    public void setServiceFlows(ArrayList<ServiceFlow> serviceFlows) {
        this.serviceFlows = serviceFlows;
    }

    public void addServiceFlow(ServiceFlow serviceFlow){
        if (serviceFlows == null){
            serviceFlows = new ArrayList<>();
        }

        serviceFlows.add(serviceFlow);
    }

    public boolean isRegistered() {
        return registered;
    }

    public void setRegistered(boolean registered) {
        this.registered = registered;
    }

    static public class ServiceFlow {
        @Indexed
        String flowId;
        String routeCode;
        String type;
        Date updated;
        String tenant;

        public String getTenant() {
            return tenant;
        }

        public void setTenant(String tenant) {
            this.tenant = tenant;
        }

        public String getRouteCode() {
            return routeCode;
        }

        public void setRouteCode(String routeCode) {
            this.routeCode = routeCode;
        }

        public String getType() {
            return type;
        }

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

        public Date getUpdated() {
            return updated;
        }

        public void setUpdated(Date updated) {
            this.updated = updated;
        }

        public String getFlowId() {
            return flowId;
        }

        public void setFlowId(String flowId) {
            this.flowId = flowId;
        }
    }
}
