package com.inspect.ivs.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.redis.service.RedisService;
import com.inspect.ivs.base.feign.domain.IvsResult;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.service.IvsCommonService;
import com.inspect.ivs.service.IvsResourceRetryableDelegate;
import com.inspect.ivs.util.UriUtils;
import com.inspect.ivs.view.IvsPresetListView;
import com.inspect.ivs.view.IvsPresetView;
import com.inspect.ivs.vo.IvsPresetExVo;
import com.inspect.ivs.vo.IvsVo;
import com.inspect.ivs.vo.IvsPresetVo;
import com.inspect.ivs.vo.IvsPtzVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;

import java.io.IOException;

@RestController
@RequestMapping({"/api/v1/control"})
public class IvsControlController {
    private static final Logger log = LoggerFactory.getLogger(IvsControlController.class);

    private final RedisService redisService;
    private final IvsCommonService ivsCommonService;
    private final IvsResourceRetryableDelegate ivsResourceRetryableDelegate;
    @Value("${ivs.version:3800}")
    private String version;

    public IvsControlController(
            RedisService redisService,
            IvsCommonService ivsCommonService, IvsResourceRetryableDelegate ivsResourceRetryableDelegate) {
        this.redisService = redisService;
        this.ivsCommonService = ivsCommonService;
        this.ivsResourceRetryableDelegate = ivsResourceRetryableDelegate;
    }

