// Drawing lifecycle re-exports

///|
pub using @ffi {
  begin_drawing,
  end_drawing,
  begin_scissor_mode,
  end_scissor_mode,
  begin_blend_mode,
  end_blend_mode,
  end_texture_mode,
  end_shader_mode,
  end_vr_stereo_mode,
}

// Drawing wrapper

///|
pub fn clear_background(color : Color) -> Unit {
  @ffi.clear_background(color.to_bytes())
}

// Shader uniform type constants

///|
pub const ShaderUniformFloat : Int = 0

///|
pub const ShaderUniformVec2 : Int = 1

///|
pub const ShaderUniformVec3 : Int = 2

///|
pub const ShaderUniformVec4 : Int = 3

///|
pub const ShaderUniformInt : Int = 4

///|
pub const ShaderUniformIvec2 : Int = 5

///|
pub const ShaderUniformIvec3 : Int = 6

///|
pub const ShaderUniformIvec4 : Int = 7

///|
pub const ShaderUniformSampler2d : Int = 8

// Shader location index constants

///|
pub const ShaderLocVertexPosition : Int = 0

///|
pub const ShaderLocVertexTexcoord01 : Int = 1

///|
pub const ShaderLocVertexTexcoord02 : Int = 2

///|
pub const ShaderLocVertexNormal : Int = 3

///|
pub const ShaderLocVertexTangent : Int = 4

///|
pub const ShaderLocVertexColor : Int = 5

///|
pub const ShaderLocMatrixMvp : Int = 6

///|
pub const ShaderLocMatrixView : Int = 7

///|
pub const ShaderLocMatrixProjection : Int = 8

///|
pub const ShaderLocMatrixModel : Int = 9

///|
pub const ShaderLocMatrixNormal : Int = 10

///|
pub const ShaderLocVectorView : Int = 11

///|
pub const ShaderLocColorDiffuse : Int = 12

///|
pub const ShaderLocColorSpecular : Int = 13

///|
pub const ShaderLocColorAmbient : Int = 14

///|
pub const ShaderLocMapAlbedo : Int = 15

///|
pub const ShaderLocMapMetalness : Int = 16

///|
pub const ShaderLocMapNormal : Int = 17

///|
pub const ShaderLocMapRoughness : Int = 18

///|
pub const ShaderLocMapOcclusion : Int = 19

///|
pub const ShaderLocMapEmission : Int = 20

///|
pub const ShaderLocMapHeight : Int = 21

///|
pub const ShaderLocMapCubemap : Int = 22

///|
pub const ShaderLocMapIrradiance : Int = 23

///|
pub const ShaderLocMapPrefilter : Int = 24

///|
pub const ShaderLocMapBrdf : Int = 25

///|
pub const ShaderLocVertexBoneids : Int = 26

///|
pub const ShaderLocVertexBoneweights : Int = 27

///|
pub const ShaderLocBoneMatrices : Int = 28
// Blend mode constants

///|
pub const BlendAlpha : Int = 0

///|
pub const BlendAdditive : Int = 1

///|
pub const BlendMultiplied : Int = 2

///|
pub const BlendCustom : Int = 6

// ============================================================================
// VR stereo
// ============================================================================

///|
struct VrStereoConfig(@ffi.VrStereoConfig)

///|
#as_free_fn(unload_vr_stereo_config)
pub fn VrStereoConfig::unload(self : VrStereoConfig) -> Unit {
  @ffi.unload_vr_stereo_config(self.0)
}

///|
#as_free_fn(load_vr_stereo_config)
pub fn VrStereoConfig::load(device : VrDeviceInfo) -> VrStereoConfig {
  @ffi.load_vr_stereo_config(device.to_bytes())
}

///|
#as_free_fn(begin_vr_stereo_mode)
pub fn VrStereoConfig::begin_mode(self : VrStereoConfig) -> Unit {
  @ffi.begin_vr_stereo_mode(self.0)
}

