-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

--- Shared backgrounds for transparent image areas
-- @module core.ui.transparent

local config = require("core.config")
local style = require("core.style")

local transparent = {}

local DEFAULT_TILE_SIZE = 16
local DEFAULT_DARK_COLOR = { 86, 86, 86, 255 }
local DEFAULT_LIGHT_COLOR = { 122, 122, 122, 255 }

--- Resolve the shared transparency setting for one drawing operation
-- @param options table|nil optional tile_size, dark_color, and light_color values
-- @return string kind
-- @return number tile_size
-- @return table dark_color
-- @return table light_color
local function resolve_background(options)
	local background = config.transparent_background
	local tile_size = options and options.tile_size or DEFAULT_TILE_SIZE
	local dark_color = options and options.dark_color or DEFAULT_DARK_COLOR
	local light_color = options and options.light_color or DEFAULT_LIGHT_COLOR

	if background == "theme" then
		return "solid", tile_size, style.background, style.background
	elseif type(background) == "table" then
		return "solid", tile_size, background, background
	end

	return "pattern", tile_size, dark_color, light_color
end

--- Draw the configured background beneath one transparent area
-- @param x number area x
-- @param y number area y
-- @param width number area width
-- @param height number area height
-- @param options table|nil optional pattern appearance
function transparent.draw(x, y, width, height, options)
	local kind, tile_size, dark_color, light_color = resolve_background(options)

	if kind == "solid" then
		renderer.draw_rect(x, y, width, height, dark_color)
	else
		renderer.draw_checkerboard(x, y, width, height, tile_size, dark_color, light_color)
	end
end

--- Draw one tiled canvas over the configured background
-- @param image userdata source canvas
-- @param source_x number visible source x
-- @param source_y number visible source y
-- @param source_width number visible source width
-- @param source_height number visible source height
-- @param dest_x number destination x
-- @param dest_y number destination y
-- @param dest_width number destination width
-- @param dest_height number destination height
-- @param clip_x number clip x
-- @param clip_y number clip y
-- @param clip_width number clip width
-- @param clip_height number clip height
-- @param options table|nil optional pattern appearance
function transparent.draw_canvas_tiled(
	image,
	source_x, source_y, source_width, source_height,
	dest_x, dest_y, dest_width, dest_height,
	clip_x, clip_y, clip_width, clip_height,
	options
)
	local _, tile_size, dark_color, light_color = resolve_background(options)

	renderer.draw_canvas_tiled(
		image,
		source_x, source_y, source_width, source_height,
		dest_x, dest_y, dest_width, dest_height,
		clip_x, clip_y, clip_width, clip_height,
		tile_size, dark_color, light_color
	)
end

return transparent
