package com.pwt.netty.common.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.pwt.netty.common.chargerold.ChargerMessageHeader;
import com.pwt.netty.common.chargerold.RequestMessage;
import com.pwt.netty.common.chargerold.client.*;
import com.pwt.netty.util.ObjUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.jupiter.api.Test;

public class ChargerTest {

    // 电池故障
    @Test
    void batteryFaultTest() throws IllegalAccessException
    {
        batteryFaultTest_();
    }
    //电池状态
    @Test
    void batteryStatusTest() throws IllegalAccessException
    {
        batteryStatusTest_();
    }
    //电池其他数据
    @Test
    void batteryOtherTest() throws IllegalAccessException
    {
        batteryOtherTest_();
    }
    //BMS
    @Test
    void bmsStatusTest() throws IllegalAccessException
    {
        bmsStatusTest_();
    }
    //充电完成数据
    @Test
    void chargeCompleteTest() throws IllegalAccessException
    {
        chargeCompleteTest_();
    }
    //充电机报警数据
    @Test
    void chargerAlarmTest() throws IllegalAccessException
    {
        chargerAlarmTest_();
    }
    //充电机充电状态数据
    @Test
    void chargerChargeStatusTest() throws IllegalAccessException
    {
        chargerChargeStatusTest_();
    }
    //充电机状态数据
    @Test
    void chargerStatusTest() throws IllegalAccessException
    {
        chargerStatusTest_();
    }
    //配置命令数据
    @Test
    void configurationCmdTest() throws IllegalAccessException
    {
        configurationCmdTest_();
    }
    //控制灯闪烁
    @Test
    void controlLampTest() throws IllegalAccessException
    {
        controlLampTest_();
    }
    //校时
    @Test
    void inspectionTimeTest() throws IllegalAccessException
    {
        inspectionTimeTest_();
    }
    //启停
    @Test
    void startStopTest() throws IllegalAccessException
    {
        startStopTest_();
    }

    void startStopTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X50);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        StartStopData data1 = (StartStopData) ObjUtil.getProperty(new StartStopData());
        data1.encode(buf);

        StartStopData data2 = new StartStopData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void inspectionTimeTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X90);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        InspectionTimeData data1 = (InspectionTimeData) ObjUtil.getProperty(new InspectionTimeData());
        data1.encode(buf);

        InspectionTimeData data2 = new InspectionTimeData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void controlLampTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X80);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ControlLampData data1 = (ControlLampData) ObjUtil.getProperty(new ControlLampData());
        data1.encode(buf);

        ControlLampData data2 = new ControlLampData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    private void configurationCmdTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X70);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ConfigurationCmdData data1 = (ConfigurationCmdData) ObjUtil.getProperty(new ConfigurationCmdData());
        data1.encode(buf);

        ConfigurationCmdData data2 = new ConfigurationCmdData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void chargerStatusTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X15);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ChargerStatusData data1 = (ChargerStatusData) ObjUtil.getProperty(new ChargerStatusData());
        data1.encode(buf);

        ChargerStatusData data2 = new ChargerStatusData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X1B);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void chargerChargeStatusTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X10);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ChargerChargeStatusData data1 = (ChargerChargeStatusData) ObjUtil.getProperty(new ChargerChargeStatusData());
        data1.encode(buf);

        ChargerChargeStatusData data2 = new ChargerChargeStatusData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void chargerAlarmTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X30);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ChargerAlarmData data1 = (ChargerAlarmData) ObjUtil.getProperty(new ChargerAlarmData());
        data1.encode(buf);

        ChargerAlarmData data2 = new ChargerAlarmData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void chargeCompleteTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X20);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        ChargeCompleteData data1 = (ChargeCompleteData) ObjUtil.getProperty(new ChargeCompleteData());
        data1.encode(buf);

        ChargeCompleteData data2 = new ChargeCompleteData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X16);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void bmsStatusTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X13);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        BmsStatusData data1 = (BmsStatusData) ObjUtil.getProperty(new BmsStatusData());
        data1.encode(buf);

        BmsStatusData data2 = new BmsStatusData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X19);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void batteryOtherTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X14);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        BatteryOtherData data1 = (BatteryOtherData) ObjUtil.getProperty(new BatteryOtherData());
        data1.encode(buf);

        BatteryOtherData data2 = new BatteryOtherData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X1A);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void batteryStatusTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X11);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        BatteryStatusData data1 = (BatteryStatusData) ObjUtil.getProperty(new BatteryStatusData());
        data1.encode(buf);

        BatteryStatusData data2 = new BatteryStatusData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X17);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);
    }

    void batteryFaultTest_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        ChargerMessageHeader header = new ChargerMessageHeader();
        header.setStart(0X12);
        header.setSerialNo(1551771312);
        header.setChargerAddress("32110010101111");
        header.setLength(10);

        header.encode(buf);

        ChargerMessageHeader decodeHeader = new ChargerMessageHeader();
        decodeHeader.decode(buf);
        assertEquals(header, decodeHeader);

        BatteryFaultData data1 = (BatteryFaultData) ObjUtil.getProperty(new BatteryFaultData());
        data1.encode(buf);

        BatteryFaultData data2 = new BatteryFaultData();
        data2.decode(buf);

        assertEquals(data1, data2);

        RequestMessage message = new RequestMessage();
        message.setHeader(header);
        message.setMessageData(data1);
        message.setEnd(0X18);
        buf.clear();

        message.encode(buf);

        RequestMessage message2 = new RequestMessage();
        message2.decode(buf);
        assertEquals(message, message2);

    }
}
