package com.cesgroup.cesmonitor.web;

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

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 org.springframework.web.bind.annotation.ResponseBody;

import com.cesgroup.cesmonitor.bean.Cpu;
import com.cesgroup.cesmonitor.bean.Disk;
import com.cesgroup.cesmonitor.bean.HostByGroup;
import com.cesgroup.cesmonitor.bean.Memory;
import com.cesgroup.cesmonitor.bean.OneHostConfig;
import com.cesgroup.cesmonitor.bean.PriorityCount;
import com.cesgroup.cesmonitor.bean.PriorityDetail;
import com.cesgroup.cesmonitor.bean.ResponseTime;
import com.cesgroup.cesmonitor.service.AlarmListService;
import com.cesgroup.cesmonitor.service.FormViewService;
import com.cesgroup.cesmonitor.service.MapService;
import com.cesgroup.cesmonitor.service.OverviewService;
import com.cesgroup.cesmonitor.web.host.HostUtil;
import com.zabbix4j.hostgroup.HostgroupGetResponse.Result;
import com.zabbix4j.trigger.TriggerGetResponse;

/**
 * 这里是图表总览页面的控制层，调用业务层代码，会与前台进行数据的交互，包括返回的JSON，HTML代码;
 */
@Controller
@RequestMapping(value = "/formview")
public class FormViewController {

    @Autowired
    OverviewService overviewService;

    @Autowired
    MapService mapService;

    @Autowired
    FormViewService formViewService;

    @Autowired
    AlarmListService alarmListService;

    /**
     *
     * @param model
     *            模型
     * @return 前端页面
     */
    @RequestMapping(value = "index")
    public String viewall(final Model model) {
        List<Result> listHosts = formViewService.listHostGroupsWithHosts();
        model.addAttribute("listHosts", listHosts);
        model.addAttribute("groups", overviewService.listHostGroupsWithHosts());
        model.addAttribute("hostTypes", HostUtil.getHostTypes());
        return "formview/index";
    }

    /**
     *
     * @param model
     *
     * @param hostid
     *
     * @return 返回主机的Hostid到主机详细信息界面
     */
    @RequestMapping(value = "host")
    public String host(final Model model, final Integer hostid) {
        /*model.addAttribute("hostid", hostid);*/
        List<Result> listHosts = formViewService.listHostGroupsWithHosts();
        model.addAttribute("listHosts", listHosts);
        model.addAttribute("groups", overviewService.listHostGroupsWithHosts());
        model.addAttribute("hostTypes", HostUtil.getHostTypes());
        return "formview/host";
    }

    /**
     *
     * @param model
     *
     * @return 返回网络拓扑信息及主机组信息
     */
    @RequestMapping(value = "listHostGroups")
    @ResponseBody
    //localhost/cesmonitor/formview/listHostGroups
    public List<com.zabbix4j.hostgroup.HostgroupGetResponse.Result> listHostGroups() {
        return formViewService.listHostGroups();
    }

    /**
     *
     * @return 这星期每天异常数
     */
    @RequestMapping(value = "currentWeek")
    @ResponseBody
    public Map<Integer, Integer> currentWeekmap() {
        return formViewService.currentWeekExceptionData();
    }

    /**
     *
     * @param model
     *
     * @return 返回到快速访问界面
     */
    @RequestMapping(value = "quickConfig")
    public String index2(final Model model) {
        return "formview/quickConfig";
    }

    /**
     *
     * @param model
     *
     * @return 返回到快速访问展示界面
     */
    @RequestMapping(value = "quickDisplay")
    public String index(final Model model) {
        return "formview/quickDisplay";
    }

    /**
     *
     * @param model
     *
     * @return 返回到告警列表
     */
    @RequestMapping(value = "listOfAlarms")
    public String index3(final Model model) {
        return "formview/listOfAlarms";
    }

    /**
     *
     * @return 上星期每天异常数
     */
    @RequestMapping(value = "lastWeek")
    @ResponseBody
    public Map<Integer, Integer> lastWeekmap() {
        return formViewService.lastWeekExceptionData();
    }

    /**
     *
     * @return 上月每天异常数
     */
    @RequestMapping(value = "lastMonth")
    @ResponseBody
    public Map<Integer, Integer> lastMonthmap() {
        return formViewService.lastMonthExceptionData();
    }

    /**
     *
     * @return 当月每天异常数
     */
    @RequestMapping(value = "currentMonth")
    @ResponseBody
    public Map<Integer, Integer> currentMonthmap() {
        return formViewService.currentMonthExceptionData();
    }

    /**
     *
     * @return 异常告警统计图
     */
    @RequestMapping(value = "priorityCount")
    @ResponseBody
    public List<PriorityCount> listPriorityCount(final String name) {
        return formViewService.findCountByPriority(name);
    }

