package com.fdkankan.scene.feign;

import com.aliyuncs.utils.StringUtils;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.scene.api.dto.SceneCntDTO;
import com.fdkankan.scene.api.dto.SceneInfoDTO;
import com.fdkankan.scene.api.dto.ScenePODTO;
import com.fdkankan.scene.api.dto.SceneProDTO;
import com.fdkankan.scene.entity.SceneCooperation;
import com.fdkankan.scene.entity.ScenePO;
import com.fdkankan.scene.entity.SceneProPO;
import com.fdkankan.scene.service.*;
import com.fdkankan.scene.vo.SceneInfoParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 提供计算调用
 */
@RestController
@RequestMapping("/feign/user/scene")
@Slf4j
public class UserSceenFeign {

    @Autowired
    IUserSceneService userSceneService;
    @Autowired
    ISceneDownloadLogService sceneDownloadLogService;
    @Autowired
    ISceneEditInfoService sceneEditInfoService;
    @Autowired
    ISceneService sceneService;
    @Autowired
    ISceneProService sceneProService;
    @Autowired
    ISceneResourceCameraService sceneResourceCameraService;
    @Autowired
    ISceneResourceCooperationService sceneResourceCooperationService;
    @Autowired
    ISceneCooperationService sceneCooperationService;
    @Autowired
    IFdkkLaserService fdkkLaserService;
    @Autowired
    ISceneProEditService sceneProEditService;
    @Autowired
    ISceneResourceService sceneResourceService;

    @PostMapping("/getSceneCnt")
    ResultData<SceneCntDTO> getSceneCnt(@RequestParam(value = "userId") Long userId, Long cameraId){
        SceneCntDTO sceneCntDTO = userSceneService.getSceneCnt(userId, cameraId);
        return ResultData.ok(sceneCntDTO);
    }

    @PostMapping("/getSceneNumByCameraId")
    ResultData<Integer> getSceneNumByCameraId(@RequestParam(value = "cameraId") Long cameraId,
                                              @RequestParam(value = "cameraType")Integer cameraType,
                                              @RequestParam(value = "snCode")String snCode) {
        Integer sceneNum = userSceneService.getSceneNumByCameraId(cameraId,cameraType,snCode);
        return ResultData.ok(sceneNum);
    }

    @PostMapping("/getLastSceneByCameraId")
    ResultData<SceneProDTO> getLastSceneByCameraId(@RequestParam(value = "cameraId") Long cameraId){
        return ResultData.ok(userSceneService.getLastSceneByCameraId(cameraId));
    }

    @PostMapping("/updateSceneDownloadLog")
    ResultData updateSceneDownloadLog(@RequestParam(value = "num") String num,
        @RequestParam(value = "status") Integer status, String url, String reason){
        sceneDownloadLogService.updateSceneDownloadLog(num, status, url, reason);
        return ResultData.ok();
    }

    @PostMapping("/getSceneViewInfo")
    ResultData<SceneInfoDTO> getSceneViewInfo(@RequestParam(value = "num") String num) throws Exception{
        return ResultData.ok(
            sceneEditInfoService.getSceneInfo4Download(
                SceneInfoParamVO.builder().num(num).build()));
    }
    @Autowired
    private ISceneProAppService sceneProAppService;

    @PostMapping("/lockBeyondSpaceScenes")
    ResultData lockBeyondSpaceScenes(@RequestParam(value = "usedSpace")Long usedSpace,
                                     @RequestParam(value = "totalSpace")Long totalSpace,
                                     @RequestParam(value = "cameraId")Long cameraId) throws Exception{
        sceneProAppService.lockBeyondSpaceScenes(usedSpace,totalSpace,cameraId);
        return ResultData.ok();
    }

    @PostMapping("/unlockBeyondSpaceScenes")
    ResultData unlockBeyondSpaceScenes(@RequestParam(value = "usedSpace")Long usedSpace,
                                       @RequestParam(value = "cameraId")Long cameraId) throws Exception{
        sceneProAppService.unlockBeyondSpaceScenes(usedSpace,cameraId);
        return ResultData.ok();
    }
    @PostMapping("/findAllByYesterday")
    ResultData<List<ScenePO>> findAllByYesterday() throws Exception{
        List<ScenePO> allByYesterday = sceneService.findAllByYesterday();
        List<ScenePODTO> List = new ArrayList<>();
        for (ScenePO scenePO : allByYesterday) {
            ScenePODTO scenePODTO = new ScenePODTO();
            BeanUtils.copyProperties(scenePO,scenePODTO);
            List.add(scenePODTO);
        }
        return ResultData.ok(List);
    }

