package com.cesgroup.cesmonitor.service;

import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.cesgroup.cesmonitor.common.JSON;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.map.FanLayout;
import com.cesgroup.cesmonitor.service.map.FanLayoutH;
import com.cesgroup.cesmonitor.service.map.Layout;
import com.cesgroup.cesmonitor.service.map.MapAutoCreate;
import com.cesgroup.cesmonitor.service.map.MapAutoCreateUpdate;
import com.cesgroup.cesmonitor.service.map.MapCreateFromHosts;
import com.cesgroup.cesmonitor.service.map.MapCreateFromHostsUpdate;
import com.cesgroup.cesmonitor.service.map.MapCreateFromITService;
import com.cesgroup.cesmonitor.service.map.MapCreateFromITServiceUpdate;
import com.cesgroup.cesmonitor.service.map.TwoColumnLayout;
import com.cesgroup.cesmonitor.service.map.UmbrellaLayout;
import com.cesgroup.cesmonitor.service.map.UmbrellaLayoutH;
import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.map.MapGetRequest;
import com.zabbix4j.map.MapGetResponse;
import com.zabbix4j.map.MapObject;

/**
 * 拓扑图服务
 *
 * @author zhang.erwei
 * @version 1.0.0 2017年5月16日
 */
@Service("mapService")
public class MapService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 根据zabbix IT服务创建拓扑图
     *
     * @param serviceId
     *            IT服务节点ID
     */
    public void createFromITService(Integer serviceId) {
        try {
            new MapCreateFromITService().createMap(serviceId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据zabbix IT服务更新拓扑图
     *
     * @param serviceId
     *            IT服务节点ID
     */
    public void createFromITServiceUpdate(Integer mapId, Integer serviceId) {
        try {
            new MapCreateFromITServiceUpdate().update(mapId, serviceId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 自动创建
     *
     * @param name
     *            拓扑图名称
     * @param coreDeviceIP
     *            核心交换机IP地址
     * @return
     */
    public void createAuto(String name, String coreDeviceIP, String community) {
        try {
            MapAutoCreate create = new MapAutoCreate();
            create.create(name, coreDeviceIP, community);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 自动更新
     *
     * @param name
     *            拓扑图名称
     * @param coreDeviceIP
     *            核心交换机IP地址
     * @return
     */
    public void createAutoUpdate(Integer mapId, String coreDeviceIP, String community) {
        try {
            MapAutoCreateUpdate update = new MapAutoCreateUpdate();
            update.update(mapId, coreDeviceIP, community);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 遍历zabbix中所有的设备，创建拓扑图
     *
     * @param name
     *            拓扑图名称
     * @param gatewayIPs
     *            网关节点列表
     * @param layoutFlag
     *            布局方式1:基本布局；1：伞型；2：扇形
     */
    public void createFromHosts(String name, List<String> gatewayIPs, int layoutFlag) {
        try {
            MapCreateFromHosts create = new MapCreateFromHosts();
            Layout layout = null;
            if (layoutFlag == 0) {
                layout = new TwoColumnLayout();
            } else if (layoutFlag == 1) {
                layout = new UmbrellaLayout();
            } else if (layoutFlag == 2) {
                layout = new FanLayout();
            } else if (layoutFlag == 3) {
                layout = new UmbrellaLayoutH();
            } else if (layoutFlag == 4) {
                layout = new FanLayoutH();
            }
            //防止空引用
            if (null != layout) {
                create.createMap(name, gatewayIPs, layout);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 遍历zabbix中所有的设备，更新拓扑图
     *
     * @param mapId
     *            拓扑图Id
     * @param gatewayIPs
     *            网关节点列表
     *
     */
    public void createFromHostsUpdate(Integer mapId, List<String> gatewayIPs) {
        try {
            MapCreateFromHostsUpdate update = new MapCreateFromHostsUpdate();
            update.update(mapId, gatewayIPs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置添加到监控总览页面拓扑图Id
     *
     * @param sysmapid
     *            拓扑图ID
     */
    public void addToOverview(Integer sysmapid) {
        JSON.saveStringToFile("" + sysmapid, new File("sysmapid.json"));
    }

    /**
     * 获得监控总览页面拓扑图Id
     *
     * @return 拓扑图Id
     */
    public Integer getOverviewSysmmapid() {
        return Integer.valueOf(JSON.getStringFromFile(new File("sysmapid.json")));
    }

    /**
     * 获得监控总览页面拓扑图对象
     *
     * @return 拓扑图对象
     */
    public MapObject getMapObject() {
        try {
            Integer sysmapid = getOverviewSysmmapid();
            MapGetRequest request = new MapGetRequest();
            MapGetRequest.Params params = request.getParams();
            params.addSysMapId(sysmapid);
            //        params.setSelectIconMap(ZabbixApiParamter.QUERY.extend.name());
            params.setSelectLinks(ZabbixApiParamter.QUERY.extend.name());
            params.setSelectSelements(ZabbixApiParamter.QUERY.extend.name());
            //        params.setSelectUrls(ZabbixApiParamter.QUERY.extend.name());
            ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
            MapGetResponse response = zabbixApi.map().get(request);
            return response.getResult().get(0);
        } catch (ZabbixApiException e) {
            throw new RuntimeException(e);
        }
    }
}
