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.domain.entity.message.MessageId;
import com.sikey.wa04.business.infrastructure.adapter.RabbitMQMessaging;
import com.sikey.wa04.business.infrastructure.adapter.RedisMessageIdIncrementingAdapter;
import com.sikey.wa04.business.infrastructure.dto.request.messaging.VideoCallDialRequest;
import com.sikey.wa04.business.infrastructure.dto.request.messaging.VideoCallHangupRequest;
import com.sikey.wa04.common.dto.header.GenericManufacturerHeader;
import com.sikey.wa04.common.dto.message.VideoCallInNoticeMessage;
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;
import java.util.List;

@Slf4j
@Service
public class VideoCallMessageService {

    @Resource
    private RedisMessageIdIncrementingAdapter messageIdIncrementingAdapter;

    @Resource
    private MessageAdapter messageAdapter;

    @Resource
    private RabbitMQMessaging rabbitMQMessaging;

    public void dial(VideoCallDialRequest request) {
        log.info("Video call dial: {}", request);
        Instant sendTime = Instant.now();
        Message message = new Message()
                .setId(new MessageId(messageIdIncrementingAdapter.incr()))
                .setType(MessageType.VIDEO_CALL_IN_NOTICE_MESSAGE)
                .setContent(new VideoCallInNoticeMessage.VideoCallInNoticeContent()
                        .setRoomId(request.getRoomId())
                        .setVideoCallToken(request.getVideoCallToken()))
                .setGroup(false)
                .setSenderId(CurrentRequestUser.get().getId())
                .setReceiverId(List.of(request.getSid()))
                .setSendTime(sendTime);
        messageAdapter.save(message);
        publish(request.getHeader(), message);
    }

    public void hangup(VideoCallHangupRequest request) {
        log.info("Video call hangup: {}", request);
        Instant sendTime = Instant.now();
        Message message = new Message()
                .setId(new MessageId(messageIdIncrementingAdapter.incr()))
                .setType(MessageType.VIDEO_CALL_OUT_NOTICE_MESSAGE)
                .setContent(new VideoCallInNoticeMessage.VideoCallInNoticeContent()
                        .setRoomId(request.getRoomId()))
                .setGroup(false)
                .setSenderId(CurrentRequestUser.get().getId())
                .setReceiverId(List.of(request.getSid()))
                .setSendTime(sendTime);
        messageAdapter.save(message);
        publish(request.getHeader(), message);
    }

    private void publish(GenericManufacturerHeader headers, Message message) {
        log.info("Send message {}", message);
        Object messagingObject = MessageConverter.toMessaging(message);
        rabbitMQMessaging.publish(headers, messagingObject);
    }
}
