/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/

package com.cesgroup.cesmonitor.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.TjService;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.host.HostGetRequest;
import com.zabbix4j.host.HostGetResponse;
import com.zabbix4j.trigger.TriggerGetRequest;
import com.zabbix4j.trigger.TriggerGetResponse;

/**
 * 统计
 *
 */
@Controller
@RequestMapping(value = "/tj")
public class TjController {

    @Autowired
    TjService tjService;



    @RequestMapping(value = "listProblem")
    public TriggerGetResponse listProblem(final Model model) {
        try {
            final TriggerGetRequest request = new TriggerGetRequest();
            final TriggerGetRequest.Params params = request.getParams();
            // params.addTriggerid(triggerId);
            params.setExpandDescription(true);
            params.setExpandExpression(true);
            // params.setFilter(filter);
            // params.setSelectLastEvent(ZabbixApiParamter.QUERY.extend.name());
            // params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
            // params.setSelectFunctions(ZabbixApiParamter.QUERY.extend.name());
            return Util.getLoginUser().getZabbixApi().trigger().get(request);
        } catch (final ZabbixApiException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping(value = "index")
    public String index(final Model model) {
        final HostGetRequest request = new HostGetRequest();
        final HostGetRequest.Params params = request.getParams();

        //final ArrayList<Integer> hostIds = new ArrayList<Integer>();

        params.setSelectDiscoveryRule(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectGroups(ZabbixApiParamter.QUERY.count.name());
        // params.setSelectItems(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectApplications(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectDiscoveries(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectGraphs(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectHostDiscovery(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectHttpTests(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectInterfaces(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectInventory(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectMacros(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectParentTemplates(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectScreens(ZabbixApiParamter.QUERY.extend.name());
        // params.setSelectTriggers(ZabbixApiParamter.QUERY.extend.name());

        try {
            final HostGetResponse response = Util.getLoginUser().getZabbixApi().host().get(request);
            model.addAttribute("response", response);
        } catch (final ZabbixApiException e) {
            throw new RuntimeException(e);
        }
        return "tj/index";
    }

    // /**
    // * 登录失败跳转
    // * @param userName
    // * @param model
    // * @return
    // */
    // @RequestMapping(method = RequestMethod.POST)
    // public String fail(@RequestParam(USERNAME_PARAM) String userName, Model
    // model) {
    // model.addAttribute(USERNAME_PARAM, userName);
    // return "login/login";
    // }
}
