package com.fdkankan.ucenter.controller.api;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.annotation.VerifySign;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.ResultData;
import com.fdkankan.ucenter.entity.ScenePlus;
import com.fdkankan.ucenter.entity.ScenePlusExt;
import com.fdkankan.ucenter.entity.ScenePro;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.*;
import com.fdkankan.ucenter.vo.LaserSceneVo;
import com.fdkankan.ucenter.vo.response.CameraAppVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 激光使用接口
 */
@RestController
@RequestMapping("/ucenter/laser")
public class LaserController extends BaseController {

    @Autowired
    ISceneProService sceneProService;
    @Autowired
    IScenePlusService scenePlusService;
    @Autowired
    IScenePlusExtService scenePlusExtService;
    @Autowired
    ICameraService cameraService;
    @Autowired
    IUserService userService;
    @Autowired
    ICameraDetailService cameraDetailService;

    /**
     * 根据手机号码获取 相机 sncode
     */
    @RequestMapping(value = "/getSnCodeByPhone",method = RequestMethod.GET)
    @VerifySign
    public Result getSnCodeByUserName(@RequestParam(required = false)String phone,
                                      @RequestParam(required = false,defaultValue = "4")String sceneSource) throws Exception {
        if(StringUtils.isBlank(phone)){
            String token = getToken();
            phone = JwtUtil.getUsername(token);
        }
        if(StringUtils.isBlank(phone)){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }

        List<Long> userIds = userService.getLikeUserName(phone);
        HashMap<String,Object> map = new HashMap<>();
        Set<String> snCodeSet = new HashSet<>();
        map.put("phone",phone);
        map.put("snCodes",snCodeSet);
        if(userIds.size() <=0){
            return Result.success(map);
        }
        List<CameraAppVo> camera = new ArrayList<>();
        if(sceneSource.contains(",")){
            String[] split = sceneSource.split(",");
            for (String resource : split) {
                if("3".equals(resource)){   //看见
                    camera.addAll(cameraDetailService.getListByUserIdsAndType(userIds, 9));
                }
                if("4".equals(resource)){   //深时
                    camera.addAll( cameraDetailService.getListByUserIdsAndType(userIds, 10));
                }
                if("5".equals(resource)){   //深光
                    camera.addAll(cameraDetailService.getListByUserIdsAndType(userIds, 11));
                }
            }
        }
        if("3".equals(sceneSource)){
            camera = cameraDetailService.getListByUserIdsAndType(userIds, 9);
        }
        if("4".equals(sceneSource)){
            camera = cameraDetailService.getListByUserIdsAndType(userIds, 10);
        }
        if("5".equals(sceneSource)){
            camera = cameraDetailService.getListByUserIdsAndType(userIds, 11);
        }
        if(camera.size() >0){
            snCodeSet = camera.stream().map(CameraAppVo::getSnCode).collect(Collectors.toSet());
            map.put("snCodes",snCodeSet);
        }
        return Result.success(map);
    }

    /**
     * 获取场景数据
     */
    @RequestMapping(value = "/getSceneByNum",method = RequestMethod.GET)
    @VerifySign
    public Result getSceneByNum(@RequestParam(required = false)String num) throws Exception {
        ScenePro sceneProEntity = sceneProService.getByNum(num);
        LaserSceneVo vo = new LaserSceneVo();
        Long userId = null;
        if(sceneProEntity!= null){
            vo.setNum(sceneProEntity.getNum());
            userId = sceneProEntity.getUserId();
            vo.setShootCount(sceneProEntity.getShootCount());
        }
        ScenePlus scenePlus = scenePlusService.getByNum(num);
        if(scenePlus != null){
            vo.setNum(scenePlus.getNum());
            vo.setTitle(scenePlus.getTitle());
            vo.setCreateTime(scenePlus.getCreateTime());
            userId = scenePlus.getUserId();
            ScenePlusExt byPlusId = scenePlusExtService.getByPlusId(scenePlus.getId());
            if(byPlusId !=null){
                vo.setShootCount(byPlusId.getShootCount());
                vo.setAlgorithmTime(byPlusId.getAlgorithmTime());
            }
        }
        if(userId != null){
            User user = userService.getById(userId);
            if(user!=null){
                vo.setUserPhone(user.getUserName());
                vo.setUserId(user.getId());
            }
        }
        return Result.success(vo);
    }

    @Value("${4dkk.laserService.basePath}")
    private String basePath;
    @RequestMapping(value = "/getSsDomain",method = RequestMethod.GET)
    public ResultData getSsDomain(){
        return ResultData.ok(basePath);
    }
}
