package com.fdkankan.ucenter.controller.inner;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.sign.SignUtils;
import com.fdkankan.ucenter.annotation.VerifySign;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.constants.ResultCode;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.AppSecret;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.IAppSecretService;
import com.fdkankan.ucenter.service.IInnerService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.service.impl.LoginService;
import com.fdkankan.ucenter.vo.request.LaserSceneParam;
import com.fdkankan.ucenter.vo.request.SceneParam;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 内部接口控制器
 * </p>
 *
 * @author dengsixing
 * @since 2022/8/19
 **/
@RestController
@RequestMapping("/ucenter/_inner")
public class InnerController extends BaseController {

    @Autowired
    private IInnerService innerService;
    @Autowired
    IUserService userService;
    @Autowired
    LoginService loginService;
    @Autowired
    IAppSecretService appSecretService;

    /**
     * 根据场景码获取token
     */
    @GetMapping("/_token")
    @VerifySign
    public Result createTokenByNum(String num){
        return innerService.createTokenByNum(num);
    }

    /**
     * 查询场景资源路径
     */
    @GetMapping("/querySceneDataSource")
    @VerifySign
    public String querySceneDataSource(String num){
        return innerService.querySceneDataSource(num);
    }

    /**
     * 查询场景资源路径
     */
    @GetMapping("/querySceneNum")
    @VerifySign
    public Result querySceneNum(String path){
        if(ObjectUtils.isEmpty(path)){
            return Result.failure("请输入路径");
        }
        return innerService.querySceneNum(path);
    }

    /**
     * 查询场景资源路径
     */
    @GetMapping("/getAllSceneDbInfo")
    @VerifySign
    public Result getAllSceneDbInfo(String num){
        if(ObjectUtils.isEmpty(num)){
            return Result.failure("请输入场景码");
        }
        return innerService.querySceneNum(num);
    }

    /**
     * 查询场景资源路径
     */
    @GetMapping("/downloadCapture")
    @VerifySign
    public Result downloadCapture(String num){
        if(ObjectUtils.isEmpty(num)){
            return Result.failure("请输入场景号");
        }
        return innerService.downloadCapture(num);
    }
    /**
     * 根据场景码获取场景码版本 深时使用
     */
    @GetMapping("/_getSceneNumVersion")
    @VerifySign
    public Result getSceneNumVersion(@RequestParam(required = false) String num){
        if(StringUtils.isBlank(num)){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return Result.success(innerService.getSceneInfo(num));
    }

    @GetMapping(value = "/getSnCode/{snCode}")
    @VerifySign
    public Result getSnCode( @PathVariable String snCode) {
        if(StringUtils.isBlank(snCode)){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return Result.success(innerService.getCameraDetail(snCode));
    }

    @GetMapping("/getTokenByUserName/{userName}")
    @VerifySign
    public Result getTokenByUserName(@PathVariable String userName){
        User user = userService.getByUserName(userName);
        if(user == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
        }
        return Result.success(loginService.login(user));
    }

    @PostMapping(value = "/getSceneBySnCode")
    @VerifySign
    public Result getSceneSnCode( @RequestBody SceneParam param) {
        if(StringUtils.isBlank(param.getSnCode())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return Result.success(innerService.getSceneBySnCode(param));
    }

    @PostMapping(value = "/getSceneByToken")
    @VerifySign
    public Result getSceneByToken( @RequestBody LaserSceneParam param) {
        if(StringUtils.isBlank(getToken())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return Result.success(innerService.getSceneByToken(param,getToken()));
    }

    @Autowired
    RedisUtil redisUtil;
    @GetMapping(value = "/pdsfsdfsrvateddsfeky/{appId}")
    @VerifySign
    public Result getPrivateKey(@PathVariable String appId){
        AppSecret byAppId = null;
        if(redisUtil.hasKey("ucenter:sign:appid:"+appId)){
            byAppId = JSONObject.parseObject(redisUtil.get("ucenter:sign:appid:"+appId),AppSecret.class);
        }else {
            byAppId = appSecretService.getByAppId(appId);
        }
        if(!redisUtil.hasKey("ucenter:sign:appid:"+appId)){
            redisUtil.set("ucenter:sign:appid:"+appId, JSONObject.toJSONString(byAppId),60 *60 *2);
        }
        return Result.success(byAppId);
    }
}
