package cn.reghao.tnb.message.app.rabbit;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.message.app.config.SpringLifecycle;
import cn.reghao.tnb.message.app.service.MessageConsumer;
import cn.reghao.tnb.message.app.ws.media.MediaService;
import com.google.gson.JsonObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author reghao
 * @date 2024-11-28 14:37:18
 */
@Slf4j
@Component
public class RabbitListeners {
    private final MediaService mediaService;
    private final MessageConsumer messageConsumer;
    private final SpringLifecycle springLifecycle;

    public RabbitListeners(MediaService mediaService, MessageConsumer messageConsumer,
                           SpringLifecycle springLifecycle) {
        this.mediaService = mediaService;
        this.messageConsumer = messageConsumer;
        this.springLifecycle = springLifecycle;
    }

    @RabbitListener(bindings =@QueueBinding(
            // 队列名字可以随便定义
            value = @Queue(autoDelete = "false"),
            // 交换机的名字必须和生产者保持一致, fanout 是以广播模式(发布订阅模式)
            exchange = @Exchange(value = "amq.fanout", type = ExchangeTypes.FANOUT))
    )
    public void consumeWsMessage(@Payload String msg) {
        String nodeId = springLifecycle.getNodeId();
        log.info("WsMessage -> {}", msg);
        JsonObject jsonObject = JsonConverter.jsonToJsonElement(msg).getAsJsonObject();
        String broadcastBy = jsonObject.get("broadcastBy").getAsString();
        if (broadcastBy.equals(nodeId)) {
            log.info("broadcast by myself, ignore message...");
            return;
        }

        String type = jsonObject.get("type").getAsString();
        if ("sendViewCount".equals(type)) {
            String videoId = jsonObject.get("videoId").getAsString();
            mediaService.sendViewCount(videoId);
        }
        /*ChatPayload chatPayload = JsonConverter.jsonToObject(msg, ChatPayload.class);
        long receiverId = chatPayload.getReceiverId();
        String content = chatPayload.getContent();
        if (wsConnection.getUserSession(receiverId) != null) {
            log.info("send {} -> {}", receiverId, content);
            try {
                wsConnection.sendChatMessage(receiverId+"", chatPayload);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            log.info("{} 的 session 不在本节点上", receiverId);
        }*/
    }

    @RabbitListener(bindings =@QueueBinding(
            value = @Queue(value = "tnb.message.account", durable = "true"),
            key = "tnb.message.account",
            exchange = @Exchange(value = "amq.direct")),
            ackMode = "MANUAL"
    )
    public void consumeNotifyMessage(@Payload String jsonPayload, Channel channel, Message message) {
        messageConsumer.handleMessage(jsonPayload);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("手动确认消息 {}", deliveryTag);
            channel.basicAck(deliveryTag,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
