package cn.sikey.mcdisk.service;

import cn.sikey.framework.common.exception.ServiceException;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.mcdisk.controller.app.authorizephoto.vo.*;
import cn.sikey.mcdisk.convert.AuthorizePhotoConvert;
import cn.sikey.mcdisk.dal.dataobject.AuthorizePhotoDO;
import cn.sikey.mcdisk.dal.mysql.AuthorizePhotoMapper;
import cn.sikey.mcdisk.entity.ActivateCloudPhotoAlbumMessage;
import cn.sikey.mcdisk.enums.CodeStatusEnum;
import cn.sikey.mcdisk.enums.IsAuthorizeEnum;
import cn.sikey.mcdisk.enums.MessageType;
import cn.sikey.mcdisk.feign.ApiMessageFeignService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Objects;

/**
 * @Author: nelson
 * @Date: 2025/7/1
 * @Description: 授权相册
 */
@Service
@Slf4j
public class AuthorizePhotoServiceImpl implements AuthorizePhotoService {

    @Resource
    private AuthorizePhotoMapper authorizePhotoMapper;

    @Resource
    private AuthorizeService authorizeService;

    @Resource
    private ApiMessageFeignService apiMessageFeignService;

    @Resource
    private MessageIdIncrementingService<Long> messageIdIncrementingService;

    @Qualifier("myTaskExecutor")
    @Autowired
    private ThreadPoolTaskExecutor myTaskExecutor;

    /**
     * 授权相册
     *
     * @param appOauthPhotoReqVO 授权相册
     * @return
     */
    @Override
    public CommonResult oauthPhoto(AppOauthPhotoReqVO appOauthPhotoReqVO) {
        log.info("[授权相册]参数：{}", appOauthPhotoReqVO);

        String ticket = appOauthPhotoReqVO.getTicket();

        AuthorizePhotoDO authorizePhotoD = authorizePhotoMapper.queryOauthPhotoByDeviceId(ticket);
        if (Objects.nonNull(authorizePhotoD)) {
            throw new ServiceException(IsAuthorizeEnum.AUTHORIZED.getValue(), IsAuthorizeEnum.AUTHORIZED.getName());
        }

        authorizePhotoMapper.insertOauthPhoto(appOauthPhotoReqVO);

        log.info("[授权相册]授权成功");

        return CommonResult.success();
    }

    /**
     * 查询授权相册
     *
     * @param deviceId 设备编码
     * @return
     */
    @Override
    public QueryOauthPhotoByDeviceIdRespVO queryOauthPhotoByDeviceId(String deviceId) {
        QueryOauthPhotoByDeviceIdRespVO queryOauthPhotoByDeviceIdRespVO = new QueryOauthPhotoByDeviceIdRespVO();

        AuthorizePhotoDO authorizePhotoDO = authorizePhotoMapper.queryOauthPhotoByDeviceId(deviceId);
        if (Objects.nonNull(authorizePhotoDO)) {
            queryOauthPhotoByDeviceIdRespVO.setId(authorizePhotoDO.getId());
            queryOauthPhotoByDeviceIdRespVO.setUserId(authorizePhotoDO.getUserId());
            queryOauthPhotoByDeviceIdRespVO.setDeviceName(authorizePhotoDO.getModel());
            queryOauthPhotoByDeviceIdRespVO.setNickName(authorizePhotoDO.getNickName());
            queryOauthPhotoByDeviceIdRespVO.setAuthorizeStatus(IsAuthorizeEnum.AUTHORIZED.getValue());
        } else {
            queryOauthPhotoByDeviceIdRespVO.setAuthorizeStatus(IsAuthorizeEnum.UNAUTHORIZED.getValue());
        }

        log.info("[查询授权相册]返回数据：{}", queryOauthPhotoByDeviceIdRespVO);
        return queryOauthPhotoByDeviceIdRespVO;
    }

    /**
     * 通知手表开通云相册服务
     *
     * @param notifyWatchAuthorizationReqVO 通知手表授权
     * @return
     */
    @Override
    public void notifyWatchAuthorization(NotifyWatchAuthorizationReqVO notifyWatchAuthorizationReqVO) {
        // 设备授权

        // 推送通知设备
        ActivateCloudPhotoAlbumMessage request = new ActivateCloudPhotoAlbumMessage();
        request.setMsgId(messageIdIncrementingService.incr());
        request.setMsgType(MessageType.ACTIVATE_CLOUD_PHOTO_ALBUM_SERVICE);
        request.setSendId(notifyWatchAuthorizationReqVO.getUserId());
        request.setRecvId(notifyWatchAuthorizationReqVO.getTicket());
        request.setSendTime(Instant.now().toEpochMilli());
        ActivateCloudPhotoAlbumMessage.Content content = new ActivateCloudPhotoAlbumMessage.Content();
        content.setTicket(notifyWatchAuthorizationReqVO.getTicket());
        request.setContent(content);
        apiMessageFeignService.activateCloudPhotoAlbum(request);
        log.info("[通知手表开通云相册服务]发送推送成功：{}", request);
    }

    /**
     * 查询授权相册
     *
     * @param oauthPhotoReqVO 授权相册
     * @return
     */
    @Override
    public AuthorizePhotoRespVO queryOauthPhoto(OauthPhotoReqVO oauthPhotoReqVO) {
        AuthorizePhotoDO authorizePhotoD = authorizePhotoMapper.queryOauthPhotoByDeviceId(oauthPhotoReqVO.getTicket());
        return AuthorizePhotoConvert.INSTANCE.convertOauthRespVO(authorizePhotoD);
    }
}
