package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.i3plus.icloud.core.sdk.ICoreDemoCloud;
import cn.estsh.i3plus.icloud.wms.sdk.IWmsDemoCloud;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.bean.BaseModelBean;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysRole;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/impp/cloud")
@Api(description="分布式服务测试")
public class DemoCloudController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoCloudController.class);

    @Value("${server.port}")
    private String serverPort;

    @Value("${eureka.instance.ip-address}")
    private String ipAddress;

    @Autowired
    ICoreDemoCloud iCoreDemoCloud;

    @Autowired
    IWmsDemoCloud iWmsDemoCloud;

    @GetMapping(value="/test")
    @ApiOperation(value="core测试",notes = "core测试")
    public BaseResultBean testCore(String test) {
        LOGGER.info("iWmsDemoCloud:{},ip:{},port:{}，【impp-core接受数据】{}",iWmsDemoCloud,ipAddress,serverPort,test);
        //return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        return BaseResultBean.buildBaseResultBean(true,"");
    }

    @GetMapping(value="/test-wms")
    @ApiOperation(value="wms测试",notes = "wms测试")
    public ResultBean testWms(String test) {
        LOGGER.info("iWmsDemoCloud:{},ip:{},port:{}，【impp-core接受数据】{}",iWmsDemoCloud,ipAddress,serverPort,test);

        BaseResultBean result = iWmsDemoCloud.testWms(test);
        if(result.isSuccess()){
            return ResultBean.success("wms返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }else{
            LOGGER.error("无法调用wms服务：{}",result.getErrorMsg());
            return ResultBean.fail("调用wms服务出错：" + result.getErrorMsg());
        }
    }

    @GetMapping(value="/test-get")
    @ApiOperation(value="测试get",notes = "测试get")
    public ResultBean testFeignGet(String test) {
        LOGGER.info("【impp-core-get接受数据】{}" ,test);
        return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PostMapping(value="/test-post")
    @ApiOperation(value="测试post",notes = "测试post")
    public ResultBean testFeignPost(String test) {
        LOGGER.info("【impp-core-post接受数据】{}" ,test);
        return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PutMapping(value="/test-put")
    @ApiOperation(value="put接受数据",notes = "put接受数据")
    public ResultBean testPut(String test,String name) {
        LOGGER.info("【impp-core-put接受数据】param1:{},param2:{}", test,name);
        return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @DeleteMapping(value="/test-delete")
    @ApiOperation(value="delete接受数据",notes = "delete接受数据")
    public ResultBean testDelete(
            @RequestBody Pager pager,
            @RequestParam("test") String test) {
        LOGGER.info("【impp-core-delete接受数据】pager:{},test:{}",pager,test);
        return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PutMapping(value="/test-cloud-put")
    @ApiOperation(value="测试cloudput",notes = "测试cloudput")
    public ResultBean putTestCloud(String test,String name) {
        LOGGER.info("【impp-core-put接受数据】test:{},name:{}",test,name);
        return ResultBean.success("返回：" + test).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PostMapping(value="/test-object")
    @ApiOperation(value="测试单对象")
    public ResultBean testObject(@RequestBody Pager pager) {
        LOGGER.info("【impp-core-object接受数据】{}" ,pager);
        return ResultBean.success("返回：" + pager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PostMapping(value="/test-map")
    @ApiOperation(value="测试get",notes = "测试get")
    public ResultBean testFeignMap(@RequestBody Map<String,String> map) {
        LOGGER.info("【impp-core-map接受数据】{}" ,map);
        return ResultBean.success("返回：" + map).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @PostMapping(value="/test-arr")
    @ApiOperation(value="测试arr",notes = "测试arr")
    public ResultBean testFeignArr(@RequestBody String[] arr) {
        LOGGER.info("【impp-core-arr接受数据】{}" ,arr);
        return ResultBean.success("返回成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(arr);
    }

    @PostMapping(value="/test-more-object")
    @ApiOperation(value="测试多对象模型",notes = "测试多对象模型")
    public ResultBean testFeignMoreObj(@RequestBody BaseModelBean<SysRole> roleBean) {
        LOGGER.info("【impp-core-more-obj接受多对象模型】objBean:{},pager:{}" ,roleBean.getObj(),roleBean.getPager());
        return ResultBean.success("返回：" + roleBean).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @Autowired
    public HttpServletRequest request;

    @PostMapping(value="/test-post-request-body")
    @ApiOperation(value="测试request-body")
    public ResultBean testPostFeignRequestBody(@RequestParam("test") String test,@RequestBody BaseModelBean<SysRole> roleBean) {
        LOGGER.info("PostMapping【param】{}" ,test);

        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println("key:"+key + "  ,value: " + value);
        }

        LOGGER.info("【测试request-body-post】objBean:{},pager:{}" ,roleBean.getObj(),roleBean.getPager());
        return ResultBean.success("返回：" + roleBean).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }

    @GetMapping(value="/test-get-request-body")
    @ApiOperation(value="测试request-body")
    public ResultBean testGetFeignRequestBody(@RequestBody BaseModelBean<SysRole> roleBean) {
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println("key:"+key + "  ,value: " + value);
        }

        LOGGER.info("【测试request-body-get】objBean:{},pager:{}" ,roleBean.getObj(),roleBean.getPager());
        return ResultBean.success("返回：" + roleBean).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }
}
