package com.yihu.hos.controllers;

import com.yihu.hos.services.ESBCamelService;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
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;

/**
 * Created by lingfeng on 2016/8/4.
 */
@Controller
@RequestMapping("/esb")
public class ESBCamelController {
    @Resource(name= ESBCamelService.BEAN_ID)
    ESBCamelService esbCamelService;

    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增Processor处理器", produces = "application/json", notes = "当外界组件通知一个新的processor处理器被定义时，该事件被触发")
    public Result test() {
      return Result.success("test");
    }

    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增Processor处理器", produces = "application/json", notes = "当外界组件通知一个新的processor处理器被定义时，该事件被触发")
    public Result onProcessorAdded(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className,
            @ApiParam(name = "path", value = "class文件路径", required = true)
            @RequestParam(value = "path") String path) {
        return esbCamelService.onProcessorAdded(serviceFlow, packageName, className, path);
    }

    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "修改Processor处理器", produces = "application/json", notes = "当外界组件通知一个已有的processor处理器data部分发生变化时，该事件被触发")
    public Result onProcessorDataChanged(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className,
            @ApiParam(name = "path", value = "class文件路径", required = true)
            @RequestParam(value = "path") String path) {
        return esbCamelService.onProcessorDataChanged(serviceFlow, packageName, className, path);
    }

    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增Route路由", produces = "application/json", notes = "当外界组件通知一个新的RouteDefine路由被定义时，该事件被触发")
    public Result onRouteDefineAdded(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className,
            @ApiParam(name = "path", value = "class文件路径", required = true)
            @RequestParam(value = "path") String path) {
        return esbCamelService.onRouteDefineAdded(serviceFlow, packageName, className, path);
    }

    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "修改Route路由", produces = "application/json", notes = "当外界组件通知一个已有的RouteDefine路由定义被改变时，主要就是路由定义内容被改变时，该事件被触发")
    public Result onRouteDefineChanged(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className,
            @ApiParam(name = "path", value = "class文件路径", required = true)
            @RequestParam(value = "path") String path) {
        return esbCamelService.onRouteDefineChanged(serviceFlow, packageName, className, path);
    }

    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "当外界组件通知一个已有的RouteDefine路由定义被删除时，该事件被触发")
    public Result onRouteDefineDelete(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className) {
        return esbCamelService.onRouteDefineDelete(serviceFlow, packageName, className);
    }

    @RequestMapping(value = "/route/start", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "启动路由时，该事件被触发")
    public Result onRouteDefineStart(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow) {
        return esbCamelService.onRouteDefineStart(serviceFlow);
    }

    @RequestMapping(value = "/route/stop", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "停止路由时，该事件被触发")
    public Result onRouteDefineStop(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow) {
        return esbCamelService.onRouteDefineStop(serviceFlow);
    }
}
