package com.fdkankan.ucenter.controller.fire;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.AppConstant;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.constant.CameraConstant;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.Camera;
import com.fdkankan.ucenter.entity.CameraDetail;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.ICameraDetailService;
import com.fdkankan.ucenter.service.ICameraService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.vo.request.CameraDetailParam;
import com.fdkankan.ucenter.vo.response.CameraVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Log4j2
/**火调-绑定相机*/
@RestController
@RequestMapping("/fd/ucenter/user/camera")
public class FdUserCameraController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICameraService cameraService;

    @Autowired
    private ICameraDetailService cameraDetailService;

    /**
     * 用户添加设备
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addCamera(@RequestBody JSONObject param) throws Exception {
        if(ObjectUtils.isEmpty(param.getString("snCode"))){
            throw new BusinessException(-1,"缺少必要参数");
        }
        String snCode = param.getString("snCode");
        String userName = param.getString("userName");
        String platform = param.getString("platform");
        if(StringUtils.isEmpty(userName)){
            userName = getUserName();
        }
        Camera cameraEntity = cameraService.getBySnCode(snCode);
        if(ObjectUtils.isEmpty(cameraEntity)){
            throw new BusinessException(CameraConstant.FAILURE_CODE_6020,CameraConstant.FAILURE_MSG_6020);
        }
        CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
        if(ObjectUtils.isEmpty(cameraDetailEntity)){
            throw new BusinessException(CameraConstant.FAILURE_CODE_6020,CameraConstant.FAILURE_MSG_6020);
        }
        if (ObjectUtils.isEmpty(cameraDetailEntity.getCompanyId()) ) {
            throw new BusinessException(CameraConstant.FAILURE_CODE_6005, CameraConstant.FAILURE_MSG_6005);
        }
        if(!StringUtils.isEmpty(platform) && "fusion".equals(platform) && !cameraDetailEntity.getCompanyId().equals(25L)){
            throw new BusinessException(CameraConstant.FAILURE_CODE_6005, CameraConstant.FAILURE_MSG_6005);
        }
        if(!StringUtils.isEmpty(platform) && "sp".equals(platform) && !cameraDetailEntity.getCompanyId().equals(26L)){
            throw new BusinessException(CameraConstant.FAILURE_CODE_6005, CameraConstant.FAILURE_MSG_6005);
        }

        if(cameraDetailEntity.getUserId() != null){
            User user = userService.getByUserName(userName);
            if(user != null){
                if(cameraDetailEntity.getUserId().equals(user.getId())){
                    return Result.success();
                }
            }
            throw new BusinessException(AppConstant.FAILURE_CODE_4011, AppConstant.FAILURE_MSG_4011);
        }

        // 绑定相机
        cameraService.bind(cameraDetailEntity.getType(),cameraEntity.getSnCode(), userName);

        return Result.success();
    }

    /**
     * 获取用户设备
     * @return
     */
    @PostMapping(value = "/details")
    public Result detail(@RequestBody CameraDetailParam param) {
        if(StringUtils.isEmpty(param.getChildName()) && param.getChildNames().size() <=0){
            return Result.success(new ArrayList<>());
        }
        if(param.getChildNames().size() <=0){
            param.setChildNames(Arrays.asList(param.getChildName()));
        }
        List<CameraVo> list = new ArrayList<>();
        for (String childName : param.getChildNames()) {
            CameraVo vo = userService.findCameraDetailByChildName(getToken(), childName);
            if(vo == null){
                continue;
            }
            vo.setUsedSpace(null);
            vo.setTotalSpace(null);
            list.add(vo);
        }
        return Result.success(list);
    }
}
