const std = @import("std");
pub const c = @import("c.zig").c;

pub const PixelFormat = enum(c_int) {
    pixelformatYv12 = 0x32315659, //Planar mode: Y + V + U  (3 planes)
    pixelformatIyuv = 0x56555949, //Planar mode: Y + U + V  (3 planes)
    pixelformatYuy2 = 0x32595559, //Packed mode: Y0+U0+Y1+V0 (1 plane)
    pixelformatUyvy = 0x59565955, //Packed mode: U0+Y0+V0+Y1 (1 plane)
    pixelformatYvyu = 0x55595659, //Packed mode: Y0+V0+Y1+U0 (1 plane)
    pixelformatNv12 = 0x3231564e, //Planar mode: Y + U/V interleaved  (2 planes)
    pixelformatNv21 = 0x3132564e, //Planar mode: Y + V/U interleaved  (2 planes)
    pixelformatP010 = 0x30313050, //Planar mode: Y + U/V interleaved  (2 planes)
    pixelformatExternalOes = 0x2053454f, //Android video texture format
    pixelformatMjpg = 0x47504a4d, //Motion JPEG
};

pub const Surface = opaque {};

pub const Colorspace = enum(c_int) {
    colorspaceSrgb = 0x120005a0, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
    colorRangeFull,
    colorPrimariesBt709,
    transferCharacteristicsSrgb,
    matrixCoefficientsIdentity,
    colorspaceSrgbLinear = 0x12000500, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709
    transferCharacteristicsLinear,
    colorspaceHdr10 = 0x12002600, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020
    colorPrimariesBt2020,
    transferCharacteristicsPq,
    colorspaceJpeg = 0x220004c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_FULL_G22_NONE_P709_X601
    transferCharacteristicsBt601,
    matrixCoefficientsBt601,
    colorspaceBt601Limited = 0x211018c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601
    colorRangeLimited,
    colorPrimariesBt601,
    colorspaceBt601Full = 0x221018c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601
    colorspaceBt709Limited = 0x21100421, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709
    transferCharacteristicsBt709,
    matrixCoefficientsBt709,
    colorspaceBt709Full = 0x22100421, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709
    colorspaceBt2020Limited = 0x21102609, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P2020
    matrixCoefficientsBt2020Ncl,
    colorspaceBt2020Full = 0x22102609, //Equivalent to DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P2020

    pub const colorspaceRgbDefault = .colorspaceSrgb; //The default colorspace for RGB surfaces if no colorspace is specified
    pub const colorspaceYuvDefault = .colorspaceJpeg; //The default colorspace for YUV surfaces if no colorspace is specified
};

pub const PropertiesID = u32;

pub const CameraID = u32;

pub const Camera = opaque {
    pub inline fn getCameraPermissionState(camera: *Camera) c_int {
        return c.SDL_GetCameraPermissionState(camera);
    }

    pub inline fn getCameraID(camera: *Camera) CameraID {
        return c.SDL_GetCameraID(camera);
    }

    pub inline fn getCameraProperties(camera: *Camera) PropertiesID {
        return c.SDL_GetCameraProperties(camera);
    }

    pub inline fn getCameraFormat(camera: *Camera, spec: ?*CameraSpec) bool {
        return @bitCast(c.SDL_GetCameraFormat(camera, spec));
    }

    pub inline fn acquireCameraFrame(camera: *Camera, timestampNS: *u64) ?*Surface {
        return c.SDL_AcquireCameraFrame(camera, @ptrCast(timestampNS));
    }

    pub inline fn releaseCameraFrame(camera: *Camera, frame: ?*Surface) void {
        return c.SDL_ReleaseCameraFrame(camera, frame);
    }

    pub inline fn closeCamera(camera: *Camera) void {
        return c.SDL_CloseCamera(camera);
    }
};

pub const CameraSpec = extern struct {
    format: PixelFormat, // Frame format
    colorspace: Colorspace, // Frame colorspace
    width: c_int, // Frame width
    height: c_int, // Frame height
    framerate_numerator: c_int, // Frame rate numerator ((num / denom) == FPS, (denom / num) == duration in seconds)
    framerate_denominator: c_int, // Frame rate demoninator ((num / denom) == FPS, (denom / num) == duration in seconds)
};

pub const CameraPosition = enum(c_int) {
    cameraPositionUnknown,
    cameraPositionFrontFacing,
    cameraPositionBackFacing,
};

pub inline fn getNumCameraDrivers() c_int {
    return c.SDL_GetNumCameraDrivers();
}

pub inline fn getCameraDriver(index: c_int) [*c]const u8 {
    return c.SDL_GetCameraDriver(index);
}

pub inline fn getCurrentCameraDriver() [*c]const u8 {
    return c.SDL_GetCurrentCameraDriver();
}

pub inline fn getCameras(count: *c_int) ?*CameraID {
    return c.SDL_GetCameras(@ptrCast(count));
}

pub inline fn getCameraSupportedFormats(instance_id: CameraID, count: *c_int) [*c][*c]CameraSpec {
    return c.SDL_GetCameraSupportedFormats(instance_id, @ptrCast(count));
}

pub inline fn getCameraName(instance_id: CameraID) [*c]const u8 {
    return c.SDL_GetCameraName(instance_id);
}

pub inline fn getCameraPosition(instance_id: CameraID) CameraPosition {
    return c.SDL_GetCameraPosition(instance_id);
}

pub inline fn openCamera(instance_id: CameraID, spec: *const CameraSpec) ?*Camera {
    return c.SDL_OpenCamera(instance_id, @ptrCast(spec));
}
