package com.eactive.eai.rms.onl.common.service.ums;

import com.eactive.eai.rms.data.entity.man.monitoringProperty.service.MonitoringPropertyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 25.11.05 - 광주은행 미사용 클래스
 */
@Slf4j
@Component
@Deprecated
public class UmsSender {
    
    private final MonitoringPropertyService monitoringPropertyService;

    public UmsSender(MonitoringPropertyService monitoringPropertyService) {
        this.monitoringPropertyService = monitoringPropertyService;
    }

    public void sendMessage(String message) throws IOException {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

            String umsUrl = monitoringPropertyService.getPrpty2ValById("Monitoring", "ums.url");
            HttpPost httpPost = new HttpPost(umsUrl);

            // Convert message to UTF-8 bytes and send
            byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
            httpPost.setEntity(new ByteArrayEntity(messageBytes, ContentType.create("text/plain", StandardCharsets.UTF_8)));

            if (log.isDebugEnabled()) {
                log.debug("Sending message with UTF-8 byte length: {}", messageBytes.length);
            }


            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                int statusCode = response.getCode();
                if (log.isDebugEnabled()) {
                    HttpEntity entity = response.getEntity();
                    try {
                        log.debug(EntityUtils.toString(entity, StandardCharsets.UTF_8));
                    } catch (ParseException e) {
                        log.debug(e.getMessage());
                    }
                }
                if (statusCode != 200) {
                    throw new RuntimeException("Message sending failed with status: " + statusCode);
                }
            }
        }
    }
}
