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.List;
import java.util.Map;

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

public class UnbalanceResultAssemblerTest {

    @Test
    public void assembleRows_mergesVoltageUnbalanceAndSequenceBySort() {
        SimAndDigNonHarmonicResult unbalance = total("V_UNBAN", 31,
                "{\"data\": 0.002506, \"unit\": \"%\", \"isData\": 4, \"radius\": \"-0.2~0.2\", \"errorData\": 0.002506, \"resultData\": 0}");
        SimAndDigNonHarmonicResult sequence = sequence("SeqV", 31,
                "{\"data\": 57.728199, \"isData\": 5, \"resultData\": 33.31598}",
                "{\"data\": 0.004355, \"isData\": 5, \"resultData\": 0}",
                "{\"data\": 0.00516, \"isData\": 5, \"resultData\": 0}");

        List<Map<String, String>> rows = UnbalanceResultAssembler.assembleRows(Arrays.asList(unbalance, sequence));

        assertEquals(1, rows.size());
        Map<String, String> row = rows.get(0);
        assertEquals("0.0000", row.get(ItemReportKeyEnum.STANDARD.getKey()));
        assertEquals("0.0025", row.get(ItemReportKeyEnum.TEST.getKey()));
        assertEquals("0.0025", row.get(ItemReportKeyEnum.ERROR.getKey()));
        assertEquals("±0.2%", row.get(ItemReportKeyEnum.ERROR_SCOPE.getKey()));
        assertEquals("无法比较", row.get(ItemReportKeyEnum.RESULT.getKey()));
        assertEquals("57.7282", row.get(ItemReportKeyEnum.POS_SEQ.getKey()));
        assertEquals("0.0044", row.get(ItemReportKeyEnum.NEG_SEQ.getKey()));
        assertEquals("0.0052", row.get(ItemReportKeyEnum.ZERO_SEQ.getKey()));
    }

    @Test
    public void assembleRows_mergesCurrentUnbalanceAndSequenceBySort() {
        SimAndDigNonHarmonicResult unbalance = total("I_UNBAN", 12,
                "{\"data\": 0.31, \"unit\": \"%\", \"isData\": 2, \"radius\": \"-0.1~0.1\", \"errorData\": 0.21, \"resultData\": 0.1}");
        SimAndDigNonHarmonicResult sequence = sequence("SeqA", 12,
                "{\"data\": 4.9999, \"isData\": 5, \"resultData\": 5}",
                "{\"data\": 0.0021, \"isData\": 5, \"resultData\": 0}",
                "{\"data\": 0.0008, \"isData\": 5, \"resultData\": 0}");

        List<Map<String, String>> rows = UnbalanceResultAssembler.assembleRows(Arrays.asList(sequence, unbalance));

        assertEquals(1, rows.size());
        Map<String, String> row = rows.get(0);
        assertEquals("0.1000", row.get(ItemReportKeyEnum.STANDARD.getKey()));
        assertEquals("0.3100", row.get(ItemReportKeyEnum.TEST.getKey()));
        assertEquals("0.2100", row.get(ItemReportKeyEnum.ERROR.getKey()));
        assertEquals("不合格", row.get(ItemReportKeyEnum.RESULT.getKey()));
        assertEquals("4.9999", row.get(ItemReportKeyEnum.POS_SEQ.getKey()));
        assertEquals("0.0021", row.get(ItemReportKeyEnum.NEG_SEQ.getKey()));
        assertEquals("0.0008", row.get(ItemReportKeyEnum.ZERO_SEQ.getKey()));
    }

    @Test
    public void assembleRows_defaultsMissingSequenceValuesToSlash() {
        SimAndDigNonHarmonicResult unbalance = total("V_UNBAN", 7,
                "{\"data\": 0.05, \"unit\": \"%\", \"isData\": 1, \"radius\": \"-0.2~0.2\", \"errorData\": 0.01, \"resultData\": 0.04}");

        List<Map<String, String>> rows = UnbalanceResultAssembler.assembleRows(Arrays.asList(unbalance));

        assertEquals(1, rows.size());
        Map<String, String> row = rows.get(0);
        assertEquals("/", row.get(ItemReportKeyEnum.POS_SEQ.getKey()));
        assertEquals("/", row.get(ItemReportKeyEnum.NEG_SEQ.getKey()));
        assertEquals("/", row.get(ItemReportKeyEnum.ZERO_SEQ.getKey()));
        assertEquals("合格", row.get(ItemReportKeyEnum.RESULT.getKey()));
    }

    @Test
    public void assembleRows_leavesTimeUnsetForItemAnchorPointFallback() {
        SimAndDigNonHarmonicResult unbalance = total("V_UNBAN", 7,
                "{\"data\": 0.05, \"unit\": \"%\", \"isData\": 1, \"radius\": \"-0.2~0.2\", \"errorData\": 0.01, \"resultData\": 0.04}");

        List<Map<String, String>> rows = UnbalanceResultAssembler.assembleRows(Arrays.asList(unbalance));

        assertEquals(1, rows.size());
        assertNull(rows.get(0).get(ItemReportKeyEnum.TIME.getKey()));
    }

    private SimAndDigNonHarmonicResult total(String code, Integer sort, String tValue) {
        SimAndDigNonHarmonicResult result = new SimAndDigNonHarmonicResult();
        result.setAdType(code);
        result.setSort(sort);
        result.setTValue(tValue);
        return result;
    }

    private SimAndDigNonHarmonicResult sequence(String code, Integer sort, String aValue, String bValue, String cValue) {
        SimAndDigNonHarmonicResult result = new SimAndDigNonHarmonicResult();
        result.setAdType(code);
        result.setSort(sort);
        result.setAValue(aValue);
        result.setBValue(bValue);
        result.setCValue(cValue);
        return result;
    }
}
