import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.services.LanguageClient;

/**
 * A singleton logger utility for sending log messages from the Language Server
 * to the Language Client using the Language Server Protocol (LSP).
 * <p>
 * This class wraps the {@link LanguageClient#logMessage(MessageParams)} method
 * to provide a simple way to log messages once the client has been initialized.
 * </p>
 *
 * <p>Usage example:</p>
 * <pre>{@code
 * LSClientLogger logger = LSClientLogger.getInstance();
 * logger.initialize(languageClient);
 * logger.logMessage("Server started", MessageType.Info);
 * }</pre>
 *
 * The text will appear on the client.
 */
public class LSClientLogger {

   /**
    * Singleton instance of the logger.
    */
   private static LSClientLogger INSTANCE;

   /**
    * The Language Client instance used to send messages.
    */
   private LanguageClient client;

   /**
    * Flag to indicate whether the logger has been initialized with a client.
    */
   private boolean isInitialized;

   /**
    * Private constructor to enforce singleton pattern.
    */
   private LSClientLogger() {
   }

   /**
    * Initializes the logger with a {@link LanguageClient}.
    * <p>
    * This must be called before sending any log messages.
    * Initialization is performed only once.
    * </p>
    *
    * @param languageClient the client used to send log messages
    */
   public void initialize(LanguageClient languageClient) {
      if (!Boolean.TRUE.equals(isInitialized)) {
         this.client = languageClient;
      }
      isInitialized = true;
   }

   /**
    * Returns the singleton instance of the logger.
    *
    * @return the singleton {@code LSClientLogger} instance
    */
   public static LSClientLogger getInstance() {
      if (INSTANCE == null) {
         INSTANCE = new LSClientLogger();
      }
      return INSTANCE;
   }

   /**
    * Sends a log message to the client.
    *
    * @param message the text of the message to log
    * @param type    the type of message (e.g., {@link MessageType#Info},
    *                {@link MessageType#Warning}, {@link MessageType#Error})
    */
   public void logMessage(String message, MessageType type) {
      if (!isInitialized) {
         return;
      }
      client.logMessage(new MessageParams(type, message));
   }
}
