package com.fdkankan.ucenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.constants.ResultCode;
import com.fdkankan.ucenter.entity.Camera;
import com.fdkankan.ucenter.exception.BusinessException;
import com.fdkankan.ucenter.service.ICameraDetailService;
import com.fdkankan.ucenter.service.ICameraService;
import com.fdkankan.ucenter.service.IScenePlusService;
import com.fdkankan.ucenter.service.ISceneProService;
import com.fdkankan.ucenter.service.impl.AppSceneService;
import com.fdkankan.ucenter.vo.request.AppSceneParam;
import com.fdkankan.ucenter.vo.request.DeviceListParam;
import com.fdkankan.ucenter.vo.request.SceneParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备中心接口
 */
@Log4j2
/**设备中心接口-场景模块*/
@RestController
@RequestMapping("/ucenter/device/scene")
public class DeviceSceneController extends BaseController {

    @Autowired
    ISceneProService sceneProService;
    @Autowired
    ICameraService cameraService;


    /**
     * 获取相机场景记录
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getScenes(@RequestBody SceneParam param) throws Exception {
        param.setHasFolder(0);
        if(StringUtils.isNotBlank(param.getSearchKey())){
            param.setSceneName(param.getSearchKey());
        }
        String username = JwtUtil.getUsername(getToken());
        if(StringUtils.isBlank(username)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        Camera camera = cameraService.getBySnCode(username);
        if(camera == null){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        param.setCameraId(camera.getId());
        JSONObject data = sceneProService.newList(param, null);
        JSONObject pageInfo = data.getJSONObject("pageInfo");
        pageInfo.put("sceneNum",pageInfo.getInteger("total"));
        return Result.success(pageInfo);
    }


    /**
     * 删除场景
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result deleteScene(@RequestBody AppSceneParam param) throws Exception {
        param.setCameraType(null);
        sceneProService.delete(param.getSceneNum(),getUserId(),null);
        return Result.success();
    }


}
