package com.fdkankan.ucenter.controller.fire;

import com.fdkankan.common.constant.ErrorCode;
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.common.ResultData;
import com.fdkankan.ucenter.common.constants.ResultCodeMsg;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.ScenePlus;
import com.fdkankan.ucenter.entity.ScenePro;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.IScenePlusService;
import com.fdkankan.ucenter.service.ISceneProService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.service.impl.LoginService;
import com.fdkankan.ucenter.vo.ResponseScene;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@Log4j2
/**火调-绑定场景*/
@RestController
@RequestMapping("/fd/ucenter/user/scene")
public class FdUserSceneController extends BaseController {

    @Autowired
    private ISceneProService sceneProService;
    @Autowired
    private IScenePlusService scenePlusService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private IUserService userService;

    /**
     * 获取场景详情
     */
    @RequestMapping(value = "/detailList", method = RequestMethod.POST)
    public Result getSceneDetailList(@RequestBody FdRequestSceneParam param) throws Exception {
        if(StringUtils.isEmpty(getUserName())){
            throw new BusinessException(-1,"用户未登录，或token不合法！");
        }
        List<ResponseScene> sceneDetailList = new ArrayList<>();
        for (String scene : param.getSceneNumList()) {
            ResponseScene sceneDetail = sceneProService.getSceneDetail(scene);
            if(sceneDetail != null){
                sceneDetailList.add(sceneDetail);
            }
        }
        log.info("火调返回数据:{}",sceneDetailList);
        return Result.success(sceneDetailList);
    }

    @GetMapping("/getTokenByNum")
    public ResultData getTokenByNum(@RequestParam(required = false) String num ){
        if(StringUtils.isEmpty(getUserName())){
            throw new BusinessException(-1,"用户未登录，或token不合法！");
        }
        if(StringUtils.isEmpty(num)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
        }
        Long userId = null;
        ScenePlus scenePlus = scenePlusService.getByNum(num);
        if(scenePlus != null){
            userId = scenePlus.getUserId();
        }else {
            ScenePro scenePro = sceneProService.getByNum(num);
            if(scenePro != null){
                userId = scenePro.getUserId();
            }
        }
        if(userId == null){
            throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400010, ResultCodeMsg.FAILURE_MSG_400010);
        }
        User user = userService.getById(userId);
        if(user == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
        }
        return ResultData.ok(loginService.login(user));
    }
}