// VrStereoConfig field accessors

///|
pub fn VrStereoConfig::left_lens_center(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_left_lens_center(self.0))
}

///|
pub fn VrStereoConfig::right_lens_center(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_right_lens_center(self.0))
}

///|
pub fn VrStereoConfig::left_screen_center(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_left_screen_center(self.0))
}

///|
pub fn VrStereoConfig::right_screen_center(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_right_screen_center(self.0))
}

///|
pub fn VrStereoConfig::scale(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_scale(self.0))
}

///|
pub fn VrStereoConfig::scale_in(self : VrStereoConfig) -> Vector2 {
  Vector2::from_bytes(@ffi.vr_stereo_config_scale_in(self.0))
}

///|
#deprecated("Use VrStereoConfig::left_lens_center instead")
pub fn vr_stereo_config_left_lens_center(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_left_lens_center(config.0)
}

///|
#deprecated("Use VrStereoConfig::right_lens_center instead")
pub fn vr_stereo_config_right_lens_center(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_right_lens_center(config.0)
}

///|
#deprecated("Use VrStereoConfig::left_screen_center instead")
pub fn vr_stereo_config_left_screen_center(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_left_screen_center(config.0)
}

///|
#deprecated("Use VrStereoConfig::right_screen_center instead")
pub fn vr_stereo_config_right_screen_center(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_right_screen_center(config.0)
}

///|
#deprecated("Use VrStereoConfig::scale instead")
pub fn vr_stereo_config_scale(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_scale(config.0)
}

///|
#deprecated("Use VrStereoConfig::scale_in instead")
pub fn vr_stereo_config_scale_in(config : VrStereoConfig) -> Bytes {
  @ffi.vr_stereo_config_scale_in(config.0)
}

// VrDeviceInfo value type: 60 bytes
// hResolution(int), vResolution(int), hScreenSize(float), vScreenSize(float),
// eyeToScreenDistance(float), lensSeparationDistance(float), interpupillaryDistance(float),
// lensDistortionValues[4](float), chromaAbCorrection[4](float)

///|
pub struct VrDeviceInfo {
  h_resolution : Int
  v_resolution : Int
  h_screen_size : Float
  v_screen_size : Float
  eye_to_screen_distance : Float
  lens_separation_distance : Float
  interpupillary_distance : Float
  lens_distortion_values : FixedArray[Float] // length 4
  chroma_ab_correction : FixedArray[Float] // length 4
} derive(Show)

///|
pub fn VrDeviceInfo::new(
  h_resolution : Int,
  v_resolution : Int,
  h_screen_size : Float,
  v_screen_size : Float,
  eye_to_screen_distance : Float,
  lens_separation_distance : Float,
  interpupillary_distance : Float,
  lens_distortion_values : FixedArray[Float],
  chroma_ab_correction : FixedArray[Float],
) -> VrDeviceInfo {
  {
    h_resolution,
    v_resolution,
    h_screen_size,
    v_screen_size,
    eye_to_screen_distance,
    lens_separation_distance,
    interpupillary_distance,
    lens_distortion_values,
    chroma_ab_correction,
  }
}

///|
pub fn VrDeviceInfo::to_bytes(info : VrDeviceInfo) -> Bytes {
  let buf = @buffer.new(size_hint=60)
  buf.write_int_le(info.h_resolution)
  buf.write_int_le(info.v_resolution)
  buf.write_float_le(info.h_screen_size)
  buf.write_float_le(info.v_screen_size)
  buf.write_float_le(info.eye_to_screen_distance)
  buf.write_float_le(info.lens_separation_distance)
  buf.write_float_le(info.interpupillary_distance)
  for i in 0..<4 {
    buf.write_float_le(info.lens_distortion_values[i])
  }
  for i in 0..<4 {
    buf.write_float_le(info.chroma_ab_correction[i])
  }
  buf.to_bytes()
}