package com.pwt.netty.common.test;

import com.pwt.netty.common.vehicle2old.Vehicle2MessageHeader;
import com.pwt.netty.common.vehicle2old.RequestMessage;
import com.pwt.netty.common.vehicle2old.client.*;
import com.pwt.netty.util.ObjUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class Vechicle2Test {

    private final String VIN = "LA9ZL9NH2PALG6519";


    @Test
    void heartBeat() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        Vehicle2MessageHeader header = new Vehicle2MessageHeader();
        header.setOpCode(0X03);
        header.setLength(1);
        header.setVIN(VIN);
        header.encode(buf);

        Vehicle2MessageHeader decodeHeader = new Vehicle2MessageHeader();
        decodeHeader.decode(buf);

        assertEquals(header, decodeHeader);

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

        HeartBeatData data2 = new HeartBeatData();
        data2.decode(buf);
        assertEquals(data1, data2);

        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setHeader(header);
        requestMessage.setMessageData(data1);
        requestMessage.setEnd(RequestMessage.END);

        buf.clear();
        requestMessage.encode(buf);

        RequestMessage decodeMsg = new RequestMessage();
        decodeMsg.decode(buf);
        assertEquals(requestMessage, decodeMsg);
    }

    @Test
    void power() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        Vehicle2MessageHeader header = new Vehicle2MessageHeader();
        header.setOpCode(0X01);
        header.setLength(1);
        header.setVIN(VIN);
        header.encode(buf);

        Vehicle2MessageHeader decodeHeader = new Vehicle2MessageHeader();
        decodeHeader.decode(buf);

        assertEquals(decodeHeader, header);

        PowerData data1 = (PowerData) ObjUtil.getProperty(new PowerData());
        data1.setVehicleTotalDischargeRation(0.01);
        data1.encode(buf);

        PowerData data2 = new PowerData();
        data2.decode(buf);
        assertEquals(data1, data2);

        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setHeader(header);
        requestMessage.setMessageData(data1);
        requestMessage.setEnd(RequestMessage.END);

        buf.clear();
        requestMessage.encode(buf);

        RequestMessage decodeMsg = new RequestMessage();
        decodeMsg.decode(buf);
        assertEquals(requestMessage, decodeMsg);
    }

    @Test
    void lock_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        Vehicle2MessageHeader header = new Vehicle2MessageHeader();
        header.setOpCode(0X05);
        header.setLength(1);
        header.setVIN(VIN);
        header.encode(buf);

        Vehicle2MessageHeader decodeHeader = new Vehicle2MessageHeader();
        decodeHeader.decode(buf);

        assertEquals(decodeHeader, header);

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

        LockData data2 = new LockData();
        data2.decode(buf);
        assertEquals(data1, data2);

        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setHeader(header);
        requestMessage.setMessageData(data1);
        requestMessage.setEnd(RequestMessage.END);

        buf.clear();
        requestMessage.encode(buf);

        RequestMessage decodeMsg = new RequestMessage();
        decodeMsg.decode(buf);
        assertEquals(requestMessage, decodeMsg);
    }

    @Test
    void unLock_() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        Vehicle2MessageHeader header = new Vehicle2MessageHeader();
        header.setOpCode(0X07);
        header.setLength(1);
        header.setVIN(VIN);
        header.encode(buf);

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

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

        UnLockData data2 = new UnLockData();
        data2.decode(buf);
        assertEquals(data1, data2);

        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setHeader(header);
        requestMessage.setMessageData(data1);
        requestMessage.setEnd(RequestMessage.END);

        buf.clear();
        requestMessage.encode(buf);

        RequestMessage decodeMsg = new RequestMessage();
        decodeMsg.decode(buf);
        assertEquals(requestMessage, decodeMsg);
    }

    @Test
    void setParam() throws IllegalAccessException
    {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        Vehicle2MessageHeader header = new Vehicle2MessageHeader();
        header.setOpCode(0X011);
        header.setLength(1);
        header.setVIN(VIN);
        header.encode(buf);

        Vehicle2MessageHeader decodeHeader = new Vehicle2MessageHeader();
        decodeHeader.decode(buf);

        assertEquals(header, decodeHeader);

        SetParametersData data1 = (SetParametersData) ObjUtil.getProperty(new SetParametersData());
        data1.setVehicleTotalDischargeRation(0.01);
        data1.encode(buf);

        SetParametersData data2 = new SetParametersData();
        data2.decode(buf);
        assertEquals(data1, data2);

        RequestMessage requestMessage = new RequestMessage();
        requestMessage.setHeader(header);
        requestMessage.setMessageData(data1);
        requestMessage.setEnd(RequestMessage.END);

        buf.clear();
        requestMessage.encode(buf);

        RequestMessage decodeMsg = new RequestMessage();
        decodeMsg.decode(buf);
        assertEquals(requestMessage, decodeMsg);
    }
}