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


package engine.net.client.msg;

import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.mbEnums.GameObjectType;
import engine.net.AbstractConnection;
import engine.net.ByteBufferReader;
import engine.net.ByteBufferWriter;
import engine.net.Protocol;
import engine.objects.AbstractCharacter;
import engine.objects.Building;

public class MoveToPointMsg extends ClientNetMsg {

    private int sourceType; //ordinal of the character type that sent this message 54=PlayerCharacter
    private int sourceID; // uuid of the source character of this message
    private float startLat; //start loc of move message (offset if inside a building not world loc)
    private float startLon; //start loc of move message (offset if inside a building not world loc)
    private float startAlt; //start loc of move message (offset if inside a building not world loc)
    private float endLat; //end loc of move message (offset if inside a building not world loc)
    private float endLon; //end loc of move message (offset if inside a building not world loc)
    private float endAlt; //end loc of move message (offset if inside a building not world loc)
    private int startLocType; // enum ordinal of the object player is inside 0=nothing 8=building
    private int inBuildingUUID; // uuid of the building character is currently inside
    private int inBuilding; // is inside a building 0=true -1=false
    private int inBuildingFloor; // floor of building character is currently in -1=not inside building 0/1/2/3 = floor number
    private byte initiatedByAttack; // move message sent as a result of move to target to attack 0=false 1=true
    private byte unknown03;

    /**
     * This is the general purpose constructor.
     */
    public MoveToPointMsg() {
        super(Protocol.MOVETOPOINT);
    }


    public MoveToPointMsg(MoveToPointMsg msg) {
        super(Protocol.MOVETOPOINT);
        this.sourceType = msg.sourceType;
        this.sourceID = msg.sourceID;
        this.startLat = msg.startLat;
        this.startLon = msg.startLon;
        this.startAlt = msg.startAlt;
        this.endLat = msg.endLat;
        this.endLon = msg.endLon;
        this.endAlt = msg.endAlt;
        this.startLocType = msg.startLocType;
        this.inBuildingUUID = msg.inBuildingUUID;
        this.inBuilding = msg.inBuilding;
        this.inBuildingFloor = msg.inBuildingFloor;
        this.initiatedByAttack = msg.initiatedByAttack;
        this.unknown03 = msg.unknown03;
    }

    //Moving Furniture out of building to unload properly. //for outside regions only
    public MoveToPointMsg(Building building) {
        super(Protocol.MOVETOPOINT);
        this.sourceType = building.getObjectType().ordinal();
        this.sourceID = building.getObjectUUID();
        this.startLat = building.getLoc().x;
        this.startLon = building.getLoc().z;
        this.startAlt = building.getLoc().y;
        this.endLat = building.getLoc().x;
        this.endLon = building.getLoc().z;
        this.endAlt = building.getLoc().y;
        this.startLocType = 0;
        this.inBuildingUUID = 0;
        this.inBuilding = -1;
        this.inBuildingFloor = -1;
        this.initiatedByAttack = 0;
        this.unknown03 = 0;
    }


    /**
     * 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 MoveToPointMsg(AbstractConnection origin, ByteBufferReader reader) {
        super(Protocol.MOVETOPOINT, origin, reader);
    }

    /**
     * Serializes the subclass specific items to the supplied NetMsgWriter.
     */
    @Override
    protected void _serialize(ByteBufferWriter writer) {
        writer.putInt(this.sourceType);
        writer.putInt(this.sourceID);

        writer.putFloat(this.startLat);
        writer.putFloat(this.startAlt);
        writer.putFloat(this.startLon);

        writer.putFloat(this.endLat);
        writer.putFloat(this.endAlt);
        writer.putFloat(this.endLon);

        writer.putInt(this.startLocType);
        writer.putInt(this.inBuildingUUID);

        writer.putInt(this.inBuilding);
        writer.putInt(this.inBuildingFloor);

        writer.put((byte) 0);
        writer.put((byte) 0);

    }

