package com.cesgroup.cesmonitor.web;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cesgroup.cesmonitor.bean.AlarmInfo;
import com.cesgroup.cesmonitor.common.Util;
import com.cesgroup.cesmonitor.service.AlarmListService;
import com.zabbix4j.host.HostGetResponse;
import com.zabbix4j.trigger.TriggerGetResponse;

@Controller
@RequestMapping("/alarmList")
public class AlarmListController {

    @Autowired
    AlarmListService alarmListService;


    /**
     *
     * @param groupId
     *            群组Id
     * @param hostId
     *            主机Id
     * @param objectId
     *            触发器Id
     * @param status
     *            时间状态
     * @param warningLevel
     *            事件严重性
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return
     *         当前页事件告警信息列表
     */
    @RequestMapping(value = "filterAlarmInfo")
    @ResponseBody
    // http://localhost/cesmonitor/alarmlist/filterAlarmInfo.json/listOfAlarm?groupId=9&hostId=-1&objectId=-1&status=-1&warninglevel=-1&startTime=0&endTime=0&interval=-1&page=1
    public List<AlarmInfo> filterAlarmInfo(final Integer groupId, final Integer hostId, final Integer objectId,
        final Integer status, final Integer warningLevel, Long startTime, Long endTime, final Integer interval,
        @RequestParam(value = "page", required = false) final Integer page) {
        Integer pageNum = (page == null) ? 1 : page;
        Long deadLine = Util.getDeadLine(interval);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if (interval == 24) {
            calendar.add(Calendar.HOUR_OF_DAY, -24);
            endTime = deadLine + 86400;
            startTime = calendar.getTime().getTime() / 1000 + 86400;

        } else if (interval == 30) {
            calendar.add(Calendar.DAY_OF_YEAR, -30);
            endTime = deadLine + 86400;
            startTime = calendar.getTime().getTime() / 1000 + 86400;

        } else if (interval == 6 || interval == 12) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            endTime = deadLine + 86400;
            calendar.add(Calendar.MONTH, -interval);
            startTime = calendar.getTime().getTime() / 1000;

        } else if (interval == -1) {
            startTime = (long) 0;
            endTime = (long) 0;
        }
        List<AlarmInfo> list = alarmListService.findAlarmEventInfo(groupId, hostId, objectId,
            status, warningLevel, startTime, endTime, pageNum);
        return list;
    }

    /**
     *
     * @return
     *         所有触发器
     */
    @RequestMapping(value = "getAllTriggers")
    @ResponseBody
    public List<TriggerGetResponse.Result> getAllTriggers(final Integer hostId) {
        return alarmListService.getAllTriggers(hostId);
    }

    /**
     *
     * @param groupId
     *            群组Id
     * @return
     *         某个群组的触发器
     */
    @RequestMapping(value = "getTriggersByGroupId")
    @ResponseBody
    public List<TriggerGetResponse.Result> getTriggersByGroupId(final Integer groupId) {
        return alarmListService.getTriggersByGroup(groupId);
    }

    @RequestMapping(value = "getHostByTriggerId")
    @ResponseBody
    public HostGetResponse.Result getHostByTriggerId(final Integer triggerId) {
        return alarmListService.getHostByTriggerId(triggerId);
    }
}
