#include"..\Resources.txt"

function CreateDialogueEvent {
    let imgpathC = CSD ~ "./img/Convo/Cirno_1.png";
    let objImgLeft = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objImgLeft, 81);
    ObjPrim_SetTexture(objImgLeft, imgpathC);
    ObjSprite2D_SetSourceRect(objImgLeft, 0, 0, 623, 525);
    ObjSprite2D_SetDestCenter(objImgLeft);
	ObjRender_SetScaleXYZ(objImgLeft,0.68,0.7,0);
    ObjRender_SetX(objImgLeft, GetStgFrameLeft() + GetStgFrameWidth()/2 - 100);
    ObjRender_SetY(objImgLeft, 288);
    let imgpathE = CSD ~ "./img/Convo/Enemy.png";
    let objImgRight = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objImgRight, 81);
    ObjPrim_SetTexture(objImgRight, imgpathE);
    ObjSprite2D_SetSourceRect(objImgRight, 0, 0, 753, 493);
    ObjSprite2D_SetDestCenter(objImgRight);
	ObjRender_SetScaleXYZ(objImgRight,0.68,0.7,0);
    ObjRender_SetX(objImgRight, GetStgFrameLeft() + GetStgFrameWidth()/2 + 250);
    ObjRender_SetY(objImgRight, 288);
    // Prepare text background. We will use render priority 83.
    let TEXTBOX_BUFFER = 16; // Buffers on edges
    let TEXTBOX_WIDTH = GetStgFrameWidth - TEXTBOX_BUFFER*2;
    let TEXTBOX_HEIGHT = 48;
    let objTextBack = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objTextBack, 83);
    ObjSprite2D_SetSourceRect(objTextBack, 0, 0, 1, 1); // Use default white texture
    ObjSprite2D_SetDestRect(objTextBack, 0, 0, TEXTBOX_WIDTH, TEXTBOX_HEIGHT);
    ObjRender_SetX(objTextBack, GetStgFrameLeft() + TEXTBOX_BUFFER);
    ObjRender_SetY(objTextBack, GetStgFrameTop() + GetStgFrameHeight() - TEXTBOX_HEIGHT - TEXTBOX_BUFFER);
    ObjRender_SetColor(objTextBack, 0, 0, 0);
    ascent(i in 0..30){
        ObjRender_SetAlpha(objTextBack, 192/30*i);
        yield;
    }
    ObjRender_SetAlpha(objTextBack, 192);
    // Prepare text object. We will use render priority 83.
    let TEXT_MARGIN_LR = 8; // Left and Right Margins.
    let TEXT_MARGIN_TOP = 6; // Top Margin
    let TEXT_MAXWIDTH = TEXTBOX_WIDTH - TEXT_MARGIN_LR*2;
    let objText = ObjText_Create();
    ObjText_SetText(objText, "Default[r]Default");
    ObjText_SetFontSize(objText, 15);
    ObjText_SetFontBold(objText, false);
    ObjText_SetFontType(objText,"Fragment Core");
    Obj_SetRenderPriorityI(objText, 83);
    ObjText_SetFontColorTop(objText, 255, 255, 255); //Will be altered inside the event
    ObjText_SetFontColorBottom(objText, 255, 255, 255);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 255, 255, 255);
    ObjText_SetFontBorderWidth(objText, 1);
    //ObjText_SetMaxWidth(objText, TEXT_MAXWIDTH);
    ObjRender_SetX(objText, GetStgFrameLeft() + TEXTBOX_BUFFER + TEXT_MARGIN_LR);
    ObjRender_SetY(objText, GetStgFrameTop() + GetStgFrameHeight() - TEXTBOX_HEIGHT - TEXTBOX_BUFFER + TEXT_MARGIN_TOP);
    return [objTextBack, objText, objImgLeft, objImgRight];
}
// Given an array of object IDs, destroys all of them
function DestroyDialogueEvent(objects) {
    ascent(i in 0..length(objects)) {
        Obj_Delete(objects[i]);
    }
}
// On player key press, wait specified number of frames
function EventWaitA1(frames) {
    while(GetVirtualKeyState(VK_OK) == KEY_FREE || GetVirtualKeyState(VK_OK) == KEY_HOLD){yield;}
    loop(frames){yield;}
}
// Timed wait for specified number of frames
function EventWaitA2(frames) {
    loop(frames){yield;}
}
// At start of event, wait for key to be released, wait for key to be pressed, and then wait specified number of frames
function EventWaitA3(frames) {
    while(GetVirtualKeyState(VK_OK) == KEY_HOLD){yield;}
    while(GetVirtualKeyState(VK_OK) != KEY_PUSH){yield;}
    loop(frames){yield;} 
}
// Sets portrait to background
function SetPortraitToBackground(portrait) {
    ObjRender_SetAlpha(portrait, 192);
    ObjRender_SetColor(portrait, 192, 192, 192);
}
// Sets portrait to foreground
function SetPortraitToForeground(portrait) {
    ObjRender_SetAlpha(portrait, 255);
    ObjRender_SetColor(portrait, 255, 255, 255);
}

function autoformLineInsertionA2(input, parent, maxwidth) {
    let output = "";
    let tokenized = SplitString(input, " "); // Split the string into an array using the space " " as a delimiter
    while (length(tokenized) > 0) { // While there remain tokens to be processed
        let newline = ""; // Prepare the next line of text
        let ok = false; // Boolean for loop control
        while (!ok && length(tokenized) > 0) {
            // First, add the next token to the new line
            let backup = newline; // Used to restore newline in case of overflow
            newline = newline ~ tokenized[0] ~ " "; // Space is used as a buffer

            // Check if the new line is in bounds
            ObjText_SetText(parent, newline);

            let width = ObjText_GetTotalWidth(parent);

            // If in bounds, proceed with next token and erase the used token from the token array
            // Otherwise, the current line is complete. Exit loop.
            if (width < maxwidth) {
                tokenized = erase(tokenized, 0);
            } else {
                newline = backup; // Restore backup
                ok = true;
            }
        }
        output = output ~ newline ~ "[r]";
    }
    ObjText_SetText(parent, output);
    return output;
}