    /**
     *
     * @param order
     *            降序还是升序
     * @param limit
     *            取多少条
     * @return 所有主机响应时间排行
     */
    @RequestMapping(value = "responseTime")
    @ResponseBody
    public List<ResponseTime> allHostsResponseTime(final String order, final Integer limit) {
        return formViewService.allHostsResponseTime(order, limit);
    }

    /**
     *
     * @param order
     *            降序还是升序
     * @param limit
     *            取多少条
     * @return 所有主机磁盘利用率排行
     */
    @RequestMapping(value = "hostsDisk")
    @ResponseBody
    public List<Disk> allHostsDisk(final String order, final Integer limit) {
        return formViewService.allHostsDisk(order, limit);
    }

    /**
     *
     * @param order
     *            降序还是升序
     * @param limit
     *            取多少条
     * @return 所有主机CPU利用率排行
     */
    @RequestMapping(value = "hostsCpu")
    @ResponseBody
    public List<Cpu> allHostsCpu(final String order, final Integer limit) {
        return formViewService.allHostsCpu(order, limit);
    }

    /**
     *
     * @param order
     *            降序还是升序
     * @param limit
     *            取多少条
     * @return 所有主机内存利用率排行
     */
    @RequestMapping(value = "hostsMemory")
    @ResponseBody
    public List<Memory> allHostsMemory(final String order, final Integer limit) {
        return formViewService.allHostsMemory(order, limit);
    }

    /**
     *
     * @param hosts
     *            页面上保存的ID字符串
     * @return 返回用户关注的主机信息
     */
    @RequestMapping(value = "currentFocus")
    @ResponseBody
    public Map<Long, OneHostConfig> currentFocus(final String hosts) {
        return formViewService.currentFocus(hosts);
    }

    /**
     *
     * @param groupName
     *            主机群组名
     * @param hostIp
     *            主机的IP号
     * @return 返回具体的一个主机或者一个组下的所有主机
     */
    @RequestMapping(value = "hostByGroup")
    @ResponseBody
    public List<HostByGroup> findHostByGroup(final String groupName, final String hostIp) {
        return formViewService.findHostByGroup(groupName, hostIp);
    }

    /**
     *
     * @param groupName
     *            主机群组名
     * @param hostIp
     *            主机的IP号
     * @return 返回具体的一个主机或者一个组下的所有主机
     */
    @RequestMapping(value = "findHostByGroupAndSearch")
    @ResponseBody
    public List<HostByGroup> findHostByGroupIdAndSearch(final String groupId, final String hostIp) {
        return formViewService.findHostByGroupId(groupId, hostIp);
    }

    /**
     *
     * @param priority
     *            异常等级
     * @return 根据具体异常等级查找出来的信息
     */
    @RequestMapping(value = "messageByPriority")
    @ResponseBody
    public List<PriorityDetail> findMessageByPriority(final Integer priority, final String name) {
        return formViewService.findMessageByPriority(priority, name);
    }

    /**
     *
     * @param str
     *            页面保存的ID
     */
    @RequestMapping(value = "savecurrentfocusconfig")
    public void saveCurrentConfig(final String str) {
        formViewService.saveConfig(str);
    }

    /**
     * @param str
     *            页面保存的组件
     */
    @RequestMapping(value = "savepageconfig")
    public void savePageConfig(final String str) {
        formViewService.savePageCfg(str);
    }

    /**
     *
     * @return 返回文件当前关注框中的内容
     */
    @RequestMapping(value = "getcurrentfocusconfig")
    @ResponseBody
    public Map<Integer, Map<String, String>> getCurrentConfig() {
        return formViewService.getConfig();
    }

    /**
     *
     * @return 返回文件当前关注框中的内容
     */
    @RequestMapping(value = "getpageconfig")
    @ResponseBody
    public Map<Integer, String[]> getPageConfig() {
        return formViewService.getPageConfig();
    }

    /**
     *
     * @return 返回当前用户刷新时间
     */
    @RequestMapping(value = "getUserRefresh")
    @ResponseBody
    public Integer getUserRefresh(final Integer userId) {
        return formViewService.findUserRefresh(userId);
    }

    @RequestMapping(value = "getHostGroups")
    @ResponseBody
    public List<com.zabbix4j.hostgroup.HostgroupGetResponse.Result> getHostGroups() {
        return overviewService.listHostGroupsWithHosts();

    }

    @RequestMapping(value = "getHostTriggers")
    @ResponseBody
    public List<TriggerGetResponse.Result> getTriggers(final int hostId) {
        return formViewService.getTriggers(hostId);

    }

    @RequestMapping(value = "getType")
    @ResponseBody
    public List<String> getType(final String hostId) {
        return formViewService.getType(hostId);

    }

    @RequestMapping(value = "getMenu")
    @ResponseBody
    public List<String> getMenu() {
        List<String> l = new ArrayList<String>();
        l.add(String.valueOf(formViewService.getOpen()));
        return l;

    }

}