const sdl = @import("./../sdl.zig").c;

pub const TimerStatus = enum { started, stopped, paused };

pub const Timer = struct {
    start_ticks_ms: u32 = 0,
    pause_ticks_ms: u32 = 0,
    status: TimerStatus = TimerStatus.stopped,

    pub fn start(self: *Timer) void {
        if (self.status == .started) return;
        self.status = .started;
        self.start_ticks_ms = sdl.SDL_GetTicks();
        self.pause_ticks_ms = 0;
    }

    pub fn stop(self: *Timer) void {
        if (self.status == .stopped) return;
        self.status = .stopped;
        self.start_ticks_ms = 0;
        self.pause_ticks_ms = 0;
    }

    pub fn reset(self: *Timer) void {
        self.status = .started;
        self.start_ticks_ms = sdl.SDL_GetTicks();
        self.pause_ticks_ms = 0;
    }

    pub fn pause(self: *Timer) void {
        if (self.status == .paused or self.status == .stopped) return;
        self.status = .paused;
        self.pause_ticks_ms = sdl.SDL_GetTicks();
    }

    pub fn unpause(self: *Timer) void {
        if (self.status == .started or self.status == .stopped) return;
        self.status = .started;
        self.start_ticks_ms = sdl.SDL_GetTicks() - self.pause_ticks_ms;
        self.pause_ticks_ms = 0;
    }

    pub fn getTicks(self: *Timer) u32 {
        switch (self.status) {
            .stopped => {
                return 0;
            },
            .paused => {
                return self.pause_ticks_ms;
            },
            .started => {
                return sdl.SDL_GetTicks() - self.start_ticks_ms;
            },
        }
    }
};