    @PostMapping("/updatePayStatus")
    ResultData updatePayStatus(@RequestParam(value = "num")String num ,
                               @RequestParam(value = "payStatus")Integer payStatus){
        sceneService.updatePayStatus(num,payStatus);
        return ResultData.ok();
    }

    @PostMapping("/updateUserIdByCameraId")
    ResultData updateUserIdByCameraId(@RequestParam(value = "userId")Long userId,
                                      @RequestParam(value = "cameraId")Long cameraId) throws Exception{

        sceneService.updateUserIdByCameraId(userId,cameraId);
        sceneProService.updateUserIdByCameraId(userId,cameraId);
        return ResultData.ok();
    }

    @PostMapping("/getSceneCount")
    ResultData getSceneCount(@RequestParam(value = "cameraId")Long cameraId,
                             @RequestParam(value = "userId")Long userId ,
                             @RequestParam(value = "cameraType")Integer cameraType ) throws Exception{
        if(cameraId!=null){
            return ResultData.ok(sceneProService.getSceneCount(cameraId,null));
        }
        if(cameraType == null || cameraType != 4){
            return ResultData.ok(sceneService.getSceneCount(null,userId));
        }
        return ResultData.ok(sceneProService.getSceneCount(null,userId));
    }


    @PostMapping("/disableCooperation")
    ResultData disableCooperation(@RequestParam(value = "cameraId")Long cameraId,
                                 @RequestParam(value = "snCode")String snCode,
                                 @RequestParam(value = "cooperationUserName")String cooperationUserName){

        sceneResourceCameraService.deleteByCameraId(cameraId);
        deleteCooperation(cameraId);
        if(StringUtils.isEmpty(snCode)){
            fdkkLaserService.disableCooperation(snCode,cooperationUserName);
        }
        return ResultData.ok();
    }


    @PostMapping("/saveCooperation")
    ResultData saveCooperation(@RequestParam(value = "cameraId") Long cameraId,
                               @RequestParam(value = "resourceIds") String resourceIds,
                               @RequestParam(value = "userId") Long userId,
                               @RequestParam(value = "snCode")String snCode,
                               @RequestParam(value = "cooperationUserName")String cooperationUserName){
        sceneResourceCameraService.saveByResourceIds(cameraId,resourceIds);
        List<SceneProPO> sceneProPOS = deleteCooperation(cameraId);
        sceneCooperationService.saveByResourceIds(sceneProPOS,cameraId,resourceIds,userId);

        if(StringUtils.isEmpty(snCode)){
            fdkkLaserService.disableCooperation(snCode,cooperationUserName);
            fdkkLaserService.enableCameraCooperation(snCode,cooperationUserName);
        }
        return ResultData.ok();
    }

    private List<SceneProPO>  deleteCooperation(Long cameraId){
        List<SceneProPO> sceneProList = sceneProService.findByCameraIdPro(cameraId);
        if(!CollectionUtils.isEmpty(sceneProList)) {
            List<String> numList = sceneProList.stream().map(SceneProPO::getNum).collect(Collectors.toList());
            List<SceneCooperation> list = sceneCooperationService.getListByNums(numList);
            List<Long> cooIds = list.parallelStream().map(SceneCooperation::getId).collect(Collectors.toList());
            if(cooIds.size() >0){
                //删除相机下每个场景的关联关系
                sceneResourceCooperationService.deleteByCooperationIds(cooIds);
                sceneCooperationService.updateRecStatusList(cooIds);
            }
        }
        return sceneProList;
    }

    @PostMapping("/updateImageVersionFromSceneProEdit")
    ResultData updateImageVersionFromSceneProEdit(@RequestParam(value = "num" ) String num){
        sceneProEditService.updateImageVersionByNum(num);
        return ResultData.ok();
    }

    @PostMapping("/getSceneResourceByCameraId")
    ResultData getSceneResourceByCameraId(@RequestParam(value = "cameraId" ) Long cameraId){
        return ResultData.ok(sceneResourceService.findByCameraId(cameraId));
    }
    @PostMapping("/checkDownLoad")
    ResultData checkDownLoad(@RequestParam(value = "sceneNum") String sceneNum){
        return sceneProService.checkDownLoad(sceneNum);
    }

    @PostMapping("/findByCameraIds")
    ResultData findByCameraIds(@RequestParam(value = "cameraIds") String cameraIds,
                                      @RequestParam(value = "startTime") String startTime,
                                      @RequestParam(value = "endTime")   String endTime){
        return ResultData.ok( sceneProService.findByCameraIds(cameraIds,startTime,endTime));
    }
}

