/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.service</p>
 * <p>文件名:InterfaceOutgoingReportService.java</p>
 * <p>创建时间:2017年5月12日 下午1:18:30</p>
 * <p>作者:user</p>
 */

package com.cesgroup.cesmonitor.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cesgroup.cesmonitor.bean.FastGroup;
import com.cesgroup.cesmonitor.bean.FastGroupGraph;
import com.cesgroup.cesmonitor.bean.QuickAccessGraph;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.dao.QuickAccessGraphMapper;
import com.cesgroup.cesmonitor.dao.QuickAccessMapper;
import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.graph.GraphCreateRequest;
import com.zabbix4j.graph.GraphCreateResponse;
import com.zabbix4j.graph.GraphDeleteRequest;
import com.zabbix4j.graph.GraphGetRequest;
import com.zabbix4j.graph.GraphGetResponse;
import com.zabbix4j.graph.GraphItem;
import com.zabbix4j.graph.GraphObject;

/**
 * 上海中信信息发展股份有限公司; 创建时间:2018年1月22日;
 *
 * @author user
 * @version 1.0.0
 */
@Service
public class QuickAccessGraphService {

    @Autowired
    private QuickAccessGraphMapper quickAccessGraphMapper;

    @Autowired
    private QuickAccessMapper quickAccessMapper;

    /**
     * @param userId
     * @return 返回快速访问组
     */
    public List<FastGroup> findFastGroupByUser(final String userId) {
        LoginUser user = Util.getLoginUser();
        if (user.isSuperAdmin()) {
            // 超级管理员返回所有快速访问组
            return quickAccessGraphMapper.superAdminfindFastGroupByUser(userId);
        } else {
            return quickAccessGraphMapper.findFastGroupByUser(userId);
        }
    }

    /**
     * @param fastGroupId
     * @return 返回快速访问组图形
     */
    public List<FastGroupGraph> findFastGroupGraphByFastGroupId(final String fastGroupId) {
        List<QuickAccessGraph> FastGroupGraphs;
        if ("1".equals(fastGroupId)) {
            // 下拉框为所有
            LoginUser user = Util.getLoginUser();
            Integer userId = user.getUserid();
            String userId2 = userId.toString();
            if (user.isSuperAdmin()) {
                // 超级管理员返回所有图形
                return quickAccessGraphMapper.getAllGraph();
            } else {
                FastGroupGraphs = quickAccessMapper.findFastGroupGraphByUserId(userId2);

                Set<String> FastGroupIdSet = new HashSet<String>();
                String FastGroupGraphId = null;
                for (int i = 0; i < FastGroupGraphs.size(); i++) {
                    FastGroupGraphId = FastGroupGraphs.get(i).getGroupId();
                    FastGroupIdSet.add(FastGroupGraphId);
                }
                List<FastGroupGraph> listFastGroupGraph = new ArrayList<FastGroupGraph>();

                for (String GroupId : FastGroupIdSet) {
                    List<FastGroupGraph> templistFastGroupGraph;
                    templistFastGroupGraph = quickAccessMapper.findFastGraphByFastGroupId(GroupId);
                    listFastGroupGraph.addAll(templistFastGroupGraph);

                }
                return listFastGroupGraph;
            }
        } else if (null == fastGroupId) {

            // 下拉框为所有
            LoginUser user = Util.getLoginUser();
            Integer userId = user.getUserid();
            String userId2 = userId.toString();
            if (user.isSuperAdmin()) {
                // 超级管理员返回所有图形
                return quickAccessGraphMapper.getAllGraph();
            } else {
                FastGroupGraphs = quickAccessMapper.findFastGroupGraphByUserId(userId2);

                Set<String> FastGroupIdSet = new HashSet<String>();
                String FastGroupGraphId = null;
                for (int i = 0; i < FastGroupGraphs.size(); i++) {
                    FastGroupGraphId = FastGroupGraphs.get(i).getGroupId();
                    FastGroupIdSet.add(FastGroupGraphId);
                }
                List<FastGroupGraph> listFastGroupGraph = new ArrayList<FastGroupGraph>();

                for (String GroupId : FastGroupIdSet) {
                    List<FastGroupGraph> templistFastGroupGraph;
                    templistFastGroupGraph = quickAccessMapper.findFastGraphByFastGroupId(GroupId);
                    listFastGroupGraph.addAll(templistFastGroupGraph);

                }
                return listFastGroupGraph;
            }

        } else {
            LoginUser user = Util.getLoginUser();
            Integer userId = user.getUserid();
            if (user.isSuperAdmin()) {
                // 超级管理员返回所有快速访问组
                return quickAccessGraphMapper.getGraphByGroupId(fastGroupId);
            } else {
                return quickAccessGraphMapper.getGraphByGroupIdandUser(fastGroupId, userId);
            }
        }

    }

    /**
     * @param hostId
     * @return
     * @throws Exception
     */
    public List<GraphObject> listGraphByHostId(final Integer hostId) throws Exception {

        ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
        Integer targetId = createDummyGraph();
        GraphGetRequest request = new GraphGetRequest();
        GraphGetResponse response = zabbixApi.graph().get(request);
        List<com.zabbix4j.graph.GraphObject> newlist = new ArrayList<>();
        for (com.zabbix4j.graph.GraphObject rs : response.getResult()) {
            newlist.add(rs);
        }
        deleteDummy(targetId);
        return newlist;
    }

    /**
     * @return
     * @throws ZabbixApiException
     */
    private Integer createDummyGraph() throws ZabbixApiException {
        ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
        GraphCreateRequest request = new GraphCreateRequest();
        GraphCreateRequest.Params params = request.getParams();
        params.setName("Graph Delete Test");
        GraphItem gitem = new GraphItem();
        gitem.setItemid(42597);
        gitem.setColor("00AA00");
        params.addGraphItem(gitem);
        params.setHeight(800);
        params.setWidth(140);
        GraphCreateResponse response = zabbixApi.graph().create(request);
        return response.getResult().getGraphids().get(0);
    }

    /**
     * @param id
     * @throws ZabbixApiException
     */
    private void deleteDummy(final Integer id) throws ZabbixApiException {
        GraphDeleteRequest request = new GraphDeleteRequest();
        request.getParams().add(id);
    }

    /**
     * @param hostId
     * @return
     * @throws Exception
     */
    public List<GraphObject> getGraphByHostId(final Integer hostId) throws Exception {
        ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
        GraphGetRequest request = new GraphGetRequest();
        GraphGetRequest.Params params = request.getParams();
        params.addHostId(hostId);
        params.setSelectGroups(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
        GraphGetResponse response = zabbixApi.graph().get(request);
        List<com.zabbix4j.graph.GraphObject> newlist = new ArrayList<>();
        for (com.zabbix4j.graph.GraphObject rs : response.getResult()) {
            newlist.add(rs);
        }
        return newlist;
    }

    /**
     * @param graphId
     * @return
     * @throws ZabbixApiException
     */
    public List<GraphObject> getGraphByGraphId(final Integer graphId) throws ZabbixApiException {
        ZabbixApi zabbixApi = Util.getLoginUser().getZabbixApi();
        GraphGetRequest request = new GraphGetRequest();
        GraphGetRequest.Params params = request.getParams();
        params.addGraphId(graphId);
        GraphGetResponse response = zabbixApi.graph().get(request);
        Util.toJSON(response);
        List<com.zabbix4j.graph.GraphObject> newlist = new ArrayList<>();
        for (com.zabbix4j.graph.GraphObject rs : response.getResult()) {
            newlist.add(rs);
        }
        return newlist;
    }
}
