local ffi = require("ffi")
local C = ffi.C
local core = require("pxl8.core")

local transition = {}

local TYPES = {
    fade = 0,
    wipe_left = 1,
    wipe_right = 2,
    wipe_up = 3,
    wipe_down = 4,
    circle_open = 5,
    circle_close = 6,
    dissolve = 7,
    pixelate = 8
}

transition.TYPES = TYPES

local Transition = {}
Transition.__index = Transition

function Transition.new(type_name, duration)
    local type_id = TYPES[type_name] or 0
    local t = C.pxl8_transition_create(type_id, duration or 1.0)
    if t == nil then
        return nil
    end
    return setmetatable({ _ptr = t }, Transition)
end

function Transition:destroy()
    if self._ptr then
        C.pxl8_transition_destroy(self._ptr)
        self._ptr = nil
    end
end

function Transition:get_progress()
    return C.pxl8_transition_get_progress(self._ptr)
end

function Transition:is_active()
    return C.pxl8_transition_is_active(self._ptr)
end

function Transition:is_complete()
    return C.pxl8_transition_is_complete(self._ptr)
end

function Transition:render()
    C.pxl8_transition_render(self._ptr, core.gfx)
end

function Transition:reset()
    C.pxl8_transition_reset(self._ptr)
end

function Transition:set_color(color)
    C.pxl8_transition_set_color(self._ptr, color)
end

function Transition:set_reverse(reverse)
    C.pxl8_transition_set_reverse(self._ptr, reverse)
end

function Transition:start()
    C.pxl8_transition_start(self._ptr)
end

function Transition:stop()
    C.pxl8_transition_stop(self._ptr)
end

function Transition:update(dt)
    C.pxl8_transition_update(self._ptr, dt)
end

transition.Transition = Transition

return transition