    /**
     * Deserializes the subclass specific items from the supplied NetMsgReader.
     */
    @Override
    protected void _deserialize(ByteBufferReader reader) {
        this.sourceType = reader.getInt();
        this.sourceID = reader.getInt();

        this.startLat = reader.getFloat();
        this.startAlt = reader.getFloat();
        this.startLon = reader.getFloat();

        this.endLat = reader.getFloat();
        this.endAlt = reader.getFloat();
        this.endLon = reader.getFloat();

        this.startLocType = reader.getInt();
        this.inBuildingUUID = reader.getInt();

        this.inBuilding = reader.getInt();
        this.inBuildingFloor = reader.getInt();

        this.initiatedByAttack = reader.get();
        this.unknown03 = reader.get();
    }

    public int getSourceType() {
        return this.sourceType;
    }

    public void setSourceType(int value) {
        this.sourceType = value;
    }

    public int getSourceID() {
        return this.sourceID;
    }

    public void setSourceID(int value) {
        this.sourceID = value;
    }

    public float getStartLat() {
        return this.startLat;
    }

    public void setStartLat(float value) {
        this.startLat = value;
    }

    public float getStartLon() {
        return this.startLon;
    }

    public void setStartLon(float value) {
        this.startLon = value;
    }

    public float getStartAlt() {
        return this.startAlt;
    }

    public void setStartAlt(float value) {
        this.startAlt = value;
    }

    public float getEndLat() {
        return this.endLat;
    }

    public void setEndLat(float value) {
        this.endLat = value;
    }

    public float getEndLon() {
        return this.endLon;
    }

    public void setEndLon(float value) {
        this.endLon = value;
    }

    public float getEndAlt() {
        return this.endAlt;
    }

    public void setEndAlt(float value) {
        this.endAlt = value;
    }

    public int getStartLocType() {
        return this.startLocType;
    }

    public void setStartLocType(int value) {
        this.startLocType = value;
    }

    public int getInBuildingUUID() {
        return this.inBuildingUUID;
    }

    public void setInBuildingUUID(int value) {
        this.inBuildingUUID = value;
    }

    public int getInBuilding() {
        return this.inBuilding;
    }

    public void setInBuilding(int value) {
        this.inBuilding = value;
    }

    public void setInBuildingFloor(int value) {
        this.inBuildingFloor = value;
    }

    public void setStartCoord(Vector3fImmutable value) {
        this.startLat = value.x;
        this.startAlt = value.y;
        this.startLon = value.z;
    }

    public void setEndCoord(Vector3fImmutable value) {
        this.endLat = value.x;
        this.endAlt = value.y;
        this.endLon = value.z;
    }

    public void clearTarget() {
        this.startLocType = 0;
        this.inBuildingUUID = 0;
    }

    public void setPlayer(AbstractCharacter ac) {
        this.sourceType = ac.getObjectType().ordinal();
        this.sourceID = ac.getObjectUUID();
        this.setStartCoord(ac.getLoc());
        this.setEndCoord(ac.getEndLoc());
        this.startLocType = 0;
        this.inBuildingUUID = 0;
        this.inBuilding = ac.getInBuilding();
        this.inBuildingFloor = ac.getInFloorID();

    }

    public void setTarget(AbstractCharacter ac, Building target) {
        if (target == null) {
            this.setStartCoord(ac.getLoc());
            this.setEndCoord(ac.getEndLoc());
            this.startLocType = 0;
            this.inBuildingUUID = 0;
            this.inBuilding = -1;
            this.inBuildingFloor = -1;
        } else {
            Vector3fImmutable convertLocStart = ZoneManager.convertWorldToLocal(target, ac.getLoc());
            Vector3fImmutable convertLocEnd = convertLocStart;
            if (ac.isMoving())
                convertLocEnd = ZoneManager.convertWorldToLocal(target, ac.getEndLoc());

            this.setStartCoord(convertLocStart);
            this.setEndCoord(convertLocEnd);
            this.startLocType = GameObjectType.Building.ordinal();
            this.inBuildingUUID = target.getObjectUUID();
            this.inBuilding = ac.getInBuilding();
            this.inBuildingFloor = ac.getInFloorID();
        }

    }

    public int getUnknown03() {
        return unknown03;
    }

    public int getInitiatedFromAttack() {
        return initiatedByAttack;
    }

    public int getInBuildingFloor() {
        return this.inBuildingFloor;
    }
}
