package com.mosquito.project.service;

import com.mosquito.project.config.PosterConfig;
import com.mosquito.project.persistence.entity.ShortLinkEntity;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

class PosterRenderServiceTest {

    @BeforeAll
    static void enableHeadlessMode() {
        System.setProperty("java.awt.headless", "true");
    }

    @Test
    void renderPosterHtml_includesElementsAndShortUrl() {
        ShortLinkService shortLinkService = Mockito.mock(ShortLinkService.class);
        ShortLinkEntity shortLink = new ShortLinkEntity();
        shortLink.setCode("short123");
        when(shortLinkService.create(anyString())).thenReturn(shortLink);

        PosterConfig posterConfig = buildPosterConfig(buildHtmlElements());
        PosterRenderService service = new PosterRenderService(posterConfig, shortLinkService);

        String html = service.renderPosterHtml(10L, 20L, "custom");

        assertTrue(html.contains("/r/short123"));
        assertTrue(html.contains("data=%2Fr%2Fshort123"));
        assertTrue(html.contains("Hello 10"));
        assertTrue(html.contains("https://example.com/image.png"));
        assertTrue(html.contains("立即加入"));
    }

    @Test
    void renderPoster_generatesPngBytes() {
        ShortLinkService shortLinkService = Mockito.mock(ShortLinkService.class);
        PosterConfig posterConfig = buildPosterConfig(buildImageElements());
        PosterRenderService service = new PosterRenderService(posterConfig, shortLinkService);

        byte[] bytes = service.renderPoster(11L, 22L, "custom");

        assertTrue(bytes.length > 0);
    }

    private PosterConfig buildPosterConfig(Map<String, PosterConfig.PosterElement> elements) {
        PosterConfig posterConfig = new PosterConfig();
        posterConfig.setDefaultTemplate("default");

        PosterConfig.PosterTemplate template = new PosterConfig.PosterTemplate();
        template.setWidth(300);
        template.setHeight(400);
        template.setBackgroundColor("#ffffff");
        template.setElements(elements);

        Map<String, PosterConfig.PosterTemplate> templates = new HashMap<>();
        templates.put("default", template);
        templates.put("custom", template);
        posterConfig.setTemplates(templates);
        return posterConfig;
    }

    private Map<String, PosterConfig.PosterElement> buildHtmlElements() {
        Map<String, PosterConfig.PosterElement> elements = new HashMap<>();
        elements.put("text", element("text", 10, 10, 200, 30, "Hello {{activityId}}"));
        elements.put("qrcode", element("qrcode", 10, 50, 120, 120, "{{shortUrl}}"));
        elements.put("image", element("image", 10, 200, 80, 80, "https://example.com/image.png"));
        elements.put("button", element("button", 10, 300, 120, 40, "立即加入"));
        return elements;
    }

    private Map<String, PosterConfig.PosterElement> buildImageElements() {
        Map<String, PosterConfig.PosterElement> elements = new HashMap<>();
        elements.put("text", element("text", 10, 10, 200, 30, "Poster {{activityId}}"));
        elements.put("qrcode", element("qrcode", 10, 60, 120, 120, "{{shortUrl}}"));
        elements.put("rect", element("rect", 10, 200, 80, 40, ""));
        return elements;
    }

    private PosterConfig.PosterElement element(String type, int x, int y, int width, int height, String content) {
        PosterConfig.PosterElement element = new PosterConfig.PosterElement();
        element.setType(type);
        element.setX(x);
        element.setY(y);
        element.setWidth(width);
        element.setHeight(height);
        element.setContent(content);
        return element;
    }
}
