package com.inspect.patrol.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.utils.HttpClientUtils;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.patrol.contants.CascadeApiConstant;
import com.inspect.patrol.domain.ResponseResult;
import com.inspect.patrol.linkage.util.JsonUtil;
import com.inspect.patrol.service.impl.Video2NvrComponent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(
        tags = {"级联配置"}
)
@RestController
@RequestMapping({"/cascade"})
public class CascadeController extends BaseController {
    @Value("${gwb.url}")
    private String cascadeUrl;
    private final Video2NvrComponent video2NvrComponent;

    public CascadeController(Video2NvrComponent video2NvrComponent) {
        this.video2NvrComponent = video2NvrComponent;
    }

    @GetMapping({"list"})
    @ApiOperation("查询级联列表")
    public ResponseResult list(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.LIST, JsonUtil.json2Str(params));
        return ResponseResult.success(JSONObject.parseObject(result));
    }

    @GetMapping({"save"})
    @ApiOperation("级联保存")
    public ResponseResult add(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.SAVE, JsonUtil.json2Str(params));
        return ResponseResult.success(result);
    }

    @GetMapping({"info"})
    public ResponseResult info(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.INFO, JsonUtil.json2Str(params));
        return ResponseResult.success(JSONObject.parseObject(result));
    }

    @GetMapping({"channellist"})
    public ResponseResult channelList(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.CHANNEL_LIST, JsonUtil.json2Str(params));
        return ResponseResult.success(JSONObject.parseObject(result));
    }

    @GetMapping({"remove"})
    public ResponseResult remove(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.REMOVE, JsonUtil.json2Str(params));
        return ResponseResult.success(result);
    }

    @GetMapping({"savechannels"})
    public ResponseResult saveChannel(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String str = JsonUtil.json2Str(params);
        String s = str.replaceAll("\\[\\d+\\]", "[]");
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.SAVE_CHANNEL, s);
        return ResponseResult.success(result);
    }

    @GetMapping({"removechannels"})
    public ResponseResult removeChannel(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String str = JsonUtil.json2Str(params);
        String s = str.replaceAll("\\[\\d+\\]", "[]");
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.REMOVE_CHANNEL, s);
        return ResponseResult.success(result);
    }

    @GetMapping({"setshareallchannel"})
    public ResponseResult setShareChannel(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.SET_SHARE, JsonUtil.json2Str(params));
        return ResponseResult.success(result);
    }

    @GetMapping({"setchannelid"})
    public ResponseResult setChannelId(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        map.put("serial", this.video2NvrComponent.toNvrCode(map.get("serial")));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.SET_CHANNEL_ID, JsonUtil.json2Str(params));
        return ResponseResult.success(result);
    }

    @GetMapping({"pushcatalog"})
    public ResponseResult pushCatAlog(@RequestParam Map<String, String> map) throws Exception {
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(map));
        String result = HttpClientUtils.get(this.cascadeUrl + CascadeApiConstant.PUSH_CATALOG, JsonUtil.json2Str(params));
        return ResponseResult.success(result);
    }
}
