package com.sikey.wa04.business.application.service.message;

import com.sikey.wa04.business.application.converter.MessageConverter;
import com.sikey.wa04.business.domain.adapter.MessageAdapter;
import com.sikey.wa04.business.domain.entity.message.Message;
import com.sikey.wa04.business.infrastructure.adapter.RabbitMQMessaging;
import com.sikey.wa04.business.infrastructure.adapter.RedisMessageIdIncrementingAdapter;
import com.sikey.wa04.common.dto.header.GenericManufacturerHeader;
import com.sikey.wa04.common.dto.message.RemoteControlRebootNoticeMessage;
import com.sikey.wa04.common.dto.message.RemoteControlRestoreFactorySettingsNoticeMessage;
import com.sikey.wa04.common.dto.message.RemoteControlShutdownNoticeMessage;
import com.sikey.wa04.common.enums.MessageType;
import com.sikey.wa04.common.interceptor.CurrentRequestUser;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;

@Slf4j
@Service
public class RemoteControlService {

    @Resource
    private RedisMessageIdIncrementingAdapter messageIdIncrementingAdapter;

    @Resource
    private MessageAdapter messageAdapter;

    @Resource
    private RabbitMQMessaging messageQueueMessaging;

    public void shutdown(GenericManufacturerHeader header, String cid) {
        RemoteControlShutdownNoticeMessage noticeMessage = new RemoteControlShutdownNoticeMessage();
        noticeMessage.setMsgId(messageIdIncrementingAdapter.incr())
                .setMsgType(MessageType.REMOTE_CONTROL_SHUTDOWN_NOTICE_MESSAGE)
                .setSendId(CurrentRequestUser.get().getId())
                .setSendTime(Instant.now().toEpochMilli())
                .setRecvId(cid);
        noticeMessage.setContent(new RemoteControlShutdownNoticeMessage.RemoteControlShutdownNotice());

        Message message = MessageConverter.formMessaging(noticeMessage);
        messageAdapter.save(message);
        messageQueueMessaging.publish(header, MessageConverter.toMessaging(message));
    }

    public void reboot(GenericManufacturerHeader header, String cid) {
        RemoteControlRebootNoticeMessage noticeMessage = new RemoteControlRebootNoticeMessage();
        noticeMessage.setMsgId(messageIdIncrementingAdapter.incr())
                .setMsgType(MessageType.REMOTE_CONTROL_REBOOT_NOTICE_MESSAGE)
                .setSendId(CurrentRequestUser.get().getId())
                .setSendTime(Instant.now().toEpochMilli())
                .setRecvId(cid);
        noticeMessage.setContent(new RemoteControlRebootNoticeMessage.RemoteControlRebootNoticeContent());

        Message message = MessageConverter.formMessaging(noticeMessage);
        messageAdapter.save(message);
        messageQueueMessaging.publish(header, MessageConverter.toMessaging(message));
    }


    public void restoreFactorySettings(GenericManufacturerHeader header, String cid) {
        RemoteControlRestoreFactorySettingsNoticeMessage noticeMessage = new RemoteControlRestoreFactorySettingsNoticeMessage();
        noticeMessage.setMsgId(messageIdIncrementingAdapter.incr())
                .setMsgType(MessageType.REMOTE_CONTROL_RESTORE_FACTORY_SETTINGS_NOTICE_MESSAGE)
                .setSendId(CurrentRequestUser.get().getId())
                .setSendTime(Instant.now().toEpochMilli())
                .setRecvId(cid);
        noticeMessage.setContent(new RemoteControlRestoreFactorySettingsNoticeMessage.RemoteControlRestoreFactorySettingsNoticeContent());

        Message message = MessageConverter.formMessaging(noticeMessage);
        messageAdapter.save(message);
        messageQueueMessaging.publish(header, MessageConverter.toMessaging(message));
    }

}
