"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageEntry = exports.MessageOcarinaInfo = exports.FontContext = void 0;
const MessageParser_1 = __importDefault(require("../Common/MessageParser"));
class FontContext {
    ModLoader;
    Ptr;
    constructor(ModLoader, _Ptr) {
        this.ModLoader = ModLoader;
        this.Ptr = _Ptr;
    }
    get MessageOffsetPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x000000); }
    ;
    get MessageLengthPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x000004); }
    ;
    get FontTextureBuffer() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x000008, 0x003C00); }
    ;
    get IconBuffer() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x003C08, 0x000080); }
    ;
    get FontBuffer() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x003C88, 0x001400); }
    ;
    get MessageBufferPtr() { return (this.Ptr + 0x00DC88); }
    ;
    get MessageBuffer() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x00DC88, 0x000500); }
    ;
    set MessageOffsetPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x000000, _p); }
    ;
    set MessageLengthPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x000004, _p); }
    ;
    set FontTextureBuffer(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x000008, _p); }
    ;
    set IconBuffer(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x003C08, _p); }
    ;
    set FontBuffer(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x003C88, _p); }
    ;
    set MessageBuffer(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x00DC88, _p); }
    ;
}
exports.FontContext = FontContext;
class MessageOcarinaInfo {
    ModLoader;
    Ptr;
    constructor(ModLoader, _Ptr) {
        this.ModLoader = ModLoader;
        this.Ptr = _Ptr;
    }
    get Button() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x000000); }
    ;
    get Status() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x000001); }
    ;
    get Location() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x000002); }
    ;
    set Button(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x000000, _p); }
    ;
    set Status(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x000001, _p); }
    ;
    set Location(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x000002, _p); }
    ;
}
exports.MessageOcarinaInfo = MessageOcarinaInfo;
class MessageBoxFunctions {
    MessageTablePtr = 0x8010EA8C;
    Core;
    ModLoader;
    constructor(Core, ModLoader) {
        this.Core = Core;
        this.ModLoader = ModLoader;
    }
    Message_GetState(msgCtx) {
        let state;
        if (msgCtx.MessageDataPtr === 0) {
            state = 0 /* TextStates.NONE */;
        }
        else if (msgCtx.MessageMode === 53 /* MessageModes.TEXT_DONE */) {
            if (msgCtx.SelectionMode === 48 /* MessageSelectionModes.HAS_NEXT_TEXTID */) {
                state = 1 /* TextStates.DONE_HAS_NEXT */;
            }
            else if (msgCtx.SelectionMode === 16 /* MessageSelectionModes.TWO_CHOICE */ || msgCtx.SelectionMode === 32 /* MessageSelectionModes.THREE_CHOICE */) { // choice text
                state = 4 /* TextStates.CHOICE */;
            }
            else if (msgCtx.SelectionMode === 80 /* MessageSelectionModes.EVENT */ || msgCtx.SelectionMode === 64 /* MessageSelectionModes.PERSISTENT */) { // event or persistent
                state = 5 /* TextStates.EVENT */;
            }
            else if (msgCtx.SelectionMode === 96 /* MessageSelectionModes.FADING */) {
                state = 3 /* TextStates.DONE_FADING */;
            }
            else {
                state = 6 /* TextStates.DONE */;
            }
        }
        else if (msgCtx.MessageMode === 52 /* MessageModes.TEXT_AWAIT_NEXT */) {
            state = 10 /* TextStates.AWAITING_NEXT */;
        }
        else if (msgCtx.MessageMode === 26 /* MessageModes.OCARINA_DONE_PLAYING */) {
            state = 7 /* TextStates.DEMO_DONE */;
        }
        else if (msgCtx.OcarinaMode === 3) { // ocarina related
            state = 8 /* TextStates.OCARINA_CORRECT */;
        }
        else if (msgCtx.MessageMode === 31 /* MessageModes.OCARINA_ERROR_WAIT */) {
            state = 9 /* TextStates.OCARINA_ERROR */;
        }
        else if (msgCtx.MessageMode === 54 /* MessageModes.TEXT_CLOSING */ && msgCtx.StateTimer === 1) {
            state = 2 /* TextStates.CLOSING */;
        }
        else {
            state = 3 /* TextStates.DONE_FADING */;
        }
        return state;
    }
    Message_GetTableEntry(MessageID) {
        let EntryIndex = 0;
        for (let i = 0; i < 0xFFFF; i++) {
            let m = (this.MessageTablePtr + (i * 8));
            if (this.ModLoader.emulator.rdramRead16(m) == MessageID) {
                EntryIndex = i;
                break;
            }
        }
        return EntryIndex;
    }
    Message_ModifyTableEntry(CustomMessage) {
        let m = (this.MessageTablePtr + (this.Message_GetTableEntry(CustomMessage.MessageID) * 8));
        let box = ((CustomMessage.MessageBoxType << 4) | (CustomMessage.MessageBoxPosition));
        this.ModLoader.emulator.rdramWrite8(m + 2, box);
    }
    Message_Spawn(MessageCtx, Message) {
        this.Message_ModifyTableEntry(Message);
        let paramArray = [0x801C84A0, Message.MessageID, 0x801DAA30];
        let pAllocation = 0;
        let fnAddress = 0x800DCE14;
        this.ModLoader.utils.setTimeoutFrames(() => {
            if (pAllocation === 0) {
                pAllocation = this.ModLoader.heap.malloc(paramArray.length * 4);
                for (let i = 0; i < paramArray.length; i++) {
                    this.ModLoader.emulator.rdramWrite32(pAllocation + (i * 4), paramArray[i]);
                }
            }
            this.Core.commandBuffer.arbitraryFunctionCall(fnAddress, pAllocation, paramArray.length).then(() => {
                this.ModLoader.heap.free(pAllocation);
                if (this.ModLoader.emulator.rdramRead32(MessageCtx.Font.MessageBufferPtr) === 0x30313161) {
                    MessageCtx.Font.MessageBuffer = (0, MessageParser_1.default)(Message.MessageString);
                }
            });
        }, 1);
    }
}
class MessageContext extends MessageBoxFunctions {
    Ptr;
    constructor(Core, ModLoader, _Ptr) {
        super(Core, ModLoader);
        this.ModLoader = ModLoader;
        this.Ptr = _Ptr;
    }
    /* get View(): ViewContext { return new ViewContext(this.ModLoader, (this.Ptr + 0x000000)) }; */
    get Font() { return new FontContext(this.ModLoader, (this.Ptr + 0x000128)); }
    ;
    get MessageBoxSegmentPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x00E2B0); }
    ;
    get TextureSegmentPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x00E2B4); }
    ;
    get OcarinaInfoPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x00E2B8); }
    ;
    get OcarinaInfo() { return new MessageOcarinaInfo(this.ModLoader, (this.Ptr + 0x00E2B8)); }
    ;
    /* get DMA(): DirectMemoryAccess { return new DirectMemoryAccess(this.ModLoader, (this.Ptr + 0x00E2BC)) }; */
    /* get OSMessageQueue(): OSMesgQueue { return new OSMesgQueue(this.ModLoader, (this.Ptr + 0x00E2DC)) }; */
    /* get OSMessage(): OSMesg { return new OSMesg(this.ModLoader, (this.Ptr + 0x00E2F4)) }; */
    get MessageID() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E2F8); }
    ;
    get SelectedMessageID() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E2FA); }
    ;
    get MessageBoxProperties() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E2FC); }
    ;
    get MessageBoxType() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E2FD); }
    ;
    get MessageBoxPosition() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E2FE); }
    ;
    get MessageDataPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x00E300); }
    ;
    get MessageMode() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E304); }
    ;
    get MessageBufferRead() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x00E306, 0x0000C8); }
    ;
    get ReadIndex() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3CE); }
    ;
    get LoadIndex() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3D0); }
    ;
    get LoadEndIndex() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3D2); }
    ;
    get ReadEndIndex() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3D4); }
    ;
    get FastForwardMessageTimer() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3D6); }
    ;
    get CharacterXPosition() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3D8); }
    ;
    get CharacterYPosition() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3DA); }
    ;
    get CharacterColor() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x00E3DC, 0x000008); }
    ;
    get SelectionMode() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3E4); }
    ;
    get MessageChoiceIndex() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3E5); }
    ;
    get ItemDisplayFlag() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3E6); }
    ;
    get StateTimer() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3E7); }
    ;
    get MessageSpeed() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3E8); }
    ;
    get MessageSpeedOriginal() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3EA); }
    ;
    get OcarinaLastPlayedSong() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3EC); }
    ;
    get OcarinaMode() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3EE); }
    ;
    get OcarinaAction() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3F0); }
    ;
    get OcarinaCheckIndex() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3F2); }
    ;
    get OcarinaSunSongFlag() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3F4); }
    ;
    get MessageBoxNumber() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E3F6); }
    ;
    get MessageBoxPrimitiveColorIndex() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3F8); }
    ;
    get MessageBoxShadowColor() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3F9); }
    ;
    get MessageBoxShift() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3FA); }
    ;
    get MessageBoxAnimation() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3FB); }
    ;
    get MessageBoxType2() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E3FC); }
    ;
    get MessageBoxColor() { return this.ModLoader.emulator.rdramReadBuffer(this.Ptr + 0x00E3FE, 0x000008); }
    ;
    get MessageBoxCurrentAlpha() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E406); }
    ;
    get TalkingActorPtr() { return this.ModLoader.emulator.rdramRead32(this.Ptr + 0x00E408); }
    ;
    get DisableWarpSongsBool() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E40C); }
    ;
    get SunSongChoice() { return this.ModLoader.emulator.rdramRead16(this.Ptr + 0x00E40E); }
    ;
    get LastOcarinaNoteIndex() { return this.ModLoader.emulator.rdramRead8(this.Ptr + 0x00E410); }
    ;
    /* set View(_p: ViewContext) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x000000, _p) }; */
    /* set Font(_p: FontContext) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x000128, _p) }; */
    set MessageBoxSegmentPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x00E2B0, _p); }
    ;
    set TextureSegmentPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x00E2B4, _p); }
    ;
    set OcarinaInfoPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x00E2B8, _p); }
    ;
    /* set OcarinaInfo(_p: MessageOcarinaInfo) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x00E2B8, _p) }; */
    /* set DMA(_p: DirectMemoryAccess) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x00E2BC, _p) }; */
    /* set OSMessageQueue(_p: OSMesgQueue) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x00E2DC, _p) }; */
    /* set OSMessage(_p: OSMesg) { this.ModLoader.emulator.rdramWriteXX(this.Ptr + 0x00E2F4, _p) }; */
    set MessageID(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E2F8, _p); }
    ;
    set SelectedMessageID(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E2FA, _p); }
    ;
    set MessageBoxProperties(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E2FC, _p); }
    ;
    set MessageBoxType(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E2FD, Number(_p)); }
    ;
    set MessageBoxPosition(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E2FE, Number(_p)); }
    ;
    set MessageDataPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x00E300, _p); }
    ;
    set MessageMode(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E304, _p); }
    ;
    set MessageBufferRead(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x00E306, _p); }
    ;
    set ReadIndex(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3CE, _p); }
    ;
    set LoadIndex(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3D0, _p); }
    ;
    set LoadEndIndex(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3D2, _p); }
    ;
    set ReadEndIndex(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3D4, _p); }
    ;
    set FastForwardMessageTimer(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3D6, _p); }
    ;
    set CharacterXPosition(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3D8, _p); }
    ;
    set CharacterYPosition(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3DA, _p); }
    ;
    set CharacterColor(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x00E3DC, _p); }
    ;
    set SelectionMode(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3E4, _p); }
    ;
    set MessageChoiceIndex(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3E5, _p); }
    ;
    set ItemDisplayFlag(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3E6, _p); }
    ;
    set StateTimer(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3E7, _p); }
    ;
    set MessageSpeed(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3E8, _p); }
    ;
    set MessageSpeedOriginal(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3EA, _p); }
    ;
    set OcarinaLastPlayedSong(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3EC, _p); }
    ;
    set OcarinaMode(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3EE, _p); }
    ;
    set OcarinaAction(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3F0, _p); }
    ;
    set OcarinaCheckIndex(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3F2, _p); }
    ;
    set OcarinaSunSongFlag(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3F4, _p); }
    ;
    set MessageBoxNumber(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E3F6, _p); }
    ;
    set MessageBoxPrimitiveColorIndex(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3F8, _p); }
    ;
    set MessageBoxShadowColor(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3F9, _p); }
    ;
    set MessageBoxShift(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3FA, _p); }
    ;
    set MessageBoxAnimation(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3FB, _p); }
    ;
    set MessageBoxType2(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E3FC, Number(_p)); }
    ;
    set MessageBoxColor(_p) { this.ModLoader.emulator.rdramWriteBuffer(this.Ptr + 0x00E3FE, _p); }
    ;
    set MessageBoxCurrentAlpha(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E406, _p); }
    ;
    set TalkingActorPtr(_p) { this.ModLoader.emulator.rdramWrite32(this.Ptr + 0x00E408, _p); }
    ;
    set DisableWarpSongsBool(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E40C, _p); }
    ;
    set SunSongChoice(_p) { this.ModLoader.emulator.rdramWrite16(this.Ptr + 0x00E40E, _p); }
    ;
    set LastOcarinaNoteIndex(_p) { this.ModLoader.emulator.rdramWrite8(this.Ptr + 0x00E410, _p); }
    ;
}
exports.default = MessageContext;
class MessageEntry {
    MessageID;
    MessageBoxType;
    MessageBoxPosition;
    MessageString;
    ChoiceCallback;
    constructor(id, type, pos, msg, callback) {
        this.MessageID = id;
        this.MessageBoxType = type;
        this.MessageBoxPosition = pos;
        this.MessageString = msg;
        this.ChoiceCallback = callback;
    }
    ChoiceFunction(choiceIndex) {
        if (this.ChoiceCallback !== undefined) {
            this.ChoiceCallback(choiceIndex);
        }
    }
}
exports.MessageEntry = MessageEntry;
/*
Here's a lovely example of setting up choice options in a custom message.
CustomMessage = new MessageEntry(
    0x011A,
    MessageBoxTypes.NORMAL,
    MessageBoxPositions.BOTTOM,
    "Aren't you a good-looking character?\n[ch2][color=0x42]\nYes\nNo[/color]",
    (choiceIndex: number) => {
        if (Message_GetState(this.MessageCtx) === TextStates.CLOSING) {
            switch(choiceIndex) {
                case 0: { Yes } break;
                case 1: { No } break;
                case 2: { Option 3 if Applicable } break;
                default: break;
            }
        }
    }
);
*/ 
//# sourceMappingURL=MessageContext.js.map