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


package engine.net.client.msg;

import engine.net.AbstractConnection;
import engine.net.ByteBufferReader;
import engine.net.ByteBufferWriter;
import engine.net.Protocol;
import engine.objects.MenuOption;
import engine.objects.VendorDialog;

import java.util.ArrayList;

public class VendorDialogMsg extends ClientNetMsg {

    public static final int MSG_TYPE_VENDOR = 0;
    public static final int MSG_TYPE_TRAINER = 1;
    public static int cnt = 1;
    public int messageType;
    public String language;
    public int vendorObjectType;
    public int vendorObjectID;
    public int unknown01;
    public int unknown02;
    public int unknown03;
    public int unknown04;
    public String dialogType = "TrainerDialog";
    public String intro = "FighterIntro";
    public String introCode = " [ FighterIntro ] ";
    public String merchantCode = " [ Merchant options ] ";
    public int menuType = 1; // 0: close, 1: normal, 2: train, 3: untrain
    public VendorDialog vd;

    /**
     * This is the general purpose constructor.
     */
    public VendorDialogMsg(int messageType, int vendorObjectType, int vendorObjectID, String dialogType, String intro, int menuType) {
        super(Protocol.VENDORDIALOG);
        this.messageType = messageType;
        this.vendorObjectType = vendorObjectType;
        this.vendorObjectID = vendorObjectID;
        this.dialogType = dialogType;
        this.intro = intro;
        this.introCode = " [ " + intro + " ] ";
        this.menuType = menuType;
    }

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

    /**
     * Serializes the subclass specific items to the supplied ByteBufferWriter.
     */
    @Override
    protected void _serialize(ByteBufferWriter writer) {

        writer.putInt(this.messageType);
        for (int i = 0; i < 3; i++)
            writer.putInt(0);
        if (messageType == 1)
            writer.putString(this.language);
        else
            writer.putString("");
        writer.putInt(this.vendorObjectType);
        writer.putInt(this.vendorObjectID);

        for (int i = 0; i < 3; i++)
            writer.putInt(0);
        writer.put((byte) 0);
        writer.put((byte) 0);
        if (this.messageType == 1) {
            writer.putInt(this.unknown01);
            writer.putInt(this.unknown02);
        } else if (this.messageType == 4) {
            writer.putInt(0);
            writer.putInt(0x364AF0D0); // 0x325695C0
            if (this.vd != null)
                writer.putInt(vd.getObjectUUID());
            else
                writer.putInt(this.unknown03);
            writer.putInt(1);
            writer.put((byte) 0);
            writer.putInt(this.unknown03);
            writer.put((byte) 0);
            writer.putInt(0);
            writer.putInt(0);
            writer.putInt(2); // 2
            if (menuType == 2)
                writer.putInt(3);
            else if (menuType == 3)
                writer.putInt(4);
            else
                writer.putInt(5);
            //			writer.putInt(3); // 3=buy/sell/trade, 4=untrain, 5 closes
            writer.putInt(10); // 10
            writer.putInt(0);
            writer.putInt(0);
            writer.putInt(0);
            return;

        }
        writer.putInt(15);
        writer.putInt(5);
        if (this.vd != null)
            writer.putInt(vd.getObjectUUID());
        else
            writer.putInt(this.unknown03);

        //filename datablock
        writer.putInt(1);
        writer.put((byte) 1);
        writer.putInt(this.unknown03);
        writer.put((byte) 1);
        writer.putString(vd.getDialogType());
        writer.putInt(0);
        writer.putInt(1);
        writer.putInt(4);
        writer.putInt(0);
        writer.put((byte) 1);

        //vendor dialog datablock
        writer.putString(vd.getDialogType());
        writer.putString(vd.getIntro());
        writer.put((byte) 0);

        //menu options datablock
        writer.putInt(1);
        writer.putString(" [ " + vd.getIntro() + " ] ");
        ArrayList<MenuOption> options = vd.getOptions();
        writer.putInt((options.size() + 1));
        for (MenuOption option : options) {
            if (option.getMessage().equals(" [ Merchant options ] ")) {
                writer.putInt(16);
            } else {
                writer.putInt(14);
            }
            writer.put((byte) 0);
            writer.putInt(0);
            writer.put((byte) 0);
            writer.putInt(1);
            writer.putString(option.getMessage());
            writer.putInt(option.getOptionID());
            for (int i = 0; i < 3; i++)
                writer.putInt(0);
        }
        writer.putInt(10);
        writer.put((byte) 0);
        writer.putInt(0);
        writer.put((byte) 0);
        writer.putInt(1);
        writer.putString("Done");
        for (int i = 0; i < 4; i++)
            writer.putInt(0);
        //		writer.putInt(1);
        //		writer.putInt(2);
        for (int i = 0; i < 4; i++)
            writer.putInt(0);
    }

    /**
     * Deserializes the subclass specific items from the supplied
     * ByteBufferReader.
     */
    @Override
    protected void _deserialize(ByteBufferReader reader) {
        this.messageType = reader.getInt();
        for (int i = 0; i < 3; i++)
            reader.getInt();
        this.language = reader.getString();
        this.vendorObjectType = reader.getInt();
        this.vendorObjectID = reader.getInt();
        for (int i = 0; i < 3; i++)
            reader.getInt();
        reader.get();
        reader.get();
        this.unknown01 = reader.getInt();
        this.unknown02 = reader.getInt();
        this.unknown03 = reader.getInt();
        reader.getInt();
        // if (this.messageType == 1) {
        reader.get();
        reader.getInt();
        reader.get();
        reader.getInt();
        reader.getShort();
        // return;
        // }
        // reader.get();
        // this.unknown04 = reader.getInt();
        // reader.get();
        // TODO more message to go here
    }

    public void updateMessage(int messageType, int menuType) {
        this.messageType = messageType;
        this.menuType = menuType;
    }

    public void updateMessage(int messageType, VendorDialog vd) {
        this.messageType = messageType;
        this.vd = vd;
    }
}
