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

import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.mcdisk.controller.app.websocket.vo.ReloginOnWwatchReqVO;
import cn.sikey.mcdisk.entity.ActivateCloudPhotoAlbumMessage;
import cn.sikey.mcdisk.entity.ReloginOnWwatchMessage;
import cn.sikey.mcdisk.enums.MessageType;
import cn.sikey.mcdisk.feign.ApiMessageFeignService;
import cn.sikey.mcdisk.service.AuthorizeService;
import cn.sikey.mcdisk.service.MessageIdIncrementingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;

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

    @Resource
    private ApiMessageFeignService apiMessageFeignService;

    @Resource
    private MessageIdIncrementingService<Long> messageIdIncrementingService;

    @Resource
    private AuthorizeService authorizeService;

    /**
     * 通知手表重新登录
     *
     * @param request 请求
     * @return
     */
    @PostMapping("/reloginWatch")
    public CommonResult notifyReloginOnWwatch(@Validated @RequestBody ReloginOnWwatchReqVO request) {
        ReloginOnWwatchMessage rt = new ReloginOnWwatchMessage();
        rt.setMsgId(messageIdIncrementingService.incr());
        rt.setMsgType(MessageType.RELOGIN_ON_WWATCH);
        rt.setSendId(request.getUserId());
        rt.setRecvId(request.getTicket());
        ReloginOnWwatchMessage.Content content = new ReloginOnWwatchMessage.Content();
        content.setTicket(request.getTicket());
        rt.setContent(content);
        rt.setSendTime(Instant.now().toEpochMilli());

        // 更新设备授权状态
        // authorizeService.updateAuthorizeByDeviceId(request.getTicket());

        return CommonResult.success(apiMessageFeignService.reloginOnWwatch(rt));
    }

    /**
     * 通知手表开通云相册服务
     *
     * @param request 请求
     * @return
     */
    @PostMapping("/activateCloudPhotoAlbum")
    public CommonResult activateCloudPhotoAlbum(@Validated @RequestBody ActivateCloudPhotoAlbumMessage request) {
        return CommonResult.success(apiMessageFeignService.activateCloudPhotoAlbum(request));
    }

}
