//  • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
//  ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
//  ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
//  ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
//  ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2026
//                https://www.magicbane.com


//  • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
//  ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
//  ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
//  ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
//  ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2026
//                https://www.magicbane.com


package engine.net.client.msg;

import engine.mbEnums.GameObjectType;
import engine.net.AbstractConnection;
import engine.net.ByteBufferReader;
import engine.net.ByteBufferWriter;
import engine.net.Protocol;


public class DoorTryOpenMsg extends ClientNetMsg {

    private int unk1;
    private int doorID;
    private int buildingUUID;
    private int playerUUID;
    private byte toggle;

    /**
     * This is the general purpose constructor.
     */
    public DoorTryOpenMsg(int doorID, int targetID, int senderID, byte toggle) {
        super(Protocol.DOORTRYOPEN);
        this.unk1 = 0;
        this.doorID = doorID;
        this.buildingUUID = targetID;
        this.playerUUID = senderID;
        this.toggle = toggle;
    }

    /**
     * This constructor is used by NetMsgFactory. It attempts to deserialize the
     * ByteBuffer into a message. If a BufferUnderflow occurs (based on reading
     * past the limit) then this constructor Throws that Exception to the
     * caller.
     */
    public DoorTryOpenMsg(AbstractConnection origin, ByteBufferReader reader) {
        super(Protocol.DOORTRYOPEN, origin, reader);
    }

    /**
     * Serializes the subclass specific items to the supplied NetMsgWriter.
     */
    @Override
    protected void _serialize(ByteBufferWriter writer) {
        if (this.unk1 == 2) {
            writer.putInt(2);
            for (int i = 0; i < 6; i++)
                writer.putInt(0);
            writer.put((byte) 0);
        } else {
            writer.putInt(0); //0 or 2. If 2 all other variables are 0
            writer.putInt(0);
            writer.putInt(doorID);
            writer.putInt(GameObjectType.Building.ordinal());
            writer.putInt(buildingUUID);
            writer.putInt(GameObjectType.PlayerCharacter.ordinal());
            writer.putInt(playerUUID);
            writer.put(toggle);
        }
    }

    /**
     * Deserializes the subclass specific items from the supplied NetMsgReader.
     */
    @Override
    protected void _deserialize(ByteBufferReader reader) {
        this.unk1 = reader.getInt();
        reader.getInt();
        this.doorID = reader.getInt();
        reader.getInt(); // type padding
        this.buildingUUID = reader.getInt();
        reader.getInt(); // type padding
        this.playerUUID = reader.getInt();
        this.toggle = reader.get();
    }

    /**
     * @return the unknown1
     */
    public int getDoorID() {
        return doorID;
    }

    /**
     * @return the buildingUUID
     */
    public int getBuildingUUID() {
        return buildingUUID;
    }

    /**
     * @return the playerUUID
     */
    public int playerUUID() {
        return playerUUID;
    }

    /**
     * @return the toggle
     */
    public byte getToggle() {
        return toggle;
    }

    public int getUnk1() {
        return this.unk1;
    }

    public void setUnk1(int value) {
        this.unk1 = value;
    }
}