    @GetMapping({"ptz"})
    public Response ptz(IvsPtzVo ivsPtzVo) {
        ivsPtzVo.setControlPara1("2");
        log.info("[IVS] PTZ VO: {}", ivsPtzVo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cameraCode", ivsPtzVo.getCameraCode());
        jsonObject.put("controlCode", Integer.parseInt(ivsPtzVo.getControlCode()));
        jsonObject.put("controlPara1", ivsPtzVo.getControlPara1());
        jsonObject.put("controlPara2", ivsPtzVo.getControlPara2());
        IvsResult ivsResult = ivsCommonService.postJson(ivsResourceRetryableDelegate.getLabel(), jsonObject, IvsConst.URI_PTZ_CONTROL);
        if (!IvsConst.HTTP_STATUS_OK.equals(ivsResult.getResultCode())) {
            return Response.fail("摄像机不支持该操作");
        }
        log.debug("[IVS] PTZ END");
        return Response.ok();
    }

    @GetMapping({"preset"})
    public Response preset(IvsPresetVo ivsPresetVo) {
        //log.debug("[IVS] PRESET CMD: {}\n VO: {}", ivsPresetVo.getCommand(), ivsPresetVo);
        String command = ivsPresetVo.getCommand();
        if (command.equals("set")) {
            JSONObject paramJsonPreAdd = new JSONObject();
            paramJsonPreAdd.put("cameraCode", ivsPresetVo.getCameraCode());
            paramJsonPreAdd.put("presetName", ivsPresetVo.getName());
            IvsPresetView ivsPresetView = ivsCommonService.postJson(ivsResourceRetryableDelegate.getLabel(), paramJsonPreAdd, IvsConst.URI_PRESET_SET, IvsPresetView.class);
            log.debug("[IVS] PRESET SET RES: {}", ivsPresetView);
            if (!IvsConst.HTTP_STATUS_OK.equals(ivsPresetView.getResultCode())) {
                return Response.fail("摄像机不支持该操作");
            }
            return Response.ok(ivsPresetView);
        } else if (command.equals("update")) {
            JSONObject paramJsonPreUpdate = new JSONObject();
            paramJsonPreUpdate.put("cameraCode", ivsPresetVo.getCameraCode());
            JSONObject ptzPresetInfo = new JSONObject();
            ptzPresetInfo.put("presetIndex", Integer.parseInt(ivsPresetVo.getPreset()));
            ptzPresetInfo.put("presetName", ivsPresetVo.getName());
            paramJsonPreUpdate.put("ptzPresetInfo", ptzPresetInfo);
            IvsResult ivsResult = ivsCommonService.putJson(ivsResourceRetryableDelegate.getLabel(), paramJsonPreUpdate, IvsConst.URI_PRESET_SET, IvsResult.class);
            log.debug("[IVS] PRESET UPDATE RES :{}", ivsResult);
            redisService.setCacheMapValue(IvsConst.IVS_PRESET_MAP, ivsPresetVo.getPreset(), ivsPresetVo.getPreset());
            return Response.ok();
        } else if (command.equals("goto")) {
            String uuid = ivsResourceRetryableDelegate.getLabel();
            log.info("URI_PTZ_CONTROL GOTO UUID: {}, CameraCode: {}", uuid, ivsPresetVo.getCameraCode());
            ivsPresetVo.setUUID(uuid);
            return ivsResourceRetryableDelegate.gotoSnapshotPresetRetryable(version, ivsPresetVo);
        } else if (command.equals("remove")) {
            String code = ivsPresetVo.getCode();
            String[] codeArray = code.split(StringUtils.HASHTAG);
            String cameraCode = codeArray[0];
            String domainCode = codeArray[1];
            JSONObject paramJsonRemove = new JSONObject();
            paramJsonRemove.put("cameraCode", cameraCode);
            paramJsonRemove.put("domainCode", domainCode);
            paramJsonRemove.put("presetIndex", Integer.parseInt(ivsPresetVo.getPreset()));
            ivsCommonService.delete(ivsResourceRetryableDelegate.getLabel(), UriUtils.parseForReplace(IvsConst.URI_PRESET_REMOVE, paramJsonRemove));
            return Response.ok();
        } else if (command.equals("correction")) {
            return Response.ok();
        } else {
            return Response.ok();
        }
    }

    @GetMapping({"presetEx"})
    public Response presetEx(IvsPresetExVo ivsPresetVo) {
        String command = ivsPresetVo.getCommand();
        log.info("PRESET_EX param: {}, command: {}", ivsPresetVo, command);
        if (command.equals("correction")) {
            String uuid = ivsResourceRetryableDelegate.getLabel();
            log.info("PRESET_EX CORRECTION UUID: {}, CameraCode: {}",
                    uuid,
                    ivsPresetVo.getCameraCode());
            ivsPresetVo.setUUID(uuid);
            return ivsResourceRetryableDelegate.gotoSnapshotPresetExRetryable(version, ivsPresetVo);
        } else if (command.equals("correctionSet")) {
            JSONObject ptzPresetInfo = new JSONObject();
            ptzPresetInfo.put("presetIndex", Integer.parseInt(ivsPresetVo.getPreset()));
            ptzPresetInfo.put("presetName", ivsPresetVo.getName());

            JSONObject paramJsonPreSet = new JSONObject();
            paramJsonPreSet.put("cameraCode", ivsPresetVo.getCode());
            paramJsonPreSet.put("ptzPresetInfo", ptzPresetInfo);
            log.info("PRESET_EX CORRECTION_SET cameraCode: {}, preset: {}, name: {}, paramBody: {}",
                    ivsPresetVo.getCameraCode(),
                    ivsPresetVo.getPreset(),
                    ivsPresetVo.getName(),
                    paramJsonPreSet.toJSONString());
            IvsPresetView ivsPresetView = ivsCommonService.putJson(ivsResourceRetryableDelegate.getLabel(),
                    paramJsonPreSet,
                    IvsConst.URI_PRESET_SET,
                    IvsPresetView.class);
            log.info("PRESET_EX CORRECTION_SET RES: {}", ivsPresetView);
            return Response.ok(ivsPresetView);
        }else {
            return Response.ok();
        }
    }

    @GetMapping({"presetList"})
    public IvsPresetListView presetList(IvsVo ivsVo) {
        final String apiAddr = ivsResourceRetryableDelegate.getApiAddress(ivsVo);
        log.info("GET_PRESET_LIST PARAM: {}, apiAddr: {}", ivsVo, apiAddr);
        IvsPresetListView ivsPresetListView =
                ivsCommonService.get(ivsResourceRetryableDelegate.getLabel(),
                        apiAddr,
                        UriUtils.parseForReplace(IvsConst.URI_PRESET_LIST, ivsVo),
                        IvsPresetListView.class);
        log.debug("[IVS] END");
        return ivsPresetListView;
    }
}
