package com.pwt.netty.common.charger.server;

import com.pwt.netty.common.charger.RequestData;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.LocalDateTime;

/**
 * 校时命令
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = true)
public class CalibrateTimeCommand extends RequestData {

    public static final int timeDataLength = 6;

    private LocalDateTime dateTime = LocalDateTime.now();

    @Override
    public void encode(ByteBuf byteBuf) {
        byteBuf.writeByte(dateTime.getYear() % 100);
        byteBuf.writeByte(dateTime.getMonthValue());
        byteBuf.writeByte(dateTime.getDayOfMonth());
        byteBuf.writeByte(dateTime.getHour());
        byteBuf.writeByte(dateTime.getMinute());
        byteBuf.writeByte(dateTime.getSecond());
        byteBuf.writeByte(getEnd());
    }

    @Override
    public void decode(ByteBuf byteBuf) {
        int year = byteBuf.readUnsignedByte();
        int month = byteBuf.readUnsignedByte();
        int day = byteBuf.readUnsignedByte();
        int hour = byteBuf.readUnsignedByte();
        int minute = byteBuf.readUnsignedByte();
        int second = byteBuf.readUnsignedByte();
        byteBuf.skipBytes(1);
        dateTime = LocalDateTime.of(2000 + year, month, day, hour, minute, second);
    }

    @Override
    public int getProtocolLength() {
        return 7;
    }

    @Override
    public byte getEnd() {
        return 0x16;
    }

}
