package com.yihu.hos.monitor.controller;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.monitor.service.ServiceMonitorService;
import com.yihu.hos.system.service.FlowManager;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/26.
 */
@Controller("ServiceMonitorController")
@RequestMapping("/monitor/service")
public class ServiceMonitorController {
    private static Logger logger = LoggerFactory.getLogger(ServiceMonitorController.class);

    @Autowired
    private ServiceMonitorService monitorService;

    @Resource(name = FlowManager.BEAN_ID)
    private FlowManager flowManager;

    //跳转到列表页
    @RequestMapping("/initial")
    public String serviceInitial(Model model) {
        model.addAttribute("contentPage", "/monitor/service/sEnvManage");
        return "partView";
    }


    @RequestMapping(value = "/metrics", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务指标", produces = "application/json", notes = "获取服务指标")
    public Result metrics(
            HttpServletRequest request,
            @ApiParam(name = "id", value = "serviceID", required = true)
            @RequestParam(value = "id") String id,
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        try {
            HttpSession session = request.getSession();
            TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
            return monitorService.serviceMetrics(tenantSession.getTenant(), id, beginTime, endTime);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return Result.error("获取服务指标失败");
        }
    }

    @RequestMapping(value = "/trees", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务树列表", produces = "application/json", notes = "获取服务树列表")
    public Result trees() {
        try {
            return monitorService.getServiceTreeList();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取服务树列表失败");
        }
    }


    @RequestMapping(value = "/serviceStatus", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "服务状态", produces = "application/json", notes = "服务状态获取")
    public Result serviceStatus(
            HttpServletRequest request,
            @ApiParam(name = "routeCode", value = "流程Code", required = true)
            @RequestParam(value = "routeCode") Integer routeCode) {
        try {
            boolean succ = flowManager.serviceOpenOrPause(routeCode, 0);
            if (succ) {
                return Result.success("获取服务状态成功");
            } else {
                return Result.error("获取服务状态失败");
            }
        } catch (Exception e) {
            return Result.error("获取服务状态异常");
        }
    }


    @RequestMapping(value = "/startService", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "开启服务", produces = "application/json", notes = "开启服务")
    public Result startService(
            HttpServletRequest request,
            @ApiParam(name = "flowId", value = "流程ID", required = true)
            @RequestParam(value = "flowId") Integer flowId) {
        try {
            boolean succ = flowManager.serviceOpenOrPause(flowId, 1);
            if (succ) {
                return Result.success("开启服务成功");
            } else {
                return Result.error("开启服务失败");
            }
        } catch (Exception e) {
            return Result.error("开启服务异常");
        }
    }

    @RequestMapping(value = "/stopService", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "暂停服务", produces = "application/json", notes = "暂停服务")
    public Result stopService(
            HttpServletRequest request,
            @ApiParam(name = "flowId", value = "流程ID", required = true)
            @RequestParam(value = "flowId") Integer flowId) {
        try {
            boolean succ = flowManager.serviceOpenOrPause(flowId, 0);
            if (succ) {
                return Result.success("暂停服务成功");
            } else {
                return Result.error("暂停服务失败");
            }
        } catch (Exception e) {
            return Result.error("暂停服务异常");
        }
    }

}
