package com.caliverse.admin.domain.RabbitMq;

import com.caliverse.admin.domain.RabbitMq.message.LogoutReasonType;
import com.caliverse.admin.domain.RabbitMq.message.MailItem;
import com.caliverse.admin.domain.RabbitMq.message.OperationSystemMessage;
import com.caliverse.admin.domain.RabbitMq.message.ServerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MessageHandlerService {

    private final RabbitMqService rabbitMqService;

    public MessageHandlerService(RabbitMqService rabbitMqService) {
        this.rabbitMqService = rabbitMqService;
    }

    public void sendUserKickMessage(String userGuid, String serverName){
        var user_kick_builder = ServerMessage.MOS2GS_NTF_USER_KICK.newBuilder();
        user_kick_builder.setUserGuid(userGuid);
        user_kick_builder.setKickReasonMsg("");
        user_kick_builder.setLogoutReasonType(LogoutReasonType.LogoutReasonType_None);

        rabbitMqService.SendMessage(user_kick_builder.build(), serverName);
    }

    public void sendNoticeMessage(List<String> serverList, String type, List<OperationSystemMessage> msgList, List<OperationSystemMessage> senderList){
        try {
            var noticeBuilder = ServerMessage.MOS2GS_NTF_NOTICE_CHAT.newBuilder();
            noticeBuilder.addNoticeType(type);
//        noticeBuilder.setNoticeType(0, type);
//        int msgIdx = 0;
            for (OperationSystemMessage msg : msgList) {
                noticeBuilder.addChatMessage(msg);
//            noticeBuilder.setChatMessage(msgIdx, msg);
//            msgIdx++;
            }
            for (OperationSystemMessage sender : senderList) {
                noticeBuilder.addSender(sender);
            }
            for (String server : serverList) {
                rabbitMqService.SendMessage(noticeBuilder.build(), server);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    public void sendMailMessage(String serverName, String userGuid, String mailType, List<OperationSystemMessage> titleList, List<OperationSystemMessage> msgList
            , List<MailItem> itemList, List<OperationSystemMessage> senderList){
        var mail_builder = ServerMessage.MOS2GS_NTF_MAIL_SEND.newBuilder();
        mail_builder.setUserGuid(userGuid);
        mail_builder.setMailType(mailType);

        for(OperationSystemMessage title : titleList){
            mail_builder.addTitle(title);
        }
        for(OperationSystemMessage msg : msgList){
            mail_builder.addMsg(msg);
        }
        for(MailItem item : itemList){
            mail_builder.addItemList(item);
        }
        for(OperationSystemMessage sender : senderList){
            mail_builder.addSender(sender);
        }
        rabbitMqService.SendMessage(mail_builder.build(), serverName);

    }


}

