package de.avatic.taric.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DescriptionSerializer extends JsonSerializer<String> {
    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

            if (value != null) {
                String converted = value
                        .replace("!1!", "\n")                    // Paragraph mark (carriage return)
                        .replace("|", "\u00A0")                  // Unbreakable space (non-breaking space)
                        .replace("!X!", "×")                     // Multiply sign
                        .replace("!x!", "×")                     // Multiply sign
                        .replace("!o!", "°")                     // Degree symbol
                        .replace("!O!", "°")                     // Degree symbol
                        .replace("!<=!", "≤")                    // Less than or equal to
                        .replace("!>=!", "≥")                    // Greater than or equal to
                        .replace("\\n", "\n")
                        .replaceAll("\\$(.)", "<sup>$1</sup>")   // Superscript
                        .replaceAll("@(.)", "<sub>$1</sub>");    // Subscript


                gen.writeString(converted);
            } else {
                gen.writeNull();
            }
    }
}
