package com.fdkankan.ucenter.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.ucenter.annotation.VerifySign;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.ResultData;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.service.impl.AppCameraService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/ucenter/app/camera")
@Slf4j
public class AppCameraController {

    @Autowired
    private AppCameraService appCameraService;

    @PostMapping("/getCamerasForUser")
    @VerifySign
    public Result getCamerasForUser(@RequestBody JSONObject param ){
        String userName = param.get("userName") == null ? null : param.getString("userName");
        Integer cameraType = param.get("cameraType") == null ? 4 : param.getInteger("cameraType");
        if(cameraType == 4){
            cameraType = 1;
        }
        return Result.success(appCameraService.getCameraForUser(userName,cameraType));
    }

    /**
     * 绑定相机
     */
    @PostMapping("/bindCamera")
    @VerifySign
    public Result bindCamera(@RequestBody JSONObject param ){
        String userName = param.get("userName") == null ? null : param.getString("userName");
        String snCode = param.get("snCode") == null ? null : param.getString("snCode");

        return Result.success(appCameraService.bindCamera(userName,snCode));
    }
    /**
     * 解绑相机
     */
    @PostMapping("/unbind")
    @VerifySign
    public Result unbind(@RequestBody JSONObject param ){
        String userName = param.get("userName") == null ? null : param.getString("userName");
        String childName = param.get("childName") == null ? null : param.getString("childName");
        appCameraService.unbindCamera(userName,childName);
        return Result.success();
    }
    /**
     * 获取相机信息
     */
    @PostMapping("/getCameraInfo")
    @VerifySign
    public Result getCameraInfo(@RequestBody JSONObject param ){
        String childName = param.get("childName") == null ? null : param.getString("childName");
        String childPassword = param.get("childPassword") == null ? null : param.getString("childPassword");
        return Result.success(appCameraService.getCameraInfo(childName,childPassword));
    }

    /**
     * 用户相机信息上报 app 需要调用
     */
    @RequestMapping(value = "/uploadUserCameraInfo", method = RequestMethod.POST)
    @VerifySign
    public Result uploadUserCameraInfo(@RequestBody Map<String,String> param) throws Exception {
        if (ObjectUtils.isEmpty(param) || !param.containsKey("snCode") || !param.containsKey("cameraVersion")
                || !param.containsKey("appVersion")) {
            return Result.failure(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        try {
            appCameraService.uploadUserCameraInfo(param.get("snCode"),param.get("cameraVersion"),param.get("appVersion"));
        }catch ( Exception e){
            log.error("uploadUserCameraInfo--snCode:{},cameraVersion:{},appVersion:{},error:{}",
                    param.get("snCode"),param.get("cameraVersion"),param.get("appVersion"),e);
        }

        return Result.success();
    }

    @GetMapping("/checkCameraSpace")
    @VerifySign
    public Result checkCameraSpace(@RequestParam(required = false) String snCode,
                                   @RequestParam(required = false) String unicode){

        return Result.success( appCameraService.checkCameraSpace(snCode,unicode));
    }
}
