/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.web.host</p>
 * <p>文件名:HostUtil.java</p>
 * <p>创建时间:2017年10月24日 下午5:23:33</p>
 * <p>作者:Administrator</p>
 */

package com.cesgroup.cesmonitor.web.host;

import java.util.ArrayList;
import java.util.List;

import com.cesgroup.cesmonitor.common.Constant;
import com.cesgroup.cesmonitor.common.Util;
import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.host.HostObject;
import com.zabbix4j.hostgroup.HostgroupGetRequest;
import com.zabbix4j.hostgroup.HostgroupGetResponse.Result;

/**
 *
 * @author Administrator
 * @version 1.0.0 2017年10月24日
 */
public class HostUtil {

    private ArrayList<Result> hostGroups = null;

    private static List<HostType> hostTypes = null;

    public static synchronized  List<HostType> getHostTypes() {
        if (hostTypes == null) {
            hostTypes = new ArrayList<HostType>();
            final HostType ht = new HostType();
            ht.setId(1);
            ht.setName("交换机");
            hostTypes.add(ht);
            final HostType ht1 = new HostType();
            ht1.setId(Constant.SERVER);
            ht1.setName("服务器");
            hostTypes.add(ht1);
            final HostType ht3 = new HostType();
            ht3.setId(Constant.ROUTER);
            ht3.setName("路由器");
            hostTypes.add(ht3);
            final HostType ht4 = new HostType();
            ht4.setId(Constant.MIDDLEWARE);
            ht4.setName("中间件");
            hostTypes.add(ht4);
            final HostType ht5 = new HostType();
            ht5.setId(Constant.FIREWALL);
            ht5.setName("防火墙");
            hostTypes.add(ht5);
            final HostType ht6 = new HostType();
            ht6.setId(6);
            ht6.setName("PC机");
            hostTypes.add(ht6);
        }
        return hostTypes;
    }

    public ArrayList<Result> getHostGroups() {
        if (hostGroups == null) {
            try {
                final ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
                final HostgroupGetRequest request = new HostgroupGetRequest();
                final HostgroupGetRequest.Params params = request.getParams();
                params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
                params.setWith_hosts_and_templates(true);

                hostGroups = zabbixApi.hostgroup().get(request).getResult();

            } catch (final ZabbixApiException e) {
                throw new RuntimeException(e);
            }

        }
        return hostGroups;
    }

    public List<HostObject> listHost(HostType hostType, Result hostGroup) {
        final List<HostObject> list = new ArrayList<HostObject>();
        for (final HostObject h : hostGroup.getHosts()) {
            if (hostType.getId().intValue() == h.getHost_type().intValue()) {
                list.add(h);
            }
        }
        return list;

    }

    /*   public test(){
    
        for(HostType ht: getHostTypes()){
            for(Result hg :getHostGroups()){
                for(HostObject h:listHost(ht,hg)){
    
                }
            }
        }
    }*/

}
