package com.njcn.gather.report.service.impl;

import com.njcn.gather.report.pojo.enums.PowerIndexEnum;
import com.njcn.gather.tools.report.util.Docx4jUtil;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class PqReportServiceImplTest {

    @Test
    public void formatChineseReportDateUsesChineseNumerals() {
        assertEquals("二O二六年一月七日", PqReportServiceImpl.formatChineseReportDate(LocalDate.of(2026, 1, 7)));
    }

    @Test
    public void formatChineseReportDateHandlesTwoDigitMonthAndDay() {
        assertEquals("二O二六年十二月三十一日", PqReportServiceImpl.formatChineseReportDate(LocalDate.of(2026, 12, 31)));
    }

    @Test
    public void formatNumberChineseDateUsesChineseDateSeparators() {
        assertEquals("2021年1月1日", PqReportServiceImpl.formatNumberChineseDate(LocalDate.of(2021, 1, 1)));
    }

    @Test
    public void buildItemLineTitleCreatesNormalSmallParagraph() throws Exception {
        PqReportServiceImpl service = newReportService();
        ObjectFactory factory = new ObjectFactory();
        Docx4jUtil.HeadingContent lineTitleTemplate = new Docx4jUtil.HeadingContent();
        lineTitleTemplate.setHeadingText(PowerIndexEnum.LINE_TITLE.getKey());
        Map<String, List<Docx4jUtil.HeadingContent>> contentMap = new HashMap<>();
        contentMap.put(PowerIndexEnum.LINE_TITLE.getKey(), Collections.singletonList(lineTitleTemplate));

        Method method = PqReportServiceImpl.class.getDeclaredMethod("buildItemLineTitle", Map.class, int.class, ObjectFactory.class);
        method.setAccessible(true);
        P paragraph = (P) method.invoke(service, contentMap, 0, factory);

        assertEquals("测量回路1", Docx4jUtil.getTextFromP(paragraph));
        assertNull(paragraph.getPPr().getOutlineLvl());
        R run = (R) paragraph.getContent().get(0);
        assertNotNull(run.getRPr());
        assertEquals(BigInteger.valueOf(24), run.getRPr().getSz().getVal());
    }

    @Test
    public void itemAnchorGroupKeyNormalizesAngleScriptCode() throws Exception {
        Method method = PqReportServiceImpl.class.getDeclaredMethod("itemAnchorGroupKey", String.class);
        method.setAccessible(true);

        assertEquals("ANGLE", method.invoke(null, "Angle"));
        assertEquals("ANGLE", method.invoke(null, "ANGLE"));
    }

    private PqReportServiceImpl newReportService() throws Exception {
        Constructor<?> constructor = PqReportServiceImpl.class.getDeclaredConstructors()[0];
        constructor.setAccessible(true);
        return (PqReportServiceImpl) constructor.newInstance(new Object[constructor.getParameterCount()]);
    }
}
