package cn.sikey.mcdisk.controller.app.authorizephoto;

import cn.hutool.json.JSONUtil;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.mcdisk.controller.app.authorizephoto.vo.AppOauthPhotoReqVO;
import cn.sikey.mcdisk.controller.app.authorizephoto.vo.AuthorizePhotoRespVO;
import cn.sikey.mcdisk.controller.app.authorizephoto.vo.NotifyWatchAuthorizationReqVO;
import cn.sikey.mcdisk.controller.app.authorizephoto.vo.OauthPhotoReqVO;
import cn.sikey.mcdisk.controller.app.wristwatch.vo.AuthorizeRespVO;
import cn.sikey.mcdisk.entity.GetPhoneNumberRequest;
import cn.sikey.mcdisk.entity.GetPhoneNumberResponse;
import cn.sikey.mcdisk.enums.IsAuthorizeEnum;
import cn.sikey.mcdisk.enums.RateLimit;
import cn.sikey.mcdisk.feign.ApiChildFeignService;
import cn.sikey.mcdisk.service.AuthorizePhotoService;
import cn.sikey.mcdisk.service.AuthorizeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author: nelson
 * @Date: 2025/5/29
 * @Description: 云相册
 */
@RestController
@RequestMapping("/mcdisk/app/authorizePhoto")
@Slf4j
public class AppAuthorizePhotoController {

    @Resource
    private AuthorizePhotoService authorizePhotoService;

    @Resource
    private AuthorizeService authorizeService;

    @Resource
    private ApiChildFeignService apiChildFeignService;

    /**
     * 查询授权认证
     *
     * @param oauthPhotoReqVO 查询授权认证
     * @return
     */
    // @RateLimit(key = "queryAuthorization", type = RateLimit.LimitType.GLOBAL)
    @GetMapping("/queryPhotoAuthorization")
    public CommonResult queryPhotoAuthorization(@Validated OauthPhotoReqVO oauthPhotoReqVO) {
        String ticket = oauthPhotoReqVO.getTicket();
        GetPhoneNumberRequest getPhoneNumberRequest = new GetPhoneNumberRequest().setTicket(ticket);
        log.info("[查询授权认证]请求参数：{}，组装后的请求参数：{}",JSONUtil.toJsonStr(oauthPhotoReqVO),JSONUtil.toJsonStr(getPhoneNumberRequest));
        GetPhoneNumberResponse getPhoneNumberResponse = apiChildFeignService.getPhoneNumber(getPhoneNumberRequest);
        String phoneNumber = StringUtils.EMPTY;
        if(Objects.nonNull(getPhoneNumberResponse)) {
            phoneNumber = getPhoneNumberResponse.getPhoneNumber();
        }

        AuthorizePhotoRespVO authorizePhotoRespVO = authorizePhotoService.queryOauthPhoto(oauthPhotoReqVO);
        if (Objects.isNull(authorizePhotoRespVO)) {
            AuthorizePhotoRespVO respVO = new AuthorizePhotoRespVO();
            respVO.setLoginStatus(IsAuthorizeEnum.UNAUTHORIZED.getValue());
            respVO.setAuthorize(IsAuthorizeEnum.UNAUTHORIZED.getValue());

            respVO.setChildPhoneNumber(phoneNumber);
            return CommonResult.success(respVO);
        } else {
            authorizePhotoRespVO.setAuthorize(IsAuthorizeEnum.AUTHORIZED.getValue());
            AuthorizeRespVO queryAuthorize = authorizeService.queryAuthorize(ticket);
            log.info("[查询授权认证] 设备授权表，ticket：{}，返回数据：{}", ticket, JSONUtil.toJsonPrettyStr(queryAuthorize));
            if (Objects.nonNull(queryAuthorize)) {
                authorizePhotoRespVO.setLoginStatus(queryAuthorize.getAuthorizeStatus());
            } else {
                authorizePhotoRespVO.setLoginStatus(IsAuthorizeEnum.UNAUTHORIZED.getValue());
            }

            authorizePhotoRespVO.setChildPhoneNumber(phoneNumber);
            log.info("[查询授权认证] 返回数据：{}", JSONUtil.toJsonPrettyStr(authorizePhotoRespVO));
                return CommonResult.success(authorizePhotoRespVO);
        }
    }

    /**
     * 授权相册
     *
     * @param appOauthPhotoReqVO 相册认证
     * @return
     */
    @RateLimit(key = "photoAuthorization", type = RateLimit.LimitType.GLOBAL)
    @PostMapping("/photoAuthorization")
    public CommonResult photoAuthorization(@Validated @RequestBody AppOauthPhotoReqVO appOauthPhotoReqVO) {
        authorizePhotoService.oauthPhoto(appOauthPhotoReqVO);

        NotifyWatchAuthorizationReqVO notifyWatchAuthorizationReqVO = new NotifyWatchAuthorizationReqVO();
        notifyWatchAuthorizationReqVO.setTicket(appOauthPhotoReqVO.getTicket());
        notifyWatchAuthorizationReqVO.setUserId(appOauthPhotoReqVO.getUserId());
        authorizePhotoService.notifyWatchAuthorization(notifyWatchAuthorizationReqVO);
        return CommonResult.success();
    }

    /**
     * 通知手表开通云相册服务
     *
     * @param notifyWatchAuthorizationReqVO 通知手表开通云相册服务
     * @return
     */
    @RateLimit(key = "notifyWatchActivateService", type = RateLimit.LimitType.GLOBAL)
    @PostMapping("/notifyWatchActivateService")
    public CommonResult notifyWatchAuthorization(@Validated @RequestBody NotifyWatchAuthorizationReqVO notifyWatchAuthorizationReqVO) {
        authorizePhotoService.notifyWatchAuthorization(notifyWatchAuthorizationReqVO);
        return CommonResult.success();
    }

}
