package com.njcn.gather.result.util;

import com.njcn.gather.report.pojo.enums.ItemReportKeyEnum;
import com.njcn.gather.storage.pojo.po.SimAndDigNonHarmonicResult;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AngleResultAssemblerTest {

    @Test
    public void assembleRows_usesCurrentAngleI1AAndIgnoresVoltageAngleU1A() {
        SimAndDigNonHarmonicResult voltageAngle = result("U1A",
                data(0D, 0D, 0D, 1), data(0D, 0D, 0D, 1), data(0D, 0D, 0D, 1));
        SimAndDigNonHarmonicResult currentAngle = result("I1A",
                data(61.4D, 60D, 1.4D, 2), data(61D, 60D, 1D, 1), data(61D, 60D, 1D, 1));

        List<Map<String, String>> rows = AngleResultAssembler.assembleRows(Arrays.asList(voltageAngle, currentAngle));

        assertEquals(1, rows.size());
        Map<String, String> row = rows.get(0);
        assertEquals("60.0000", row.get(ItemReportKeyEnum.STANDARD.getKey()));
        assertEquals("61.4000", row.get(ItemReportKeyEnum.TEST_A.getKey()));
        assertEquals("61.0000", row.get(ItemReportKeyEnum.TEST_B.getKey()));
        assertEquals("61.0000", row.get(ItemReportKeyEnum.TEST_C.getKey()));
        assertEquals("1.4000", row.get(ItemReportKeyEnum.ERROR_A.getKey()));
        assertEquals("1.0000", row.get(ItemReportKeyEnum.ERROR_B.getKey()));
        assertEquals("1.0000", row.get(ItemReportKeyEnum.ERROR_C.getKey()));
        assertEquals("不合格", row.get(ItemReportKeyEnum.RESULT_A.getKey()));
        assertEquals("合格", row.get(ItemReportKeyEnum.RESULT_B.getKey()));
        assertEquals("合格", row.get(ItemReportKeyEnum.RESULT_C.getKey()));
        assertEquals("\u00b11.0\u00b0", row.get(ItemReportKeyEnum.ERROR_SCOPE.getKey()));
    }

    @Test
    public void assembleRows_voltageAngleOnlyReturnsNoRows() {
        SimAndDigNonHarmonicResult voltageAngle = result("U1A",
                data(0D, 0D, 0D, 1), data(0D, 0D, 0D, 1), data(0D, 0D, 0D, 1));

        assertTrue(AngleResultAssembler.assembleRows(Collections.singletonList(voltageAngle)).isEmpty());
    }

    private SimAndDigNonHarmonicResult result(String adType, String a, String b, String c) {
        SimAndDigNonHarmonicResult result = new SimAndDigNonHarmonicResult();
        result.setSort(21);
        result.setAdType(adType);
        result.setAValue(a);
        result.setBValue(b);
        result.setCValue(c);
        return result;
    }

    private String data(Double data, Double resultData, Double errorData, Integer isData) {
        return "{\"data\":" + data + ",\"unit\":\"\\u00b0\",\"isData\":" + isData
                + ",\"radius\":\"-1.0~1.0\",\"errorData\":" + errorData
                + ",\"resultData\":" + resultData + "}";
    }
}
