#import "DialogueBox.h"

@implementation DialogueBox

Rectangle make_border(Rectangle r) {
    return (Rectangle) {
        .x = r.x - thiccness,
        .y = r.y - thiccness,
        .width = r.width + thiccness + thiccness,
        .height = r.height + thiccness + thiccness,
    };
}

-(id) initWithText: (char *) text {
    self = [super init];

    if (self) {
        full_text = text;
        pos = (Vector2){0, 0};
        rect = (Rectangle){50, 450, 700, 100};
        inner_color = WHITE;
        outer_color = BLACK;
        text_color = BLACK;
        thiccness = 2;
        line_top = malloc(100);
        speed = 10;
        //line_bottom = malloc(100);
        memcpy(line_top, full_text, 100); //segfault?
    }

    return self;
}

+(id) newWithText: (char *) text {
    return [[DialogueBox alloc] initWithText: text];
}

-(void) setSpeed: (int) s {
    speed = s;
}

-(void) tick {
    left = speed;    
    
    if (left-- == 0) {
        left = speed;
    }
}

-(void) advance {
}

void swap_char(char *str, int len) {
    //might need to add reset function
    //or not use static variables.
    static int idx;
    static char chr;

    idx = idx % len;
    
    //put back char
    if (idx) {
        str[idx - 1] = chr;
    }

    chr = str[idx]; //save char
    
    str[idx++] = 0; //zero char
}

-(void) draw {
    DrawRectangleRec(make_border(rect), outer_color);
    DrawRectangleRec(rect, inner_color);
    static char c;
    static int idx;

    if (left == 0) {
        swap_char(line_top, 100);
        idx = (idx + 1) % 100;
        //idx = (idx + 1) % len(current_text)
    }

    //line_top[idx] = full_text[idx];


    DrawText(line_top, rect.x + 1, rect.y, 20, text_color);
}

@end